1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40 public class ProfileRequestDispatcherServlet extends HttpServlet {
41
42
43 private static final long serialVersionUID = 3750548606378986211L;
44
45
46 private final Logger log = LoggerFactory.getLogger(ProfileRequestDispatcherServlet.class);
47
48
49 private final Logger accessLog = LoggerFactory.getLogger(AccessLogEntry.ACCESS_LOGGER_NAME);
50
51
52 private ProfileHandlerManager handlerManager;
53
54
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
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 }