View Javadoc

1   /*
2    * Copyright [2006] [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;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.ServletConfig;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.opensaml.ws.transport.http.HTTPInTransport;
28  import org.opensaml.ws.transport.http.HTTPOutTransport;
29  import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
30  import org.opensaml.ws.transport.http.HttpServletResponseAdapter;
31  import org.opensaml.xml.util.DatatypeHelper;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import edu.internet2.middleware.shibboleth.common.log.AccessLogEntry;
36  
37  /**
38   * Servlet responsible for dispatching incoming requests to the appropriate {@link ProfileHandler}.
39   */
40  public class ProfileRequestDispatcherServlet extends HttpServlet {
41  
42      /** Serial version UID. */
43      private static final long serialVersionUID = 3750548606378986211L;
44  
45      /** Class logger. */
46      private final Logger log = LoggerFactory.getLogger(ProfileRequestDispatcherServlet.class);
47  
48      /** Access logger. */
49      private final Logger accessLog = LoggerFactory.getLogger(AccessLogEntry.ACCESS_LOGGER_NAME);
50  
51      /** Profile handler manager. */
52      private ProfileHandlerManager handlerManager;
53  
54      /** {@inheritDoc} */
55      public void init(ServletConfig config) throws ServletException {
56          super.init(config);
57  
58          String handlerManagerId = config.getInitParameter("handlerManagerId");
59          if (DatatypeHelper.isEmpty(handlerManagerId)) {
60              handlerManagerId = "shibboleth.HandlerManager";
61          }
62  
63          handlerManager = (ProfileHandlerManager) getServletContext().getAttribute(handlerManagerId);
64      }
65  
66      /** {@inheritDoc} */
67      @SuppressWarnings("unchecked")
68      protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException,
69              IOException {
70          if (accessLog.isInfoEnabled()) {
71              AccessLogEntry accessEntry = new AccessLogEntry(httpRequest);
72              accessLog.info(accessEntry.toString());
73          }
74  
75          HTTPInTransport profileReq = new HttpServletRequestAdapter(httpRequest);
76          HTTPOutTransport profileResp = new HttpServletResponseAdapter(httpResponse, httpRequest.isSecure());
77  
78          AbstractErrorHandler errorHandler = handlerManager.getErrorHandler();
79          ProfileHandler handler = handlerManager.getProfileHandler(httpRequest);
80          if (handler != null) {
81              try {
82                  handler.processRequest(profileReq, profileResp);
83                  return;
84              } catch (Throwable t) {
85                  log.error("Error processing profile request", t);
86                  if (t.getMessage() != null) {
87                      httpRequest.setAttribute(AbstractErrorHandler.ERROR_KEY, t);
88                  }
89              }
90          } else {
91              log.warn("No profile handler configured for request at path: {}", httpRequest.getPathInfo());
92              httpRequest.setAttribute(AbstractErrorHandler.ERROR_KEY, new NoProfileHandlerException(
93                      "No profile handler configured for request at path: " + httpRequest.getPathInfo()));
94          }
95  
96          errorHandler.processRequest(profileReq, profileResp);
97          return;
98      }
99  }