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 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
40
41 public class ProfileRequestDispatcherServlet extends HttpServlet {
42
43
44 private static final long serialVersionUID = 3750548606378986211L;
45
46
47 private final Logger log = LoggerFactory.getLogger(ProfileRequestDispatcherServlet.class);
48
49
50 private final Logger accessLog = LoggerFactory.getLogger(AccessLogEntry.ACCESS_LOGGER_NAME);
51
52
53 private ProfileHandlerManager handlerManager;
54
55
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
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 }