View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.profile.provider;
19  
20  import javax.servlet.RequestDispatcher;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.opensaml.ws.transport.InTransport;
25  import org.opensaml.ws.transport.OutTransport;
26  import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
27  import org.opensaml.ws.transport.http.HttpServletResponseAdapter;
28  import org.opensaml.xml.util.DatatypeHelper;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import edu.internet2.middleware.shibboleth.common.profile.AbstractErrorHandler;
33  
34  /**
35   * An error handler that forwards information to a JSP page for display to the end user.
36   * 
37   * The path to the JSP page provided to this handler may be absolute (i.e. start with a "/"), in which case the path is
38   * assumed to be from the root of the servlet context, or relative, in which case the page is assumed to be relative
39   * from the request dispatcher location. Deployers are strongly encouraged to use absolute paths.
40   * 
41   * The following request attributes are available to the JSP page:
42   * 
43   * <table>
44   * <th>
45   * <td>Attribute Name</td>
46   * <td>Object Type</td>
47   * <td>Value</td>
48   * </th>
49   * <tr>
50   * <td>requestError</td>
51   * <td>{@link Throwable}</td>
52   * <td>Error that was thrown that triggered the invocation of this handler. </td>
53   * </tr>
54   * </table>
55   */
56  public class JSPErrorHandler extends AbstractErrorHandler {
57  
58      /** Class logger. */
59      private final Logger log = LoggerFactory.getLogger(JSPErrorHandler.class);
60  
61      /** Path to JSP page. */
62      private String jspPage;
63  
64      /**
65       * Constructor.
66       * 
67       * @param page path to JSP page
68       */
69      public JSPErrorHandler(String page) {
70          jspPage = DatatypeHelper.safeTrimOrNullString(page);
71          if (jspPage == null) {
72              throw new IllegalArgumentException("JSP Error page may not be null.");
73          }
74      }
75  
76      /** {@inheritDoc} */
77      public void processRequest(InTransport in, OutTransport out) {
78          HttpServletRequest httpRequest = ((HttpServletRequestAdapter)in).getWrappedRequest();
79          HttpServletResponse httpResponse = ((HttpServletResponseAdapter)out).getWrappedResponse();
80          
81          RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(jspPage);
82          try {
83              dispatcher.forward(httpRequest, httpResponse);
84              return;
85          } catch (Throwable t) {
86              log.error("Could not dispatch to error JSP page: " + jspPage, t);
87              return;
88          }
89      }
90  }