View Javadoc

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