[java-identity-provider] branch maint-4 updated: Add helper methods for accessing Spring beans in actions.

Scott Cantor cantor.2 at osu.edu
Wed Jul 20 19:17:04 UTC 2022


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

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

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

The following commit(s) were added to refs/heads/maint-4 by this push:
     new 7127abe88 Add helper methods for accessing Spring beans in actions.
7127abe88 is described below

commit 7127abe88549bf8c2c4e058bee652cdf4948a7ba
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         | 65 ++++++++++++++++++++++
 1 file changed, 65 insertions(+)

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 813dc4cf5..2fa778398 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
@@ -27,6 +27,7 @@ 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;
 
@@ -37,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;
@@ -180,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); 
             }
@@ -188,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

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


More information about the commits mailing list