[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
Thu Nov 13 08:00:13 EST 2014


Author: rdw
Date: Thu Nov 13 08:00:13 2014
New Revision: 6892

URL: http://svn.shibboleth.net/view/java-identity-provider?rev=6892&view=rev
Log:
IDP-512 optional extra class parameter to scripted function to police the output type

Added:
    trunk/idp-profile-api/src/test/java/net/shibboleth/idp/profile/context/navigate/ScriptedFunctionTest.java   (with props)
Modified:
    trunk/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/ScriptedContextLookupFunction.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=6892&r1=6891&r2=6892&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 Thu Nov 13 08:00:13 2014
@@ -39,7 +39,7 @@
 /**
  * A {@link Function} over a {@link ProfileRequestContext} which calls out to a supplied script.
  */
-public class ScriptedContextLookupFunction implements Function<ProfileRequestContext,Object> {
+public class ScriptedContextLookupFunction implements Function<ProfileRequestContext, Object> {
 
     /** The default language is Javascript. */
     @Nonnull @NotEmpty public static final String DEFAULT_ENGINE = "JavaScript";
@@ -52,6 +52,9 @@
 
     /** Debugging info. */
     @Nullable private final String logPrefix;
+
+    /** What class we want the output to test against. */
+    @Nullable private Class outputClass;
 
     /**
      * Constructor.
@@ -74,13 +77,33 @@
         logPrefix = "Anonymous Scripted Predicate :";
     }
 
+    /**
+     * Constructor.
+     * 
+     * @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);
+        outputClass = outputType;
+    }
+
     /** {@inheritDoc} */
     @Override public Object apply(@Nullable ProfileRequestContext profileContext) {
+
         final SimpleScriptContext scriptContext = new SimpleScriptContext();
         scriptContext.setAttribute("profileContext", profileContext, ScriptContext.ENGINE_SCOPE);
 
         try {
-            return script.eval(scriptContext);
+            Object output = script.eval(scriptContext);
+            if (null != outputClass && null != output && !outputClass.isInstance(output)) {
+                log.error("{} Output of type {} was not of type {}", logPrefix, output.getClass(), outputClass);
+                return null;
+            }
+            return output;
+
         } catch (final ScriptException e) {
             log.error("{} Error while executing Function script", logPrefix, e);
             return null;
@@ -96,10 +119,26 @@
      * @throws ScriptException if the compile fails
      * @throws IOException if the file doesn't exist.
      */
+    static ScriptedContextLookupFunction
+            resourceScript(@Nonnull @NotEmpty String engineName, @Nonnull Resource resource) throws ScriptException,
+                    IOException {
+        return resourceScript(engineName, resource, null);
+    }
+
+    /**
+     * Factory to create {@link ScriptedContextLookupFunction} from a {@link Resource}.
+     * 
+     * @param resource the resource to look at
+     * @param engineName the language
+     * @param outputType the type to test against.
+     * @return the function
+     * @throws ScriptException if the compile fails
+     * @throws IOException if the file doesn't exist.
+     */
     static ScriptedContextLookupFunction resourceScript(@Nonnull @NotEmpty String engineName,
-            @Nonnull Resource resource) throws ScriptException, IOException {
+            @Nonnull Resource resource, @Nullable Class outputType) throws ScriptException, IOException {
         final EvaluableScript script = new EvaluableScript(engineName, resource.getFile());
-        return new ScriptedContextLookupFunction(script, resource.getDescription());
+        return new ScriptedContextLookupFunction(script, resource.getDescription(), outputType);
     }
 
     /**
@@ -111,7 +150,21 @@
      * @throws IOException if the file doesn't exist.
      */
     static ScriptedContextLookupFunction resourceScript(Resource resource) throws ScriptException, IOException {
-        return resourceScript(DEFAULT_ENGINE, resource);
+        return resourceScript(DEFAULT_ENGINE, resource, null);
+    }
+
+    /**
+     * Factory to create {@link ScriptedContextLookupFunction} from a {@link Resource}.
+     * 
+     * @param resource the resource to look at

[... 55 lines stripped ...]


More information about the commits mailing list