[java-identity-provider] 01/03: Add helper methods for accessing Spring beans in actions.

Scott Cantor cantor.2 at osu.edu
Wed Jul 20 19:23:40 UTC 2022


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

scantor pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=51113225d16d19ef6244ca18e2f8548e205c063f

commit 51113225d16d19ef6244ca18e2f8548e205c063f
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Jul 20 15:17:01 2022 -0400

    Add helper methods for accessing Spring beans in actions.
---
 .../idp/profile/AbstractProfileAction.java         | 76 ++++++++++++++++++++--
 1 file changed, 71 insertions(+), 5 deletions(-)

diff --git a/idp-profile-api/src/main/java/net/shibboleth/idp/profile/AbstractProfileAction.java b/idp-profile-api/src/main/java/net/shibboleth/idp/profile/AbstractProfileAction.java
index 29bc559bc..3e9700e9a 100644
--- a/idp-profile-api/src/main/java/net/shibboleth/idp/profile/AbstractProfileAction.java
+++ b/idp-profile-api/src/main/java/net/shibboleth/idp/profile/AbstractProfileAction.java
@@ -25,6 +25,12 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.ThreadSafe;
 
+import net.shibboleth.idp.profile.context.SpringRequestContext;
+import net.shibboleth.idp.profile.context.navigate.WebflowRequestContextProfileRequestContextLookup;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
 import org.opensaml.profile.action.AbstractConditionalProfileAction;
 import org.opensaml.profile.action.EventIds;
 import org.opensaml.profile.action.ProfileAction;
@@ -32,6 +38,8 @@ import org.opensaml.profile.context.EventContext;
 import org.opensaml.profile.context.ProfileRequestContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.MessageSource;
 import org.springframework.context.MessageSourceAware;
 import org.springframework.context.MessageSourceResolvable;
@@ -41,10 +49,6 @@ import org.springframework.webflow.execution.Action;
 import org.springframework.webflow.execution.Event;
 import org.springframework.webflow.execution.RequestContext;
 
-import net.shibboleth.idp.profile.context.SpringRequestContext;
-import net.shibboleth.idp.profile.context.navigate.WebflowRequestContextProfileRequestContextLookup;
-import net.shibboleth.utilities.java.support.logic.Constraint;
-
 /**
  * Base class for Spring-aware profile actions.
  * 
@@ -179,6 +183,7 @@ public abstract class AbstractProfileAction
             } else if (event instanceof String) {
                 return ActionSupport.buildEvent(action, (String) eventCtx.getEvent());
             } else if (event instanceof AttributeMap) {
+                @SuppressWarnings("unchecked")
                 final AttributeMap<Object> map = (AttributeMap<Object>) eventCtx.getEvent();
                 return ActionSupport.buildEvent(action, map.getString("eventId", EventIds.PROCEED_EVENT_ID), map); 
             }
@@ -187,6 +192,67 @@ public abstract class AbstractProfileAction
         // A null value can be used to implicitly continue evaluating an action-state until the last step.
         return null;
     }
+    
+    /**
+     * Utilizes the active flow's {@link ApplicationContext} to obtain a bean of a given name and class.
+     * 
+     * @param <T> the bean type
+     * 
+     * @param profileRequestContext the profile request context
+     * @param name bean name
+     * @param claz bean type
+     * 
+     * @return the bean or null
+     * 
+     * @since 4.3.0
+     */
+    @Nullable protected <T> T getBean(@Nonnull final ProfileRequestContext profileRequestContext,
+            @Nonnull @NotEmpty final String name, @Nonnull final Class<T> claz) {
+        
+        final SpringRequestContext springRequestContext =
+                profileRequestContext.getSubcontext(SpringRequestContext.class);
+        if (springRequestContext == null) {
+            log.warn("{} Spring request context not found in profile request context", getLogPrefix());
+            return null;
+        }
+
+        final RequestContext requestContext = springRequestContext.getRequestContext();
+        if (requestContext == null) {
+            log.warn("{} Web Flow request context not found in Spring request context", getLogPrefix());
+            return null;
+        }
+
+        return getBean(requestContext, name, claz);
+    }
+
+    /**
+     * Utilizes the active flow's {@link ApplicationContext} to obtain a bean of a given name and class. 
+     * 
+     * @param <T> the bean type
+     * 
+     * @param flowRequestContext the active flow's request context
+     * @param name bean name
+     * @param claz bean type
+     * 
+     * @return the bean or null
+     * 
+     * @since 4.3.0
+     */
+    @Nullable protected <T> T getBean(@Nonnull final RequestContext flowRequestContext,
+            @Nonnull @NotEmpty final String name, @Nonnull final Class<T> claz) {
+        
+        try {
+            final Object bean = flowRequestContext.getActiveFlow().getApplicationContext().getBean(name);
+            if (bean != null && claz.isInstance(bean)) {
+                return claz.cast(bean);
+            }
+        } catch (final BeansException e) {
+            
+        }
+        
+        log.warn("{} No bean of the correct type found named {}", getLogPrefix(), name);
+        return null;
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -235,4 +301,4 @@ public abstract class AbstractProfileAction
         }
         return springRequestCtx.getRequestContext();
     }
-}
\ 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