[java-identity-provider] 01/02: IDP-1789 Add option to SimpleAttributeDefinition to drop null or empty values

Rod Widdowson rdw at steadingsoftware.com
Tue Apr 13 15:55:59 UTC 2021


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

rdw pushed a commit to branch dev/IDP-1789
in repository java-identity-provider.

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

commit acafcfbcec1e1c33f3f15f904a8673869abc6dc9
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Tue Apr 13 16:19:34 2021 +0100

    IDP-1789 Add option to SimpleAttributeDefinition to drop null or empty values
    
    https://issues.shibboleth.net/jira/browse/IDP-1789
    
    Add code (&test) to impl.
---
 .../ad/impl/SimpleAttributeDefinition.java         | 34 ++++++++++++++++-
 .../resolver/ad/impl/SimpleAttributeTest.java      | 43 ++++++++++++++++++++++
 .../idp/saml/impl/testing/TestSources.java         |  2 +-
 3 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/idp-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeDefinition.java b/idp-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeDefinition.java
index f8287094e..150b6796e 100644
--- a/idp-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeDefinition.java
+++ b/idp-attribute-resolver-impl/src/main/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeDefinition.java
@@ -17,10 +17,15 @@
 
 package net.shibboleth.idp.attribute.resolver.ad.impl;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.ThreadSafe;
 
+import net.shibboleth.idp.attribute.EmptyAttributeValue;
 import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
 import net.shibboleth.idp.attribute.resolver.AbstractAttributeDefinition;
 import net.shibboleth.idp.attribute.resolver.PluginDependencySupport;
 import net.shibboleth.idp.attribute.resolver.ResolutionException;
@@ -36,6 +41,23 @@ import net.shibboleth.utilities.java.support.logic.Constraint;
 @ThreadSafe
 public class SimpleAttributeDefinition extends AbstractAttributeDefinition {
 
+    /** Do we strip {@link EmptyAttributeValue}s? */
+    private boolean stripNulls;
+
+    /** Set our "Null" strategy.
+     * @param what The value to set.
+     */
+    public void setStripNulls(final boolean what) {
+        stripNulls = what;
+    }
+
+   /** Do we strip nulls?
+     * @return Returns whether we strip Nulls.
+     */
+    public boolean isStripNulls() {
+        return stripNulls;
+    }
+
     /** {@inheritDoc} */
     @Override @Nonnull protected IdPAttribute doAttributeDefinitionResolve(
             @Nonnull final AttributeResolutionContext resolutionContext,
@@ -43,10 +65,18 @@ public class SimpleAttributeDefinition extends AbstractAttributeDefinition {
         Constraint.isNotNull(workContext, "AttributeResolverWorkContext cannot be null");
 
         final IdPAttribute result = new IdPAttribute(getId());
-        result.setValues(PluginDependencySupport.getMergedAttributeValues(workContext,
+        final List<IdPAttributeValue> values = PluginDependencySupport.getMergedAttributeValues(workContext,
                 getAttributeDependencies(), 
                 getDataConnectorDependencies(), 
-                getId()));
+                getId());
+        if (isStripNulls()) {
+            result.setValues(
+                    values.stream().
+                    filter(e -> (e!=null) && !(e instanceof EmptyAttributeValue)).
+                    collect(Collectors.toUnmodifiableList())); 
+        } else {
+            result.setValues(values);
+        }
 
         return result;
     }
diff --git a/idp-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeTest.java b/idp-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeTest.java
index 8d335e201..d22094f36 100644
--- a/idp-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeTest.java
+++ b/idp-attribute-resolver-impl/src/test/java/net/shibboleth/idp/attribute/resolver/ad/impl/SimpleAttributeTest.java
@@ -33,6 +33,8 @@ import net.shibboleth.idp.attribute.EmptyAttributeValue;
 import net.shibboleth.idp.attribute.EmptyAttributeValue.EmptyType;
 import net.shibboleth.idp.attribute.IdPAttribute;
 import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.StringAttributeValue;
+import net.shibboleth.idp.attribute.resolver.AbstractAttributeDefinition;
 import net.shibboleth.idp.attribute.resolver.AttributeDefinition;
 import net.shibboleth.idp.attribute.resolver.DataConnector;
 import net.shibboleth.idp.attribute.resolver.ResolutionException;
@@ -48,6 +50,7 @@ import net.shibboleth.utilities.java.support.collection.LazySet;
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
 
 /** test for {@link net.shibboleth.idp.attribute.resolver.ad.impl.SimpleAttributeDefinition}. */
+ at SuppressWarnings("javadoc")
 public class SimpleAttributeTest {
 
     /** The name. */
@@ -78,6 +81,46 @@ public class SimpleAttributeTest {
         assertTrue(result.getValues().isEmpty());
     }
 
+    @Test public void nulls() throws ResolutionException, ComponentInitializationException {
+        nulls(true);
+        nulls(false);
+    }
+
+    private void nulls(boolean strip) throws ComponentInitializationException, ResolutionException {
+        final AbstractAttributeDefinition sa = new AbstractAttributeDefinition() {
+
+            protected IdPAttribute doAttributeDefinitionResolve(AttributeResolutionContext resolutionContext,
+                    AttributeResolverWorkContext workContext) throws ResolutionException {
+                final IdPAttribute result = new IdPAttribute(TEST_ATTRIBUTE_NAME+"in");
+                result.setValues(List.of(EmptyAttributeValue.NULL, EmptyAttributeValue.ZERO_LENGTH, new StringAttributeValue("foo")));
+                return result;
+            }
+        };
+        sa.setId(TEST_ATTRIBUTE_NAME+"in");
+        sa.initialize();
+
+        final SimpleAttributeDefinition simple = new SimpleAttributeDefinition();
+        simple.setId(TEST_ATTRIBUTE_NAME);
+        simple.setAttributeDependencies(Set.of(TestSources.makeAttributeDefinitionDependency(TEST_ATTRIBUTE_NAME+"in")));
+        simple.setStripNulls(strip);
+        simple.initialize();
+
+        final AttributeResolverImpl resolver = AttributeResolverImplTest.newAttributeResolverImpl("foo", Set.of(simple, sa), Collections.emptySet());
+        resolver.initialize();
+
+        final AttributeResolutionContext context = new AttributeResolutionContext();
+        context.getSubcontext(AttributeResolverWorkContext.class, true);
+        resolver.resolveAttributes(context);
+
+        final IdPAttribute result = context.getResolvedIdPAttributes().get(TEST_ATTRIBUTE_NAME);
+        final int vals = result.getValues().size();
+        if (strip) {
+            assertEquals(vals, 1);
+        } else {
+            assertEquals(vals, 3);
+        }
+    }
+
     /**
      * Test when dependent on a data connector.
      * 
diff --git a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/impl/testing/TestSources.java b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/impl/testing/TestSources.java
index fc3304081..c981bbbe2 100644
--- a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/impl/testing/TestSources.java
+++ b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/impl/testing/TestSources.java
@@ -51,7 +51,7 @@ import net.shibboleth.utilities.java.support.component.ComponentInitializationEx
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
 
 /** Basic data sources for testing the attribute generators. */
- at SuppressWarnings("javadoc")
+ at SuppressWarnings({"javadoc", "removal"})
 public final class TestSources {
     /** The name we use in this test for the static connector. */
     public static final String STATIC_CONNECTOR_NAME = "staticCon";

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


More information about the commits mailing list