1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.enduser;
20
21 import java.security.AccessControlException;
22 import javax.ws.rs.BadRequestException;
23 import javax.ws.rs.ForbiddenException;
24 import javax.xml.ws.WebServiceException;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.syncope.client.enduser.pages.Login;
27 import org.apache.syncope.common.lib.SyncopeClientException;
28 import org.apache.wicket.authorization.UnauthorizedInstantiationException;
29 import org.apache.wicket.core.request.handler.PageProvider;
30 import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
31 import org.apache.wicket.markup.html.pages.ExceptionErrorPage;
32 import org.apache.wicket.protocol.http.PageExpiredException;
33 import org.apache.wicket.request.IRequestHandler;
34 import org.apache.wicket.request.component.IRequestablePage;
35 import org.apache.wicket.request.cycle.IRequestCycleListener;
36 import org.apache.wicket.request.cycle.RequestCycle;
37 import org.apache.wicket.request.mapper.parameter.PageParameters;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class SyncopeEnduserRequestCycleListener implements IRequestCycleListener {
42
43 private static final Logger LOG = LoggerFactory.getLogger(SyncopeEnduserRequestCycleListener.class);
44
45 private Throwable instanceOf(final Exception e, final Class<? extends Exception> clazz) {
46 return clazz.isAssignableFrom(e.getClass())
47 ? e
48 : e.getCause() != null && clazz.isAssignableFrom(e.getCause().getClass())
49 ? e.getCause()
50 : e.getCause() != null && e.getCause().getCause() != null
51 && clazz.isAssignableFrom(e.getCause().getCause().getClass())
52 ? e.getCause().getCause()
53 : null;
54 }
55
56 @Override
57 public IRequestHandler onException(final RequestCycle cycle, final Exception e) {
58 LOG.error("Exception found", e);
59
60 PageParameters errorParameters = new PageParameters();
61
62 IRequestablePage errorPage;
63 if (instanceOf(e, UnauthorizedInstantiationException.class) != null) {
64 errorParameters.add("errorMessage", SyncopeEnduserSession.Error.AUTHORIZATION.fallback());
65 errorPage = new Login(errorParameters);
66 } else if (instanceOf(e, AccessControlException.class) != null) {
67 if (StringUtils.containsIgnoreCase(instanceOf(e, AccessControlException.class).getMessage(), "expired")) {
68 errorParameters.add("errorMessage", SyncopeEnduserSession.Error.SESSION_EXPIRED.fallback());
69 } else {
70 errorParameters.add("errorMessage", SyncopeEnduserSession.Error.AUTHORIZATION.fallback());
71 }
72 errorPage = new Login(errorParameters);
73 } else if (instanceOf(e, PageExpiredException.class) != null || !SyncopeEnduserSession.get().isSignedIn()) {
74 errorParameters.add("errorMessage", SyncopeEnduserSession.Error.SESSION_EXPIRED.fallback());
75 errorPage = new Login(errorParameters);
76 } else if (instanceOf(e, BadRequestException.class) != null
77 || instanceOf(e, WebServiceException.class) != null
78 || instanceOf(e, SyncopeClientException.class) != null) {
79
80 errorParameters.add("errorMessage", SyncopeEnduserSession.Error.REST.fallback());
81 errorPage = new Login(errorParameters);
82 } else {
83 Throwable cause = instanceOf(e, ForbiddenException.class);
84 if (cause == null) {
85
86 errorPage = new ExceptionErrorPage(e, null);
87 } else {
88 errorParameters.add("errorMessage", cause.getMessage());
89 errorPage = new Login(errorParameters);
90 }
91 }
92
93 if (errorPage instanceof Login) {
94 try {
95 SyncopeEnduserSession.get().invalidate();
96 } catch (Throwable t) {
97
98 LOG.debug("Unexpected error while forcing logout after error", t);
99 }
100 }
101
102 return new RenderPageRequestHandler(new PageProvider(errorPage));
103 }
104 }