IdP Protocol Handlers are now lookup up via regular expressions.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / IdPResponder.java
index 4e0ed01..74b549b 100644 (file)
 package edu.internet2.middleware.shibboleth.idp;
 
 import java.io.IOException;
 package edu.internet2.middleware.shibboleth.idp;
 
 import java.io.IOException;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URI;
-import java.net.URL;
 import java.util.HashMap;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Random;
 
 import javax.servlet.RequestDispatcher;
 import java.util.Random;
 
 import javax.servlet.RequestDispatcher;
@@ -58,10 +57,10 @@ 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.artifact.ArtifactMapperFactory;
 import edu.internet2.middleware.shibboleth.artifact.provider.MemoryArtifactMapper;
 import edu.internet2.middleware.shibboleth.common.Credentials;
+import edu.internet2.middleware.shibboleth.common.IdPConfigLoader;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
 import edu.internet2.middleware.shibboleth.common.NameMapper;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMapping;
 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
 import edu.internet2.middleware.shibboleth.common.NameMapper;
-import edu.internet2.middleware.shibboleth.common.IdPConfigLoader;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapperException;
 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapperException;
 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
@@ -243,24 +242,7 @@ public class IdPResponder extends HttpServlet {
                log.debug("Recieved a request via GET for location (" + request.getRequestURL() + ").");
 
                try {
                log.debug("Recieved a request via GET for location (" + request.getRequestURL() + ").");
 
                try {
-                       // Determine which protocol we are responding to (at this point normally Shibv1 vs. EAuth)
-                       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.");
-                       }
-
+                       IdPProtocolHandler activeHandler = lookupProtocolHandler(request);
                        // Pass request to the appropriate handler
                        log.info("Processing " + activeHandler.getHandlerName() + " request.");
                        if (activeHandler.processRequest(request, response, null, protocolSupport) != null) {
                        // Pass request to the appropriate handler
                        log.info("Processing " + activeHandler.getHandlerName() + " request.");
                        if (activeHandler.processRequest(request, response, null, protocolSupport) != null) {
@@ -303,24 +285,7 @@ public class IdPResponder extends HttpServlet {
                                                + samlRequest.toString());
                        }
 
                                                + samlRequest.toString());
                        }
 
-                       // Determine which protocol handler is active for this endpoint
-                       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.");
-                       }
-
+                       IdPProtocolHandler activeHandler = lookupProtocolHandler(request);
                        // Pass request to the appropriate handler and respond
                        log.info("Processing " + activeHandler.getHandlerName() + " request.");
 
                        // Pass request to the appropriate handler and respond
                        log.info("Processing " + activeHandler.getHandlerName() + " request.");
 
@@ -332,6 +297,29 @@ public class IdPResponder extends HttpServlet {
                }
        }
 
                }
        }
 
+       private IdPProtocolHandler lookupProtocolHandler(HttpServletRequest request) throws SAMLException {
+
+               // Determine which protocol handler is active for this endpoint
+               String requestURL = request.getRequestURL().toString();
+               IdPProtocolHandler activeHandler = null;
+
+               Iterator registeredLocations = protocolHandlers.keySet().iterator();
+               while (registeredLocations.hasNext()) {
+                       String handlerLocation = (String) registeredLocations.next();
+                       if (requestURL.matches(handlerLocation)) {
+                               log.debug("Matched handler location: (" + handlerLocation + ").");
+                               activeHandler = (IdPProtocolHandler) protocolHandlers.get(handlerLocation);
+                               break;
+                       }
+               }
+
+               if (activeHandler == null) {
+                       log.error("No protocol handler registered for location (" + request.getRequestURL() + ").");
+                       throw new SAMLException("Request submitted to an invalid location.");
+               }
+               return activeHandler;
+       }
+
        private void sendFailureToSAMLBinding(HttpServletResponse httpResponse, SAMLRequest samlRequest,
                        SAMLException exception) throws ServletException {
 
        private void sendFailureToSAMLBinding(HttpServletResponse httpResponse, SAMLRequest samlRequest,
                        SAMLException exception) throws ServletException {