View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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                  // redirect to default Wicket error page
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                  // ignore
98                  LOG.debug("Unexpected error while forcing logout after error", t);
99              }
100         }
101 
102         return new RenderPageRequestHandler(new PageProvider(errorPage));
103     }
104 }