| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package nu.localhost.tapestry.acegi.services.internal; |
| 19 | |
|
| 20 | |
import java.io.IOException; |
| 21 | |
|
| 22 | |
import javax.servlet.FilterChain; |
| 23 | |
import javax.servlet.ServletException; |
| 24 | |
import javax.servlet.ServletRequest; |
| 25 | |
import javax.servlet.ServletResponse; |
| 26 | |
import javax.servlet.http.HttpServletRequest; |
| 27 | |
import javax.servlet.http.HttpServletResponse; |
| 28 | |
|
| 29 | |
import org.acegisecurity.AccessDeniedException; |
| 30 | |
import org.acegisecurity.AcegiSecurityException; |
| 31 | |
import org.acegisecurity.AuthenticationException; |
| 32 | |
import org.acegisecurity.InsufficientAuthenticationException; |
| 33 | |
import org.acegisecurity.context.SecurityContextHolder; |
| 34 | |
import org.acegisecurity.ui.AccessDeniedHandler; |
| 35 | |
import org.acegisecurity.ui.AccessDeniedHandlerImpl; |
| 36 | |
import org.acegisecurity.ui.ExceptionTranslationFilter; |
| 37 | |
import org.apache.commons.logging.Log; |
| 38 | |
import org.apache.commons.logging.LogFactory; |
| 39 | |
import org.apache.tapestry.ioc.internal.util.TapestryException; |
| 40 | |
import org.springframework.util.Assert; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public class AcegiExceptionTranslationFilter |
| 48 | |
extends ExceptionTranslationFilter { |
| 49 | 0 | private static final Log logger = |
| 50 | 0 | LogFactory.getLog(ExceptionTranslationFilter.class); |
| 51 | |
|
| 52 | 0 | private AccessDeniedHandler accessDeniedHandler = |
| 53 | 0 | new AccessDeniedHandlerImpl(); |
| 54 | |
|
| 55 | |
public void doFilter(final ServletRequest request, |
| 56 | |
final ServletResponse response, |
| 57 | |
final FilterChain chain) throws IOException, ServletException { |
| 58 | 0 | if (!(request instanceof HttpServletRequest)) { |
| 59 | 0 | throw new ServletException("HttpServletRequest required"); |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | if (!(response instanceof HttpServletResponse)) { |
| 63 | 0 | throw new ServletException("HttpServletResponse required"); |
| 64 | |
} |
| 65 | |
|
| 66 | |
try { |
| 67 | 0 | chain.doFilter(request, response); |
| 68 | |
|
| 69 | 0 | if (logger.isDebugEnabled()) { |
| 70 | 0 | logger.debug("Chain processed normally"); |
| 71 | |
} |
| 72 | 0 | } catch (AuthenticationException ex) { |
| 73 | 0 | handleException(request, response, chain, ex); |
| 74 | 0 | } catch (AccessDeniedException ex) { |
| 75 | 0 | handleException(request, response, chain, ex); |
| 76 | 0 | } catch (TapestryException ex) { |
| 77 | 0 | Throwable cause = getRootCause(ex); |
| 78 | 0 | if (cause instanceof AuthenticationException || cause instanceof AccessDeniedException) { |
| 79 | 0 | handleException(request, response, chain, (AcegiSecurityException) cause); |
| 80 | 0 | } else { |
| 81 | 0 | throw ex; |
| 82 | 0 | } |
| 83 | 0 | } catch (ServletException ex) { |
| 84 | 0 | Throwable cause = ex.getRootCause(); |
| 85 | 0 | if (cause instanceof AuthenticationException || cause instanceof AccessDeniedException) { |
| 86 | 0 | handleException(request, response, chain, (AcegiSecurityException) cause); |
| 87 | 0 | } else { |
| 88 | 0 | throw ex; |
| 89 | 0 | } |
| 90 | 0 | } catch (IOException ex) { |
| 91 | 0 | throw ex; |
| 92 | 0 | } |
| 93 | 0 | } |
| 94 | 0 | |
| 95 | 0 | private Throwable getRootCause(Throwable t) { |
| 96 | 0 | if (t.getCause() == null) { |
| 97 | 0 | return t; |
| 98 | |
} |
| 99 | 0 | return getRootCause(t.getCause()); |
| 100 | |
} |
| 101 | 0 | |
| 102 | 0 | private void handleException(final ServletRequest request, |
| 103 | 0 | final ServletResponse response, |
| 104 | 0 | final FilterChain chain, final AcegiSecurityException exception) |
| 105 | 0 | throws IOException, ServletException { |
| 106 | 0 | if (exception instanceof AuthenticationException) { |
| 107 | 0 | if (logger.isDebugEnabled()) { |
| 108 | 0 | logger.debug("Authentication exception occurred; redirecting to authentication entry point", exception); |
| 109 | 0 | } |
| 110 | 0 | |
| 111 | 0 | sendStartAuthentication(request, response, chain, |
| 112 | 0 | (AuthenticationException) exception); |
| 113 | 0 | } else if (exception instanceof AccessDeniedException) { |
| 114 | 0 | if (getAuthenticationTrustResolver().isAnonymous( |
| 115 | 0 | SecurityContextHolder.getContext().getAuthentication())) { |
| 116 | 0 | if (logger.isDebugEnabled()) { |
| 117 | 0 | logger.debug("Access is denied (user is anonymous); redirecting to authentication entry point", |
| 118 | 0 | exception); |
| 119 | 0 | } |
| 120 | 0 | |
| 121 | 0 | sendStartAuthentication(request, response, chain, |
| 122 | 0 | new InsufficientAuthenticationException("Full authentication is required to access this resource")); |
| 123 | 0 | } else { |
| 124 | 0 | if (logger.isDebugEnabled()) { |
| 125 | 0 | logger.debug("Access is denied (user is not anonymous); delegating to AccessDeniedHandler", |
| 126 | 0 | exception); |
| 127 | 0 | } |
| 128 | 0 | |
| 129 | 0 | this.accessDeniedHandler.handle(request, response, |
| 130 | 0 | (AccessDeniedException) exception); |
| 131 | |
} |
| 132 | 0 | } |
| 133 | 0 | } |
| 134 | 0 | |
| 135 | 0 | public void setAccessDeniedHandler( |
| 136 | 0 | final AccessDeniedHandler accessDeniedHandler) { |
| 137 | 0 | Assert.notNull(accessDeniedHandler, "AccessDeniedHandler required"); |
| 138 | 0 | this.accessDeniedHandler = accessDeniedHandler; |
| 139 | 0 | } |
| 140 | |
} |