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