Set communication profile before decoding
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / idp / profile / AbstractSAMLProfileHandler.java
index c795d80..19af4cc 100644 (file)
@@ -240,6 +240,21 @@ public abstract class AbstractSAMLProfileHandler extends
         supportedOutboundBindings = bindings;
     }
 
+    /** {@inheritDoc} */
+    public RelyingPartyConfiguration getRelyingPartyConfiguration(String relyingPartyId) {
+        try {
+            if (getMetadataProvider().getEntityDescriptor(relyingPartyId) == null) {
+                log.warn("No metadata for relying party {}, treating party as anonymous", relyingPartyId);
+                return getRelyingPartyConfigurationManager().getAnonymousRelyingConfiguration();
+            }
+        } catch (MetadataProviderException e) {
+            log.error("Unable to look up relying party metadata", e);
+            return null;
+        }
+
+        return super.getRelyingPartyConfiguration(relyingPartyId);
+    }
+
     /**
      * Populates the request context with information.
      * 
@@ -256,8 +271,8 @@ public abstract class AbstractSAMLProfileHandler extends
     protected void populateRequestContext(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
         populateRelyingPartyInformation(requestContext);
         populateAssertingPartyInformation(requestContext);
-        populateProfileInformation(requestContext);
         populateSAMLMessageInformation(requestContext);
+        populateProfileInformation(requestContext);
         populateUserInformation(requestContext);
     }
 
@@ -280,20 +295,13 @@ public abstract class AbstractSAMLProfileHandler extends
         EntityDescriptor relyingPartyMetadata;
         try {
             relyingPartyMetadata = metadataProvider.getEntityDescriptor(relyingPartyId);
+            requestContext.setPeerEntityMetadata(relyingPartyMetadata);
         } catch (MetadataProviderException e) {
             log.error("Error looking up metadata for relying party " + relyingPartyId, e);
             throw new ProfileException("Error looking up metadata for relying party " + relyingPartyId);
         }
-        
-        RelyingPartyConfiguration rpConfig = null;
-        if (relyingPartyMetadata != null) {
-            requestContext.setPeerEntityMetadata(relyingPartyMetadata);
-            rpConfig = getRelyingPartyConfiguration(relyingPartyId);
-        } else {
-            log.warn("No metadata for relying party {}, treating party as anonymous", relyingPartyId);
-            rpConfig = getRelyingPartyConfigurationManager().getAnonymousRelyingConfiguration();
-        }
 
+        RelyingPartyConfiguration rpConfig = getRelyingPartyConfiguration(relyingPartyId);
         if (rpConfig == null) {
             log.error("Unable to retrieve relying party configuration data for entity with ID {}", relyingPartyId);
             throw new ProfileException("Unable to retrieve relying party configuration data for entity with ID "
@@ -336,44 +344,52 @@ public abstract class AbstractSAMLProfileHandler extends
     }
 
     /**
-     * Populates the request context with the information about the profile. Unless overridden,
+     * Populates the request context with information from the inbound SAML message. Unless overridden,
      * {@link #populateRequestContext(BaseSAMLProfileRequestContext)} has already invoked
      * {@link #populateRelyingPartyInformation(BaseSAMLProfileRequestContext)},and
      * {@link #populateAssertingPartyInformation(BaseSAMLProfileRequestContext)} have already been invoked and the
      * properties they provide are available in the request context.
      * 
-     * This method requires the the following request context properties to be populated: relying party configuration
-     * 
-     * This methods populates the following request context properties: communication profile ID, profile configuration,
-     * outbound message artifact type, peer entity endpoint
      * 
      * @param requestContext current request context
      * 
-     * @throws ProfileException thrown if there is a problem populating the profile information
+     * @throws ProfileException thrown if there is a problem populating the request context with information
      */
-    protected void populateProfileInformation(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
-        requestContext.setCommunicationProfileId(getProfileId());
-        AbstractSAMLProfileConfiguration profileConfig = (AbstractSAMLProfileConfiguration) requestContext
-                .getRelyingPartyConfiguration().getProfileConfiguration(getProfileId());
-        requestContext.setProfileConfiguration(profileConfig);
-        requestContext.setOutboundMessageArtifactType(profileConfig.getOutboundArtifactType());
-        requestContext.setPeerEntityEndpoint(selectEndpoint(requestContext));
-    }
+    protected abstract void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext)
+            throws ProfileException;
 
     /**
-     * Populates the request context with information from the inbound SAML message. Unless overridden,
+     * Populates the request context with the information about the profile. Unless overridden,
      * {@link #populateRequestContext(BaseSAMLProfileRequestContext)} has already invoked
      * {@link #populateRelyingPartyInformation(BaseSAMLProfileRequestContext)},
      * {@link #populateAssertingPartyInformation(BaseSAMLProfileRequestContext)}, and
-     * {@link #populateProfileInformation(BaseSAMLProfileRequestContext)} have already been invoked and the properties
-     * they provide are available in the request context.
+     * {@link #populateSAMLMessageInformation(BaseSAMLProfileRequestContext)} have already been invoked and the
+     * properties they provide are available in the request context.
+     * 
+     * This method requires the the following request context properties to be populated: relying party configuration
+     * 
+     * This methods populates the following request context properties: communication profile ID, profile configuration,
+     * outbound message artifact type, peer entity endpoint
      * 
      * @param requestContext current request context
      * 
-     * @throws ProfileException thrown if there is a problem populating the request context with information
+     * @throws ProfileException thrown if there is a problem populating the profile information
      */
-    protected abstract void populateSAMLMessageInformation(BaseSAMLProfileRequestContext requestContext)
-            throws ProfileException;
+    protected void populateProfileInformation(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
+        AbstractSAMLProfileConfiguration profileConfig = (AbstractSAMLProfileConfiguration) requestContext
+                .getRelyingPartyConfiguration().getProfileConfiguration(getProfileId());
+        if (profileConfig != null) {
+            requestContext.setProfileConfiguration(profileConfig);
+            requestContext.setOutboundMessageArtifactType(profileConfig.getOutboundArtifactType());
+        }
+
+        Endpoint endpoint = selectEndpoint(requestContext);
+        if (endpoint == null) {
+            log.error("No return endpoint available for relying party {}", requestContext.getInboundMessageIssuer());
+            throw new ProfileException("No peer endpoint available to which to send SAML response");
+        }
+        requestContext.setPeerEntityEndpoint(endpoint);
+    }
 
     /**
      * Populates the request context with the information about the user if they have an existing session. Unless
@@ -413,15 +429,6 @@ public abstract class AbstractSAMLProfileHandler extends
      */
     protected void encodeResponse(BaseSAMLProfileRequestContext requestContext) throws ProfileException {
         try {
-
-            Endpoint peerEndpoint = requestContext.getPeerEntityEndpoint();
-            if (peerEndpoint == null) {
-                log
-                        .error("No return endpoint available for relying party {}", requestContext
-                                .getInboundMessageIssuer());
-                throw new ProfileException("No peer endpoint available to which to send SAML response");
-            }
-
             SAMLMessageEncoder encoder = getMessageEncoders().get(requestContext.getPeerEntityEndpoint().getBinding());
             if (encoder == null) {
                 log.error("No outbound message encoder configured for binding {}", requestContext