[spring-extensions] branch main updated: JSPT-109 - Add scripted variants of additional functional interfaces

Scott Cantor cantor.2 at osu.edu
Wed Feb 24 23:13:08 UTC 2021


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

scantor pushed a commit to branch main
in repository spring-extensions.

View the commit online:
http://git.shibboleth.net/view/?p=spring-extensions.git;a=commit;h=b3096f6a03b9bb9e1eb47a65055c7730120ae3da

The following commit(s) were added to refs/heads/main by this push:
       new  b3096f6   JSPT-109 - Add scripted variants of additional functional interfaces
b3096f6 is described below

commit b3096f6a03b9bb9e1eb47a65055c7730120ae3da
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Feb 24 18:13:05 2021 -0500

    JSPT-109 - Add scripted variants of additional functional interfaces
    
    https://issues.shibboleth.net/jira/browse/JSPT-109
    
    Add new base class for multi-input SpringExpression variants.
---
 .../util/AbstractSpringExpressionEvaluator.java    |   4 +-
 ...va => AbstractSpringExpressionEvaluatorEx.java} | 103 ++++++++-------------
 .../ext/spring/util/SpringExpressionFunction.java  |  67 +++++++++++++-
 .../ext/spring/util/SpringExpressionPredicate.java |  49 +++++++++-
 .../ext/spring/util/SpringExpressionTest.java      |   4 +-
 5 files changed, 155 insertions(+), 72 deletions(-)

diff --git a/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java b/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java
index cfdb9e5..b241928 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java
@@ -40,7 +40,9 @@ import net.shibboleth.utilities.java.support.primitive.StringSupport;
  * @param <U> type of output
  * 
  * @since 5.4.0
+ * @deprecated
  */
+ at Deprecated(forRemoval=true, since="6.1.0")
 public abstract class AbstractSpringExpressionEvaluator<T, U> {
     
     /** Class logger. */
@@ -63,7 +65,7 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
     private boolean hideExceptions;
     
     /** Value to return from predicate when an error occurs. */
-    private U returnOnError;
+    @Nullable private U returnOnError;
 
     /**
      * Constructor.
diff --git a/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java b/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluatorEx.java
similarity index 70%
copy from src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java
copy to src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluatorEx.java
index cfdb9e5..e7ddf74 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluator.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/AbstractSpringExpressionEvaluatorEx.java
@@ -36,15 +36,12 @@ import net.shibboleth.utilities.java.support.primitive.StringSupport;
  * A component that evaluates a Spring EL expression against a set of inputs
  * and returns the result.
  * 
- * @param <T> type of input
- * @param <U> type of output
- * 
- * @since 5.4.0
+ * @since 6.1.0
  */
-public abstract class AbstractSpringExpressionEvaluator<T, U> {
+public abstract class AbstractSpringExpressionEvaluatorEx {
     
     /** Class logger. */
-    @Nonnull private final Logger log = LoggerFactory.getLogger(AbstractSpringExpressionEvaluator.class);
+    @Nonnull private final Logger log = LoggerFactory.getLogger(AbstractSpringExpressionEvaluatorEx.class);
 
     /** SpEL expression to evaluate. */
     @Nullable private String springExpression;
@@ -53,63 +50,50 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
     @Nullable private Object customObject;
     
     /** The output type. */
-    @Nullable private Class<U> outputType;
-
-    /** The input type. */
-    @Nullable private Class<T> inputType;
-
+    @Nullable private Class<?> outputType;
 
     /** Whether to raise runtime exceptions if expression fails. */
     private boolean hideExceptions;
     
     /** Value to return from predicate when an error occurs. */
-    private U returnOnError;
+    @Nullable private Object returnOnError;
 
     /**
      * Constructor.
      *
      * @param expression the expression to evaluate
      */
-    public AbstractSpringExpressionEvaluator(
+    public AbstractSpringExpressionEvaluatorEx(
             final @Nonnull @NotEmpty @ParameterName(name="expression") String expression) {
         springExpression = Constraint.isNotNull(StringSupport.trimOrNull(expression),
                 "Expression cannot be null or empty");
     }
     
-    /**
-     * Set the output type to be enforced.
-     * 
-     * @param type output type
-     */
-    public void setOutputType(@Nullable final Class<U> type) {
-        outputType = type;
-    }
-
     /**
      * Get the output type to be enforced.
      * 
      * @return output type
      */
-    @Nullable protected Class<U> getOutputType() {
+    @Nullable protected Class<?> getOutputType() {
         return outputType;
     }
 
     /**
-     * Get the input type to be enforced.
-     *
-     * @return input type
+     * Set the output type to be enforced.
+     * 
+     * @param type output type
      */
-    @Nullable public  Class<T> getInputType() {
-        return inputType;
+    protected void setOutputType(@Nullable final Class<?> type) {
+        outputType = type;
     }
 
     /**
-     * Set the input type to be enforced.
+     * Get the custom (externally provided) object.
      *
-     * @param type input type
+     * @return the custom object
      */
-    public void setInputType(@Nullable final Class<T> type) {
-        inputType = type;
+    @Nullable protected Object getCustomObject() {
+        return customObject;
     }
 
     /**
@@ -121,16 +105,6 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
         customObject = object;
     }
 
-    /**
-     * Get the custom (externally provided) object.
-     *
-     * @return the custom object
-     */
-    public Object getCustomObject() {
-        return customObject;
-    }
-
-
     /**
      * Set whether to hide exceptions in expression execution (default is false).
      * 
@@ -141,35 +115,36 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
     }
 
     /**
-     * Set value to return if an error occurs (default is false).
+     * Get value to return if an error occurs.
      * 
-     * @param what value to set
+     * @return value to return
      */
-    public void setReturnOnError(final U what) {
-        returnOnError = what;
+    @Nullable protected Object getReturnOnError() {
+        return returnOnError;
+    }
+    
+    /**
+     * Set value to return if an error occurs.
+     * 
+     * @param value value to return
+     */
+    protected void setReturnOnError(@Nullable final Object value) {
+        returnOnError = value;
     }
 
     /**
      * Evaluate the Spring expression on the provided input.
      *
-     * @param input input over which to evaluate the expression
-     * @return result of applying the expression to the provided input
+     * @param input input arguments
+     * 
+     * @return result of applying the expression to the provided inputs
      */
-    @SuppressWarnings("unchecked")
-    protected U evaluate(@Nullable final T input) {
-
-        // Try outside the try so as to preserve derived semantics
-        if (null != input && null != getInputType() && !getInputType().isInstance(input)) {
-            log.error("Input was type {} which is not an instance of {}",  input.getClass(), getInputType());
-            throw new ClassCastException("Input was type " + input.getClass() + " which is not an instance of "
-                    + getInputType());
-        }
+    @Nullable protected Object evaluate(@Nullable final Object... input) {
 
         try {
             final ExpressionParser parser = new SpelExpressionParser();
             final StandardEvaluationContext context = new StandardEvaluationContext();
             context.setVariable("custom", customObject);
-            context.setVariable("input", input);
             prepareContext(context, input);
             final Object output = parser.parseExpression(springExpression).getValue(context);
 
@@ -186,7 +161,7 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
                 return getOutputType().cast(output);
             }
             
-            return (U) output;
+            return output;
         } catch (final Exception e) {
             log.error("Error evaluating Spring expression", e);
             if (hideExceptions) {
@@ -197,13 +172,11 @@ public abstract class AbstractSpringExpressionEvaluator<T, U> {
     }
     
     /**
-     * Decorate the expression context with any additional content.
+     * Pre-process the script context before execution.
      * 
-     * @param context expression context
-     * @param input to predicate
+     * @param context the expression context
+     * @param input the inputs
      */
-     protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final T input) {
-        
-    }
+     protected abstract void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input);
 
 }
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionFunction.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionFunction.java
index 1f920b6..5c841fb 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionFunction.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionFunction.java
@@ -22,6 +22,10 @@ import java.util.function.Function;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.expression.EvaluationContext;
+
 import net.shibboleth.utilities.java.support.annotation.ParameterName;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 
@@ -33,9 +37,15 @@ import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
  * 
  * @since 5.4.0
  */
-public class SpringExpressionFunction<T,U> extends AbstractSpringExpressionEvaluator<T, U> 
+public class SpringExpressionFunction<T,U> extends AbstractSpringExpressionEvaluatorEx
             implements Function<T,U> {
-    
+
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionFunction.class);
+
+    /** The input type. */
+    @Nullable private Class<T> inputType;
+
     /**
      * Constructor.
      *
@@ -45,9 +55,60 @@ public class SpringExpressionFunction<T,U> extends AbstractSpringExpressionEvalu
         super(expression);
     }
 
+    /**
+     * Get the input type to be enforced.
+     *
+     * @return input type
+     */
+    @Nullable public  Class<T> getInputType() {
+        return inputType;
+    }
+
+    /**
+     * Set the input type to be enforced.
+     *
+     * @param type input type
+     */
+    public void setInputType(@Nullable final Class<T> type) {
+        inputType = type;
+    }
+
+    /**
+     * Set the output type to be enforced.
+     *
+     * @param type output type
+     */
+    public void setOutputType(@Nullable final Class<?> type) {
+        super.setOutputType(type);
+    }
+
+    /**
+     * Set value to return if an error occurs.
+     *
+     * @param value value to return
+     */
+    @Override public void setReturnOnError(@Nullable final Object value) {
+        super.setReturnOnError(value);
+    }
+
     /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
     public U apply(@Nullable final T input) {
-        return evaluate(input);
+        
+        // Try outside the try so as to preserve derived semantics
+        if (null != input && null != getInputType() && !getInputType().isInstance(input)) {
+            log.error("Input was type {} which is not an instance of {}",  input.getClass(), getInputType());
+            throw new ClassCastException("Input was type " + input.getClass() + " which is not an instance of "
+                    + getInputType());
+        }
+
+        return (U) evaluate(input);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input) {
+        context.setVariable("input", input[0]);
     }
     
 }
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionPredicate.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionPredicate.java
index ac82b85..6a8fdf4 100644
--- a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionPredicate.java
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionPredicate.java
@@ -20,9 +20,14 @@ package net.shibboleth.ext.spring.util;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.expression.EvaluationContext;
+
 import net.shibboleth.utilities.java.support.annotation.ParameterName;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.logic.Predicate;
+import net.shibboleth.utilities.java.support.logic.ScriptedPredicate;
 
 /**
  * Predicate whose condition is defined by an Spring EL expression.
@@ -31,9 +36,15 @@ import net.shibboleth.utilities.java.support.logic.Predicate;
  * 
  * @since 5.4.0
  */
-public class SpringExpressionPredicate<T> extends AbstractSpringExpressionEvaluator<T, Boolean> 
+public class SpringExpressionPredicate<T> extends AbstractSpringExpressionEvaluatorEx 
             implements Predicate<T> {
     
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(ScriptedPredicate.class);
+
+    /** Input type. */
+    @Nullable private Class<T> inputTypeClass;
+
     /**
      * Constructor.
      *
@@ -44,6 +55,28 @@ public class SpringExpressionPredicate<T> extends AbstractSpringExpressionEvalua
         setOutputType(Boolean.class);
     }
 
+    /**
+     * Get the input type to be enforced.
+     *
+     * @return input type
+     * 
+     * @since 6.1.0
+     */
+    @Nullable public Class<T> getInputType() {
+        return inputTypeClass;
+    }
+
+    /**
+     * Set the input type to be enforced.
+     *
+     * @param type input type
+     * 
+     * @since 6.1.0
+     */
+    public void setInputType(@Nullable final Class<T> type) {
+        inputTypeClass = type;
+    }
+    
     /**
      * Set value to return if an error occurs (default is false).
      * 
@@ -55,7 +88,19 @@ public class SpringExpressionPredicate<T> extends AbstractSpringExpressionEvalua
 
     /** {@inheritDoc} */
     public boolean test(@Nullable final T input) {
-        return evaluate(input);
+        if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
+            log.error("Input of type {} was not of type {}", input.getClass(), getInputType());
+            return (boolean) getReturnOnError();
+        }
+
+        final Object result = evaluate(input);
+        return (boolean) (result != null ? result : getReturnOnError());
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input) {
+        context.setVariable("input", input[0]);
     }
     
 }
\ No newline at end of file
diff --git a/src/test/java/net/shibboleth/ext/spring/util/SpringExpressionTest.java b/src/test/java/net/shibboleth/ext/spring/util/SpringExpressionTest.java
index c2e3c38..40a68ae 100644
--- a/src/test/java/net/shibboleth/ext/spring/util/SpringExpressionTest.java
+++ b/src/test/java/net/shibboleth/ext/spring/util/SpringExpressionTest.java
@@ -36,18 +36,20 @@ public class SpringExpressionTest {
     @Test public void testPredicates() {
         
         SpringExpressionPredicate<SpringExpressionTest> predicate = new SpringExpressionPredicate<>("#input.getValue99() == 99");
+        predicate.setInputType(SpringExpressionTest.class);
         
         Assert.assertTrue(predicate.test(this));
         
         predicate = new SpringExpressionPredicate<>("#input.getValue99() == #custom");
         predicate.setCustomObject(99);
         Assert.assertTrue(predicate.test(this));
-
     }
 
     @Test public void testFunction() {
         
         SpringExpressionFunction<Object,SpringExpressionTest> func = new SpringExpressionFunction<>("#input");
+        func.setOutputType(SpringExpressionTest.class);
+        
         Assert.assertNull(func.apply(null));
 
         Assert.assertEquals(func.apply(this), this);

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


More information about the commits mailing list