[java-opensaml] 05/16: Refactor to use abstract base class for delegating ProfileActions.

Brent Putman putmanb at georgetown.edu
Sun Dec 17 00:08:15 EST 2017


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=58acce821f03bf7c3c1fd3ae4af8fcd492cae0c0

commit 58acce821f03bf7c3c1fd3ae4af8fcd492cae0c0
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Sep 1 19:14:19 2017 -0400

    Refactor to use abstract base class for delegating ProfileActions.
---
 .../AbstractHandlerDelegatingProfileAction.java    | 156 +++++++++++++++++++++
 .../impl/PopulateSignatureSigningParameters.java   |  78 +++--------
 2 files changed, 177 insertions(+), 57 deletions(-)

diff --git a/opensaml-profile-api/src/main/java/org/opensaml/profile/action/AbstractHandlerDelegatingProfileAction.java b/opensaml-profile-api/src/main/java/org/opensaml/profile/action/AbstractHandlerDelegatingProfileAction.java
new file mode 100644
index 0000000..feddecd
--- /dev/null
+++ b/opensaml-profile-api/src/main/java/org/opensaml/profile/action/AbstractHandlerDelegatingProfileAction.java
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.opensaml.profile.action;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.messaging.context.navigate.ContextDataLookupFunction;
+import org.opensaml.messaging.handler.MessageHandler;
+import org.opensaml.messaging.handler.MessageHandlerException;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.opensaml.profile.context.navigate.ParentProfileRequestContextLookup;
+
+import com.google.common.base.Function;
+import com.google.common.base.Functions;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+
+import net.shibboleth.utilities.java.support.component.DestructableComponent;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * Base class for a profile action which just delegates to an instance of {@link MessageHandler}.
+ * 
+ * @param <DelegateType> type of MessageHandler to which to delegate.
+ */
+public abstract class AbstractHandlerDelegatingProfileAction<DelegateType extends MessageHandler> 
+        extends AbstractConditionalProfileAction {
+    
+    /** Lookup function for parent ProfileRequestContext. */
+    private static final ParentProfileRequestContextLookup PRC_LOOKUP = new ParentProfileRequestContextLookup();
+    
+    /** The message handler delegate. */
+    @Nonnull private DelegateType delegate;
+    
+    /** Lookup function for the message context on which to operate. */
+    @Nonnull private ContextDataLookupFunction<ProfileRequestContext, MessageContext> messageContextLookup;
+    
+    /**
+     * Constructor.
+     *
+     * @param delegateClass the delegate class. Must have a no-argument constructor. For those that do not,
+     *          instead pass in a pre-constructed instance via 
+     *          {@link AbstractHandlerDelegatingProfileAction(MessageHandler, ContextDataLookupFunction)}.
+     * @param lookup the lookup function for the message context on which to operate, 
+     *          typically for either the inbound or outbound context
+     */
+    public AbstractHandlerDelegatingProfileAction(@Nonnull final Class<DelegateType> delegateClass, 
+            @Nonnull final ContextDataLookupFunction<ProfileRequestContext, MessageContext> lookup) {
+        Constraint.isNotNull(delegateClass, "Delegate class may not be null");
+        try {
+            delegate = delegateClass.newInstance();
+        } catch (final InstantiationException | IllegalAccessException e) {
+            throw new RuntimeException(e);
+        }
+        
+        messageContextLookup = Constraint.isNotNull(lookup, "MessageContext lookup function may not be null");
+    }
+    
+    /**
+     * Constructor.
+     *
+     * @param delegateInstance the delegate instance
+     * @param lookup the lookup function for the message context on which to operate, 
+     *          typically for either the inbound or outbound context
+     */
+    public AbstractHandlerDelegatingProfileAction(@Nonnull final DelegateType delegateInstance, 
+            @Nonnull final ContextDataLookupFunction<ProfileRequestContext, MessageContext> lookup) {
+        delegate = Constraint.isNotNull(delegateInstance, "Delegate instance may not be null");
+        messageContextLookup = Constraint.isNotNull(lookup, "MessageContext lookup function may not be null");
+    }
+    
+    /** {@inheritDoc} */
+    protected void doDestroy() {
+        super.doDestroy();
+        if (delegate != null && delegate instanceof DestructableComponent) {
+            ((DestructableComponent) delegate).destroy();
+        }
+    }
+    
+    /**
+     * Get the delegate instance.
+     * 
+     * @return the delegate instance
+     */
+    @Nonnull protected DelegateType getDelegate() {
+        return delegate;
+    }
+    
+    /**
+     * Adapt a {@link ProfileRequestContext} predicate into a {@link MessageContext} predicate via composing
+     * with a lookup function.
+     * 
+     * @param predicate the profile request context predicate
+     * @return the message context predicate
+     */
+    @Nullable protected Predicate<MessageContext> adapt(@Nullable final Predicate<ProfileRequestContext> predicate) {
+        if (predicate == null) {
+            return null;
+        } else {
+            return Predicates.compose(predicate, PRC_LOOKUP);
+        }
+    }
+
+    /**
+     * Adapt a {@link ProfileRequestContext} function to a {@link MessageContext} function via composing
+     * with a lookup function.
+     * 
+     * @param function the profile request context function
+     * @return the message context function
+     * 
+     * @param <T> the output type of the functions
+     */
+    @Nullable protected <T> Function<MessageContext, T> adapt(
+            @Nullable final Function<ProfileRequestContext, T> function) {
+        if (function == null) {
+            return null;
+        } else {
+            return Functions.compose(function, PRC_LOOKUP);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
+        final MessageContext messageContext = messageContextLookup.apply(profileRequestContext);
+        if (messageContext == null) {
+            ActionSupport.buildEvent(profileRequestContext, EventIds.INVALID_MSG_CTX);
+            return;
+        }
+        
+        try {
+            delegate.invoke(messageContext);
+            ActionSupport.buildProceedEvent(profileRequestContext);
+        } catch (final MessageHandlerException e) {
+            ActionSupport.buildEvent(profileRequestContext, EventIds.MESSAGE_PROC_ERROR);
+        }
+    }
+
+}
diff --git a/opensaml-saml-impl/src/main/java/org/opensaml/saml/common/profile/impl/PopulateSignatureSigningParameters.java b/opensaml-saml-impl/src/main/java/org/opensaml/saml/common/profile/impl/PopulateSignatureSigningParameters.java
index f3b91c2..6bbc337 100644
--- a/opensaml-saml-impl/src/main/java/org/opensaml/saml/common/profile/impl/PopulateSignatureSigningParameters.java
+++ b/opensaml-saml-impl/src/main/java/org/opensaml/saml/common/profile/impl/PopulateSignatureSigningParameters.java
@@ -24,13 +24,11 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import org.opensaml.messaging.context.navigate.ChildContextLookup;
-import org.opensaml.messaging.handler.MessageHandlerException;
-import org.opensaml.profile.action.AbstractConditionalProfileAction;
-import org.opensaml.profile.action.ActionSupport;
+import org.opensaml.profile.action.AbstractHandlerDelegatingProfileAction;
 import org.opensaml.profile.action.EventIds;
 import org.opensaml.profile.context.ProfileRequestContext;
 import org.opensaml.profile.context.navigate.OutboundMessageContextLookup;
-import org.opensaml.profile.context.navigate.ParentProfileRequestContextLookup;
+import org.opensaml.saml.common.binding.impl.PopulateSignatureSigningParametersHandler;
 import org.opensaml.saml.common.messaging.context.SAMLMetadataContext;
 import org.opensaml.saml.common.messaging.context.SAMLPeerEntityContext;
 import org.opensaml.xmlsec.SecurityConfigurationSupport;
@@ -57,7 +55,8 @@ import net.shibboleth.utilities.java.support.logic.Constraint;
  * @event {@link EventIds#INVALID_MSG_CTX}
  * @event {@link EventIds#MESSAGE_PROC_ERROR}
  */
-public class PopulateSignatureSigningParameters extends AbstractConditionalProfileAction {
+public class PopulateSignatureSigningParameters 
+        extends AbstractHandlerDelegatingProfileAction<PopulateSignatureSigningParametersHandler> {
 
     /** Class logger. */
     @Nonnull private final Logger log = LoggerFactory.getLogger(PopulateSignatureSigningParameters.class);
@@ -78,13 +77,12 @@ public class PopulateSignatureSigningParameters extends AbstractConditionalProfi
     /** Resolver for parameters to store into context. */
     @NonnullAfterInit private SignatureSigningParametersResolver resolver;
     
-    /** MessageHandler delegate. */
-    @NonnullAfterInit private org.opensaml.saml.common.binding.impl.PopulateSignatureSigningParameters delegate;
-    
     /**
      * Constructor.
      */
     public PopulateSignatureSigningParameters() {
+        super(PopulateSignatureSigningParametersHandler.class, new OutboundMessageContextLookup());
+
         // Create context by default.
         securityParametersContextLookupStrategy = Functions.compose(
                 new ChildContextLookup<>(SecurityParametersContext.class, true), new OutboundMessageContextLookup());
@@ -160,17 +158,6 @@ public class PopulateSignatureSigningParameters extends AbstractConditionalProfi
     }
     
     /** {@inheritDoc} */
-    protected boolean doPreExecute(final ProfileRequestContext profileRequestContext) {
-        if (super.doPreExecute(profileRequestContext)) {
-            log.debug("{} Signing enabled", getLogPrefix());
-            return true;
-        } else {
-            log.debug("{} Signing not enabled", getLogPrefix());
-            return false;
-        }
-    }
-
-    /** {@inheritDoc} */
     @Override
     protected void doInitialize() throws ComponentInitializationException {
         super.doInitialize();
@@ -185,50 +172,27 @@ public class PopulateSignatureSigningParameters extends AbstractConditionalProfi
                 }
             };
         }
-        
-        final ParentProfileRequestContextLookup prcLookup = new ParentProfileRequestContextLookup();
-        
-        delegate = new org.opensaml.saml.common.binding.impl.PopulateSignatureSigningParameters();
-        
+
+        final PopulateSignatureSigningParametersHandler delegate = getDelegate();
         delegate.setSignatureSigningParametersResolver(resolver);
-        delegate.setConfigurationLookupStrategy(Functions.compose(configurationLookupStrategy, prcLookup));
-        delegate.setSecurityParametersContextLookupStrategy(
-                Functions.compose(securityParametersContextLookupStrategy, prcLookup));
-        
-        if (existingParametersContextLookupStrategy != null) {
-            delegate.setExistingParametersContextLookupStrategy(
-                    Functions.compose(existingParametersContextLookupStrategy, prcLookup));
-        }
-        
-        if (metadataContextLookupStrategy != null) {
-            delegate.setMetadataContextLookupStrategy(Functions.compose(metadataContextLookupStrategy, prcLookup));
-        }
-        
+        delegate.setConfigurationLookupStrategy(adapt(configurationLookupStrategy));
+        delegate.setSecurityParametersContextLookupStrategy(adapt(securityParametersContextLookupStrategy));
+        delegate.setExistingParametersContextLookupStrategy(adapt(existingParametersContextLookupStrategy));
+        delegate.setMetadataContextLookupStrategy(adapt(metadataContextLookupStrategy));
         delegate.initialize();
     }
-
-    /** {@inheritDoc} */
-    protected void doDestroy() {
-        super.doDestroy();
-        if (delegate != null) {
-            delegate.destroy();
-        }
-    }
-
+    
     /** {@inheritDoc} */
     @Override
-    protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
-        if (profileRequestContext.getOutboundMessageContext() == null) {
-            ActionSupport.buildEvent(profileRequestContext, EventIds.INVALID_MSG_CTX);
-            return;
-        }
+    protected boolean doPreExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
         
-        try {
-            delegate.invoke(profileRequestContext.getOutboundMessageContext());
-            ActionSupport.buildProceedEvent(profileRequestContext);
-        } catch (final MessageHandlerException e) {
-            ActionSupport.buildEvent(profileRequestContext, EventIds.MESSAGE_PROC_ERROR);
+        if (super.doPreExecute(profileRequestContext)) {
+            log.debug("{} Signing enabled", getLogPrefix());
+            return true;
+        } else {
+            log.debug("{} Signing not enabled", getLogPrefix());
+            return false;
         }
     }
-    
+
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list