4346be7a81adddd37f434dd0dc5923120f2ccc1c
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / provider / SAMLv1_Base_QueryHandler.java
1
2 package edu.internet2.middleware.shibboleth.idp.provider;
3
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6
7 import org.apache.log4j.Logger;
8 import org.opensaml.NoSuchProviderException;
9 import org.opensaml.SAMLBinding;
10 import org.opensaml.SAMLBindingFactory;
11 import org.opensaml.SAMLException;
12 import org.opensaml.SAMLRequest;
13 import org.opensaml.SAMLResponse;
14 import org.w3c.dom.Element;
15
16 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
17 import edu.internet2.middleware.shibboleth.idp.RequestHandlingException;
18
19 public abstract class SAMLv1_Base_QueryHandler extends BaseServiceHandler {
20
21         private static Logger log = Logger.getLogger(SAMLv1_Base_QueryHandler.class.getName());
22         protected SAMLBinding binding;
23
24         protected SAMLv1_Base_QueryHandler(Element config) throws ShibbolethConfigurationException {
25
26                 super(config);
27
28                 try {
29                         binding = SAMLBindingFactory.getInstance(SAMLBinding.SOAP);
30                 } catch (NoSuchProviderException e) {
31                         log.error("Unable to initialize SAML SOAP binding:" + e);
32                         throw new ShibbolethConfigurationException("Couldn't initialize " + getHandlerName() + " handler.");
33                 }
34         }
35
36         protected SAMLRequest parseSAMLRequest(HttpServletRequest request) throws RequestHandlingException {
37
38                 // Parse SOAP request and marshall SAML request object
39                 SAMLRequest samlRequest = null;
40                 try {
41                         samlRequest = binding.receive(request, 1);
42                 } catch (SAMLException e) {
43                         log.error("Unable to parse request: " + e);
44                         throw new RequestHandlingException("Invalid request data.");
45                 }
46
47                 // If we have DEBUG logging turned on, dump out the request to the log
48                 // This takes some processing, so only do it if we need to
49                 if (log.isDebugEnabled()) {
50                         log
51                                         .debug("Dumping generated SAML Request:" + System.getProperty("line.separator")
52                                                         + samlRequest.toString());
53                 }
54                 return samlRequest;
55         }
56
57         protected void respondWithError(HttpServletResponse response, SAMLRequest samlRequest, SAMLException e)
58                         throws RequestHandlingException {
59
60                 log.error("Error while processing request: " + e);
61                 try {
62                         SAMLResponse samlResponse = new SAMLResponse((samlRequest != null) ? samlRequest.getId() : null, null,
63                                         null, e);
64                         if (log.isDebugEnabled()) {
65                                 log.debug("Dumping generated SAML Error Response:" + System.getProperty("line.separator")
66                                                 + samlResponse.toString());
67                         }
68                         binding.respond(response, samlResponse, null);
69                         log.debug("Returning SAML Error Response.");
70                 } catch (SAMLException se) {
71                         try {
72                                 binding.respond(response, null, e);
73                         } catch (SAMLException e1) {
74                                 log.error("Caught exception while responding to requester: " + e.getMessage());
75                                 throw new RequestHandlingException(e1.getMessage());
76                         }
77                 }
78         }
79
80 }