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