[java-identity-provider COMMIT] in /trunk/idp-profile-api/src: main/java/net/shibboleth/idp/profile/context/navigate/...

noreply at shibboleth.net noreply at shibboleth.net
Sat Nov 15 16:24:17 EST 2014


Author: rdw
Date: Sat Nov 15 16:24:17 2014
New Revision: 6915

URL: http://svn.shibboleth.net/view/java-identity-provider?rev=6915&view=rev
Log:
IDP-612 Add factory methods to create scripted Function<MessageContext, Object>

Modified:
    trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.java
    trunk/idp-profile-api/src/test/java/net/shibboleth/idp/profile/context/navigate/ScriptedFunctionTest.java

Modified: trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.java
URL: http://svn.shibboleth.net/view/java-identity-provider/trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.java?rev=6915&r1=6914&r2=6915&view=diff
==============================================================================
--- trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.java (original)
+++ trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.java Sat Nov 15 16:24:17 2014
@@ -29,6 +29,8 @@
 import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.scripting.EvaluableScript;
 
+import org.opensaml.messaging.context.BaseContext;
+import org.opensaml.messaging.context.MessageContext;
 import org.opensaml.profile.context.ProfileRequestContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,9 +39,11 @@
 import com.google.common.base.Function;
 
 /**
- * A {@link Function} over a {@link ProfileRequestContext} which calls out to a supplied script.
+ * A {@link Function} over a {@link BaseContext} which calls out to a supplied script.
+ * 
+ * @param <T> The specific type of context (either {@link ProfileRequestContext} or {@link MessageContext})
  */
-public class ScriptedContextLookupFunction implements Function<ProfileRequestContext, Object> {
+public class ScriptedContextLookupFunction<T extends BaseContext> implements Function<T, Object> {
 
     /** The default language is Javascript. */
     @Nonnull @NotEmpty public static final String DEFAULT_ENGINE = "JavaScript";
@@ -56,13 +60,19 @@
     /** What class we want the output to test against. */
     @Nullable private Class outputClass;
 
+    /** What class we want the output to test against. */
+    @Nonnull private final Class<T> inputClass;
+
     /**
      * Constructor.
      * 
+     * @param inClass the class we accept as input.
      * @param theScript the script we will evaluate.
      * @param extraInfo debugging information.
      */
-    public ScriptedContextLookupFunction(@Nonnull EvaluableScript theScript, @Nullable String extraInfo) {
+    protected ScriptedContextLookupFunction(@Nonnull Class<T> inClass, @Nonnull EvaluableScript theScript,
+            @Nullable String extraInfo) {
+        inputClass = Constraint.isNotNull(inClass, "Supplied inputClass cannot be null");
         script = Constraint.isNotNull(theScript, "Supplied script cannot be null");
         logPrefix = "Scripted Predicate from " + extraInfo + " :";
     }
@@ -70,9 +80,11 @@
     /**
      * Constructor.
      * 
+     * @param inClass the class we accept as input.
      * @param theScript the script we will evaluate.
      */
-    public ScriptedContextLookupFunction(@Nonnull EvaluableScript theScript) {
+    protected ScriptedContextLookupFunction(@Nonnull Class<T> inClass, @Nonnull EvaluableScript theScript) {
+        inputClass = Constraint.isNotNull(inClass, "Supplied inputClass cannot be null");
         script = Constraint.isNotNull(theScript, "Supplied script should not be null");
         logPrefix = "Anonymous Scripted Predicate :";
     }
@@ -80,21 +92,27 @@
     /**
      * Constructor.
      * 
+     * @param inClass the class we accept as input.
      * @param theScript the script we will evaluate.
      * @param extraInfo debugging information.
      * @param outputType the type to test against.
      */
-    public ScriptedContextLookupFunction(@Nonnull EvaluableScript theScript, @Nullable String extraInfo,
-            @Nullable Class outputType) {
-        this(theScript, extraInfo);
+    protected ScriptedContextLookupFunction(@Nonnull Class<T> inClass, @Nonnull EvaluableScript theScript,
+            @Nullable String extraInfo, @Nullable Class outputType) {
+        this(inClass, theScript, extraInfo);
         outputClass = outputType;
     }
 
     /** {@inheritDoc} */
-    @Override public Object apply(@Nullable ProfileRequestContext profileContext) {
+    @Override public Object apply(@Nullable T context) {
+
+        if (null != context && !inputClass.isInstance(context)) {
+            throw new ClassCastException(logPrefix + " Input was type " + context.getClass()
+                    + " which is not an instance of " + inputClass);
+        }
 
         final SimpleScriptContext scriptContext = new SimpleScriptContext();
-        scriptContext.setAttribute("profileContext", profileContext, ScriptContext.ENGINE_SCOPE);

[... 350 lines stripped ...]


More information about the commits mailing list