Fixed an IdP configuration schema bug.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / IdPResponder.java
index 424afb8..b34edbe 100644 (file)
@@ -26,7 +26,9 @@
 package edu.internet2.middleware.shibboleth.idp;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.Random;
 
@@ -48,11 +50,13 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
-import sun.misc.BASE64Decoder;
 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
 import edu.internet2.middleware.shibboleth.aa.arp.ArpException;
 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolverException;
+import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
+import edu.internet2.middleware.shibboleth.artifact.ArtifactMapperFactory;
+import edu.internet2.middleware.shibboleth.artifact.provider.MemoryArtifactMapper;
 import edu.internet2.middleware.shibboleth.common.Credentials;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
@@ -143,7 +147,7 @@ public class IdPResponder extends HttpServlet {
                                                "ReleasePolicyEngine");
 
                                if (itemElements.getLength() > 1) {
-                                       log.warn("Encountered multiple <ReleasePolicyEngine> configuration elements.  Using first...");
+                                       log.warn("Encountered multiple <ReleasePolicyEngine/> configuration elements.  Using first...");
                                }
                                if (itemElements.getLength() < 1) {
                                        arpEngine = new ArpEngine();
@@ -161,9 +165,23 @@ public class IdPResponder extends HttpServlet {
                                throw new ShibbolethConfigurationException("Could not load Attribute Resolver.");
                        }
 
+                       // Load artifact mapping implementation
+                       ArtifactMapper artifactMapper = null;
+                       itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
+                                       "ArtifactMapper");
+                       if (itemElements.getLength() > 1) {
+                               log.warn("Encountered multiple <ArtifactMapper/> configuration elements.  Using first...");
+                       }
+                       if (itemElements.getLength() > 0) {
+                               artifactMapper = ArtifactMapperFactory.getInstance((Element) itemElements.item(0));
+                       } else {
+                               log.debug("No Artifact Mapper configuration found.  Defaulting to Memory-based implementation.");
+                               artifactMapper = new MemoryArtifactMapper();
+                       }
+
                        // Load protocol handlers and support library
                        protocolSupport = new IdPProtocolSupport(configuration, transactionLog, nameMapper, spMapper, arpEngine,
-                                       resolver);
+                                       resolver, artifactMapper);
                        itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
                                        "ProtocolHandler");
 
@@ -194,13 +212,13 @@ public class IdPResponder extends HttpServlet {
 
                        // Load metadata
                        itemElements = originConfig.getDocumentElement().getElementsByTagNameNS(IdPConfig.configNameSpace,
-                                       "FederationProvider");
+                                       "MetadataProvider");
                        for (int i = 0; i < itemElements.getLength(); i++) {
-                               protocolSupport.addFederationProvider((Element) itemElements.item(i));
+                               protocolSupport.addMetadataProvider((Element) itemElements.item(i));
                        }
                        if (protocolSupport.providerCount() < 1) {
-                               log.error("No Federation Provider metadata loaded.");
-                               throw new ShibbolethConfigurationException("Could not load federation metadata.");
+                               log.error("No Metadata Provider metadata loaded.");
+                               throw new ShibbolethConfigurationException("Could not load SAML metadata.");
                        }
 
                        log.info("Identity Provider initialization complete.");
@@ -226,8 +244,18 @@ public class IdPResponder extends HttpServlet {
 
                try {
                        // Determine which protocol we are responding to (at this point normally Shibv1 vs. EAuth)
-                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(request.getRequestURL()
-                                       .toString());
+                       String requestURL = request.getRequestURL().toString();
+                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(requestURL);
+                       if (activeHandler == null) {
+                               log.debug("No protocol handler registered for location (" + request.getRequestURL()
+                                               + ").  Attempting to match against relative path.");
+                               try {
+                                       activeHandler = (IdPProtocolHandler) protocolHandlers.get(new URL(requestURL).getPath());
+                               } catch (MalformedURLException e) {
+                                       // squelch, we will just fail to find a handler
+                               }
+                       }
+
                        if (activeHandler == null) {
                                log.error("No protocol handler registered for location (" + request.getRequestURL() + ").");
                                throw new SAMLException("Request submitted to an invalid location.");
@@ -271,24 +299,21 @@ public class IdPResponder extends HttpServlet {
                        // If we have DEBUG logging turned on, dump out the request to the log
                        // This takes some processing, so only do it if we need to
                        if (log.isDebugEnabled()) {
-                               try {
-                                       log.debug("Dumping generated SAML Request:"
-                                                       + System.getProperty("line.separator")
-                                                       + new String(new BASE64Decoder().decodeBuffer(new String(samlRequest.toBase64(), "ASCII")),
-                                                                       "UTF8"));
-                               } catch (SAMLException e) {
-                                       log.error("Encountered an error while decoding SAMLRequest for logging purposes.");
-                               } catch (IOException e) {
-                                       log.error("Encountered an error while decoding SAMLRequest for logging purposes.");
-                               }
+                               log.debug("Dumping generated SAML Request:" + System.getProperty("line.separator")
+                                               + samlRequest.toString());
                        }
 
                        // Determine which protocol handler is active for this endpoint
-                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(request.getRequestURL()
-                                       .toString());
+                       String requestURL = request.getRequestURL().toString();
+                       IdPProtocolHandler activeHandler = (IdPProtocolHandler) protocolHandlers.get(requestURL);
                        if (activeHandler == null) {
-                               log.error("No protocol handler registered for location (" + request.getRequestURL() + ").");
-                               throw new SAMLException("Request submitted to an invalid location.");
+                               log.debug("No protocol handler registered for location (" + request.getRequestURL()
+                                               + ").  Attempting to match against relative path.");
+                               try {
+                                       activeHandler = (IdPProtocolHandler) protocolHandlers.get(new URL(requestURL).getPath());
+                               } catch (MalformedURLException e) {
+                                       // squelch, we will just fail to find a handler
+                               }
                        }
 
                        // Pass request to the appropriate handler and respond
@@ -310,15 +335,8 @@ public class IdPResponder extends HttpServlet {
                        SAMLResponse samlResponse = new SAMLResponse((samlRequest != null) ? samlRequest.getId() : null, null,
                                        null, exception);
                        if (log.isDebugEnabled()) {
-                               try {
-                                       log.debug("Dumping generated SAML Error Response:"
-                                                       + System.getProperty("line.separator")
-                                                       + new String(
-                                                                       new BASE64Decoder().decodeBuffer(new String(samlResponse.toBase64(), "ASCII")),
-                                                                       "UTF8"));
-                               } catch (IOException e) {
-                                       log.error("Encountered an error while decoding SAMLReponse for logging purposes.");
-                               }
+                               log.debug("Dumping generated SAML Error Response:" + System.getProperty("line.separator")
+                                               + samlResponse.toString());
                        }
                        binding.respond(httpResponse, samlResponse, null);
                        log.debug("Returning SAML Error Response.");
@@ -349,28 +367,28 @@ public class IdPResponder extends HttpServlet {
 
 }
 
-class FederationProviderFactory {
+class MetadataProviderFactory {
 
-       private static Logger log = Logger.getLogger(FederationProviderFactory.class.getName());
+       private static Logger log = Logger.getLogger(MetadataProviderFactory.class.getName());
 
        public static Metadata loadProvider(Element e) throws MetadataException {
 
                String className = e.getAttribute("type");
                if (className == null || className.equals("")) {
-                       log.error("Federation Provider requires specification of the attribute \"type\".");
-                       throw new MetadataException("Failed to initialize Federation Provider.");
+                       log.error("Metadata Provider requires specification of the attribute \"type\".");
+                       throw new MetadataException("Failed to initialize Metadata Provider.");
                } else {
                        try {
                                Class[] params = {Class.forName("org.w3c.dom.Element"),};
                                return (Metadata) Class.forName(className).getConstructor(params).newInstance(new Object[]{e});
                        } catch (Exception loaderException) {
-                               log.error("Failed to load Federation Provider implementation class: " + loaderException);
+                               log.error("Failed to load Metadata Provider implementation class: " + loaderException);
                                Throwable cause = loaderException.getCause();
                                while (cause != null) {
                                        log.error("caused by: " + cause);
                                        cause = cause.getCause();
                                }
-                               throw new MetadataException("Failed to initialize Federation Provider.");
+                               throw new MetadataException("Failed to initialize Metadata Provider.");
                        }
                }
        }