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

Scott Cantor cantor.2 at osu.edu
Thu Feb 25 15:14:29 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=5851de7900ec165dfa0ac9a7095f883602b478d9

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

commit 5851de7900ec165dfa0ac9a7095f883602b478d9
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Feb 25 10:14:27 2021 -0500

    JSPT-109 - Add scripted variants of additional functional interfaces
    
    https://issues.shibboleth.net/jira/browse/JSPT-109
    
    Add Spring Expression Consumer and Bi-variants.
---
 .../spring/util/SpringExpressionBiConsumer.java    | 107 +++++++++++++++++
 .../spring/util/SpringExpressionBiFunction.java    | 127 +++++++++++++++++++++
 .../spring/util/SpringExpressionBiPredicate.java   | 119 +++++++++++++++++++
 .../ext/spring/util/SpringExpressionConsumer.java  |  94 +++++++++++++++
 4 files changed, 447 insertions(+)

diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiConsumer.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiConsumer.java
new file mode 100644
index 0000000..dd76714
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiConsumer.java
@@ -0,0 +1,107 @@
+/*
+ * 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.ext.spring.util;
+
+import java.util.function.BiConsumer;
+
+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.collection.Pair;
+
+/**
+ * Predicate whose condition is defined by an Spring EL expression.
+ * 
+ * @param <T> first input type
+ * @param <U> second input type
+ * 
+ * @since 6.1.0
+ */
+public class SpringExpressionBiConsumer<T,U> extends AbstractSpringExpressionEvaluatorEx 
+            implements BiConsumer<T,U> {
+    
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionBiConsumer.class);
+
+    /** Input type 1. */
+    @Nullable private Class<T> inputTypeClass1;
+
+    /** Input type 2. */
+    @Nullable private Class<U> inputTypeClass2;
+
+    /**
+     * Constructor.
+     *
+     * @param expression the expression to evaluate
+     */
+    public SpringExpressionBiConsumer(@Nonnull @NotEmpty @ParameterName(name="expression") final String expression) {
+        super(expression);
+    }
+
+    /**
+     * Get the input type to be enforced.
+     *
+     * @return input type
+     */
+    @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+        return new Pair<>(inputTypeClass1, inputTypeClass2);
+    }
+
+    /**
+     * Set the input type to be enforced.
+     *
+     * @param type1 first input type
+     * @param type2 second input type
+     */
+    public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+        inputTypeClass1 = type1;
+        inputTypeClass2 = type2;
+    }
+    
+    /** {@inheritDoc} */
+    public void accept(@Nullable final T first, @Nullable final U second) {
+        final Pair<Class<T>,Class<U>> types = getInputTypes();
+        if (null != types) {
+            if (null != first && !types.getFirst().isInstance(first)) {
+                log.error("Input of type {} was not of type {}", first.getClass(), types.getFirst());
+                return;
+            }
+            if (null != second && !types.getSecond().isInstance(second)) {
+                log.error("Input of type {} was not of type {}", second.getClass(),
+                        types.getSecond());
+                return;
+            }
+        }
+
+        evaluate(first, second);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input) {
+        context.setVariable("input1", input[0]);
+        context.setVariable("input2", input[1]);
+    }
+    
+}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiFunction.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiFunction.java
new file mode 100644
index 0000000..d95f0ea
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiFunction.java
@@ -0,0 +1,127 @@
+/*
+ * 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.ext.spring.util;
+
+import java.util.function.BiFunction;
+
+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.collection.Pair;
+
+/**
+ * Predicate whose condition is defined by an Spring EL expression.
+ * 
+ * @param <T> first input type
+ * @param <U> second input type
+ * @param <V> return type
+ * 
+ * @since 6.1.0
+ */
+public class SpringExpressionBiFunction<T,U,V> extends AbstractSpringExpressionEvaluatorEx 
+            implements BiFunction<T,U,V> {
+    
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionBiFunction.class);
+
+    /** Input type 1. */
+    @Nullable private Class<T> inputTypeClass1;
+
+    /** Input type 2. */
+    @Nullable private Class<U> inputTypeClass2;
+
+    /**
+     * Constructor.
+     *
+     * @param expression the expression to evaluate
+     */
+    public SpringExpressionBiFunction(@Nonnull @NotEmpty @ParameterName(name="expression") final String expression) {
+        super(expression);
+    }
+
+    /**
+     * Get the input type to be enforced.
+     *
+     * @return input type
+     */
+    @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+        return new Pair<>(inputTypeClass1, inputTypeClass2);
+    }
+
+    /**
+     * Set the input type to be enforced.
+     *
+     * @param type1 first input type
+     * @param type2 second input type
+     */
+    public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+        inputTypeClass1 = type1;
+        inputTypeClass2 = type2;
+    }
+
+    /**
+     * Set the output type to be enforced.
+     *
+     * @param type output type
+     */
+    @Override 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")
+    @Nullable public V apply(@Nullable final T first, @Nullable final U second) {
+        final Pair<Class<T>,Class<U>> types = getInputTypes();
+        if (null != types) {
+            if (null != first && !types.getFirst().isInstance(first)) {
+                log.error("Input of type {} was not of type {}", first.getClass(), types.getFirst());
+                return (V) getReturnOnError();
+            }
+            if (null != second && !types.getSecond().isInstance(second)) {
+                log.error("Input of type {} was not of type {}", second.getClass(),
+                        types.getSecond());
+                return (V) getReturnOnError();
+            }
+        }
+
+        return (V) evaluate(first, second);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input) {
+        context.setVariable("input1", input[0]);
+        context.setVariable("input2", input[1]);
+    }
+    
+}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiPredicate.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiPredicate.java
new file mode 100644
index 0000000..4731b6d
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionBiPredicate.java
@@ -0,0 +1,119 @@
+/*
+ * 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.ext.spring.util;
+
+import java.util.function.BiPredicate;
+
+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.collection.Pair;
+
+/**
+ * Predicate whose condition is defined by an Spring EL expression.
+ * 
+ * @param <T> first input type
+ * @param <U> second input type
+ * 
+ * @since 6.1.0
+ */
+public class SpringExpressionBiPredicate<T,U> extends AbstractSpringExpressionEvaluatorEx 
+            implements BiPredicate<T,U> {
+    
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionBiPredicate.class);
+
+    /** Input type 1. */
+    @Nullable private Class<T> inputTypeClass1;
+
+    /** Input type 2. */
+    @Nullable private Class<U> inputTypeClass2;
+
+    /**
+     * Constructor.
+     *
+     * @param expression the expression to evaluate
+     */
+    public SpringExpressionBiPredicate(@Nonnull @NotEmpty @ParameterName(name="expression") final String expression) {
+        super(expression);
+        setOutputType(Boolean.class);
+        setReturnOnError(false);
+    }
+
+    /**
+     * Get the input type to be enforced.
+     *
+     * @return input type
+     */
+    @Nullable public Pair<Class<T>,Class<U>> getInputTypes() {
+        return new Pair<>(inputTypeClass1, inputTypeClass2);
+    }
+
+    /**
+     * Set the input type to be enforced.
+     *
+     * @param type1 first input type
+     * @param type2 second input type
+     */
+    public void setInputTypes(@Nullable final Class<T> type1, @Nullable final Class<U> type2) {
+        inputTypeClass1 = type1;
+        inputTypeClass2 = type2;
+    }
+    
+    /**
+     * Set value to return if an error occurs.
+     * 
+     * @param flag value to return
+     */
+    public void setReturnOnError(final boolean flag) {
+        setReturnOnError(Boolean.valueOf(flag));
+    }
+    
+    /** {@inheritDoc} */
+    public boolean test(@Nullable final T first, @Nullable final U second) {
+        final Pair<Class<T>,Class<U>> types = getInputTypes();
+        if (null != types) {
+            if (null != first && !types.getFirst().isInstance(first)) {
+                log.error("Input of type {} was not of type {}", first.getClass(), types.getFirst());
+                return (boolean) getReturnOnError();
+            }
+            if (null != second && !types.getSecond().isInstance(second)) {
+                log.error("Input of type {} was not of type {}", second.getClass(),
+                        types.getSecond());
+                return (boolean) getReturnOnError();
+            }
+        }
+
+        final Object result = evaluate(first, second);
+        return (boolean) (result != null ? result : getReturnOnError());
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void prepareContext(@Nonnull final EvaluationContext context, @Nullable final Object... input) {
+        context.setVariable("input1", input[0]);
+        context.setVariable("input2", input[1]);
+    }
+    
+}
\ No newline at end of file
diff --git a/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionConsumer.java b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionConsumer.java
new file mode 100644
index 0000000..ce4695c
--- /dev/null
+++ b/src/main/java/net/shibboleth/ext/spring/util/SpringExpressionConsumer.java
@@ -0,0 +1,94 @@
+/*
+ * 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.ext.spring.util;
+
+import java.util.function.Consumer;
+
+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;
+
+/**
+ * Predicate whose condition is defined by an Spring EL expression.
+ * 
+ * @param <T> type of input
+ * 
+ * @since 6.1.0
+ */
+public class SpringExpressionConsumer<T> extends AbstractSpringExpressionEvaluatorEx 
+            implements Consumer<T> {
+    
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SpringExpressionConsumer.class);
+
+    /** Input type. */
+    @Nullable private Class<T> inputTypeClass;
+
+    /**
+     * Constructor.
+     *
+     * @param expression the expression to evaluate
+     */
+    public SpringExpressionConsumer(@Nonnull @NotEmpty @ParameterName(name="expression") final String expression) {
+        super(expression);
+    }
+
+    /**
+     * 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;
+    }
+    
+    /** {@inheritDoc} */
+    public void accept(@Nullable final T input) {
+        if (null != getInputType() && null != input && !getInputType().isInstance(input)) {
+            log.error("Input of type {} was not of type {}", input.getClass(), getInputType());
+        } else {
+            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

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


More information about the commits mailing list