[java-identity-provider] branch master updated: IDP-1314 Migrate SpringExpressionContextLookupFunction to ue base class.

Rod Widdowson rdw at steadingsoftware.com
Tue Jul 10 11:54:02 EDT 2018


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

rdw pushed a commit to branch master
in repository java-identity-provider.

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

The following commit(s) were added to refs/heads/master by this push:
       new  2b65703   IDP-1314 Migrate SpringExpressionContextLookupFunction to ue base class.
2b65703 is described below

commit 2b657033058ad93fb003ed7569b93f9a6db96ce9
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Tue Jul 10 16:52:23 2018 +0100

    IDP-1314 Migrate SpringExpressionContextLookupFunction to ue base class.
    
    https://issues.shibboleth.net/jira/browse/IDP-1314
    
    In 3.4 we have to use an embedded object rather than inheritance
    so as to preserve the API.
---
 .../SpringExpressionContextLookupFunction.java     | 75 ++++++----------------
 .../SpringExpressionContextLookupFunctionTest.java | 38 +++++++++++
 2 files changed, 57 insertions(+), 56 deletions(-)

diff --git a/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunction.java b/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunction.java
index 77c6a06..78f4f13 100644
--- a/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunction.java
+++ b/idp-profile-api/src/main/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunction.java
@@ -20,47 +20,31 @@ package net.shibboleth.idp.profile.context.navigate;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import net.shibboleth.ext.spring.util.SpringExpressionFunction;
 import net.shibboleth.utilities.java.support.annotation.ParameterName;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
 import org.opensaml.messaging.context.BaseContext;
 import org.opensaml.messaging.context.navigate.ContextDataLookupFunction;
-import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.expression.EvaluationException;
-import org.springframework.expression.ExpressionParser;
-import org.springframework.expression.ParseException;
-import org.springframework.expression.spel.standard.SpelExpressionParser;
-import org.springframework.expression.spel.support.StandardEvaluationContext;
 
 /**
  * A {@link com.google.common.base.Function} over a {@link BaseContext}
  * which calls out to a Spring Expression.
  * 
  * @param <T> the specific type of context
+ * @since 3.3.0
  */
 public class SpringExpressionContextLookupFunction<T extends BaseContext>
     implements ContextDataLookupFunction<T, Object> {
 
-    /** Class logger. */
-    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionContextLookupFunction.class);
-
-    /** SpEL expression to evaluate. */
-    @Nullable private String springExpression;
-
-    /** What class we want the output to test against. */
-    @Nullable private Class outputClass;
-
-    /** What class we want the input to test against. */
-    @Nonnull private final Class<T> inputClass;
-
-    /** A custom object that can be injected into the expression. */
-    @Nullable private Object customObject;
-        
-    /** Whether to raise runtime exceptions if an expression fails. */
-    private boolean hideExceptions;
-
+    /**
+     * The object that does the work.
+     * In Future versions this should be replaced with inheritance.
+     */
+    @Deprecated
+    private final SpringExpressionFunction<T, Object> embeddedObject;
 
     /**
      * Constructor.
@@ -70,8 +54,12 @@ public class SpringExpressionContextLookupFunction<T extends BaseContext>
      */
     public SpringExpressionContextLookupFunction(@Nonnull @ParameterName(name="inClass") final Class<T> inClass,
             @Nonnull @NotEmpty @ParameterName(name="expression") final String expression) {
-        inputClass = Constraint.isNotNull(inClass, "Supplied inputClass cannot be null");
-        springExpression = Constraint.isNotNull(expression, "Supplied expression cannot be null");
+        embeddedObject = new SpringExpressionFunction<>(expression);
+        embeddedObject.setInputType(Constraint.isNotNull(inClass, "Supplied inputClass cannot be null"));
+        if(!BaseContext.class.isAssignableFrom(inClass)) {
+            LoggerFactory.getLogger(SpringExpressionContextLookupFunction.class).
+                warn("InClass {} is not derived from {}", inClass, BaseContext.class);
+        }
     }
 
     /**
@@ -85,7 +73,7 @@ public class SpringExpressionContextLookupFunction<T extends BaseContext>
             @Nonnull @NotEmpty @ParameterName(name="expression") final String expression, 
             @ParameterName(name="outputType") @Nullable final Class outputType) {
         this(inClass, expression);
-        outputClass = outputType;
+        embeddedObject.setOutputType(outputType);
     }
 
     /**
@@ -94,7 +82,7 @@ public class SpringExpressionContextLookupFunction<T extends BaseContext>
      * @return the custom object
      */
     @Nullable public Object getCustomObject() {
-        return customObject;
+        return embeddedObject.getCustomObject();
     }
 
     /**
@@ -103,7 +91,7 @@ public class SpringExpressionContextLookupFunction<T extends BaseContext>
      * @param object the custom object
      */
     @Nullable public void setCustomObject(final Object object) {
-        customObject = object;
+        embeddedObject.setCustomObject(object);
     }
 
     /**
@@ -112,37 +100,12 @@ public class SpringExpressionContextLookupFunction<T extends BaseContext>
      * @param flag flag to set
      */
     public void setHideExceptions(final boolean flag) {
-        hideExceptions = flag;
+        embeddedObject.setHideExceptions(flag);
     }
 
     /** {@inheritDoc} */
     @Override public Object apply(@Nullable final T context) {
-
-        if (null != context && !inputClass.isInstance(context)) {
-            throw new ClassCastException("Input was type " + context.getClass() + " which is not an instance of "
-                    + inputClass);
-        }
-
-        try {
-            final ExpressionParser parser = new SpelExpressionParser();
-            final StandardEvaluationContext eval = new StandardEvaluationContext();
-            eval.setVariable("custom", customObject);
-            eval.setVariable("input", context);
-            
-            final Object output = parser.parseExpression(springExpression).getValue(eval);
-            if (null != outputClass && null != output && !outputClass.isInstance(output)) {
-                log.error("Output of type {} was not of type {}", output.getClass(), outputClass);
-                return null;
-            }
-            return output;
-            
-        } catch (final ParseException|EvaluationException e) {
-            log.error("Error evaluating Spring expression", e);
-            if (hideExceptions) {
-                return null;
-            }
-            throw e;
-        }
+        return embeddedObject.apply(context);
     }
 
 }
\ No newline at end of file
diff --git a/idp-profile-api/src/test/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunctionTest.java b/idp-profile-api/src/test/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunctionTest.java
new file mode 100644
index 0000000..642fd8a
--- /dev/null
+++ b/idp-profile-api/src/test/java/net/shibboleth/idp/profile/context/navigate/SpringExpressionContextLookupFunctionTest.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.profile.context.navigate;
+
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Test for {@link SpringExpressionContextLookupFunction}.
+ */
+public class SpringExpressionContextLookupFunctionTest {
+
+    
+    @Test public void simpleTest() {
+        SpringExpressionContextLookupFunction func = new SpringExpressionContextLookupFunction(Integer.class, "#input", Integer.class);
+
+        func = new SpringExpressionContextLookupFunction(ProfileRequestContext.class, "99", Integer.class);
+        
+        Assert.assertEquals(func.apply(null), 99);
+        
+    }
+}

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


More information about the commits mailing list