[java-identity-provider] 17/27: Refactoring.

Scott Cantor cantor.2 at osu.edu
Fri May 3 14:32:07 EDT 2019


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

scantor pushed a commit to branch feature/IDP-1434
in repository java-identity-provider.

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

commit f680983ca9c577ac8b6f1248ff2a0cd53bf61c5e
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Apr 25 19:08:38 2019 -0400

    Refactoring.
---
 .../impl/BaseAddAttributeStatementToAssertion.java | 60 +++++++++++++++++++++-
 .../impl/AddAttributeStatementToAssertion.java     | 35 +++----------
 .../impl/AddAttributeStatementToAssertion.java     | 35 +++----------
 3 files changed, 73 insertions(+), 57 deletions(-)

diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/profile/impl/BaseAddAttributeStatementToAssertion.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/profile/impl/BaseAddAttributeStatementToAssertion.java
index 3dd0b3a..7c386e9 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/profile/impl/BaseAddAttributeStatementToAssertion.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/profile/impl/BaseAddAttributeStatementToAssertion.java
@@ -17,13 +17,19 @@
 
 package net.shibboleth.idp.saml.profile.impl;
 
+import java.util.Collection;
+import java.util.Properties;
 import java.util.function.Function;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import net.shibboleth.idp.attribute.AttributeEncodingException;
+import net.shibboleth.idp.attribute.IdPAttribute;
 import net.shibboleth.idp.attribute.context.AttributeContext;
+import net.shibboleth.idp.attribute.transcoding.AttributeTranscoder;
 import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
+import net.shibboleth.idp.attribute.transcoding.TranscoderSupport;
 import net.shibboleth.idp.profile.AbstractProfileAction;
 import net.shibboleth.idp.profile.config.navigate.IdentifierGenerationStrategyLookupFunction;
 import net.shibboleth.idp.profile.context.RelyingPartyContext;
@@ -33,7 +39,9 @@ import org.opensaml.profile.action.ActionSupport;
 import org.opensaml.profile.action.EventIds;
 import org.opensaml.profile.context.ProfileRequestContext;
 
+import net.shibboleth.utilities.java.support.annotation.constraint.Live;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
@@ -52,11 +60,13 @@ import org.slf4j.LoggerFactory;
  * an {@link AttributeContext} returned from a
  * lookup strategy, by default located on the {@link RelyingPartyContext} beneath the profile request context.</p>
  * 
+ * @param <T> type of objects being encoded
+ * 
  * @event {@link EventIds#PROCEED_EVENT_ID}
  * @event {@link EventIds#INVALID_MSG_CTX}
  * @event {@link EventIds#INVALID_PROFILE_CTX}
  */
-public abstract class BaseAddAttributeStatementToAssertion extends AbstractProfileAction {
+public abstract class BaseAddAttributeStatementToAssertion<T> extends AbstractProfileAction {
 
     /** Class logger. */
     @Nonnull private final Logger log = LoggerFactory.getLogger(BaseAddAttributeStatementToAssertion.class);
@@ -286,4 +296,52 @@ public abstract class BaseAddAttributeStatementToAssertion extends AbstractProfi
         return true;
     }
 
+
+    /**
+     * Access the registry of transcoding rules to transform the input attribute into a target type.
+     * 
+     * @param registry  registry of transcoding rules
+     * @param profileRequestContext current profile request context
+     * @param attribute input attribute
+     * @param to target type
+     * @param results collection to add results to
+     * 
+     * @return number of results added
+     * 
+     * @throws AttributeEncodingException if a non-ignorable error occurs
+     */
+    protected int encodeAttribute(@Nonnull final AttributeTranscoderRegistry registry,
+            @Nonnull final ProfileRequestContext profileRequestContext, @Nonnull final IdPAttribute attribute,
+            @Nonnull final Class<T> to, @Nonnull @NonnullElements @Live final Collection<T> results)
+                    throws AttributeEncodingException {
+        
+        final Collection<Properties> transcodingRules = registry.getTranscodingProperties(attribute, to);
+        if (transcodingRules.isEmpty()) {
+            log.debug("{} Attribute {} does not have any transcoding rules, nothing to do", getLogPrefix(),
+                    attribute.getId());
+            return 0;
+        }
+        
+        int count = 0;
+        
+        for (final Properties rules : transcodingRules) {
+            try {
+                final AttributeTranscoder<T> transcoder = TranscoderSupport.getTranscoder(rules);
+                final T encodedAttribute = transcoder.encode(profileRequestContext, attribute, to, rules);
+                if (encodedAttribute != null) {
+                    results.add(encodedAttribute);
+                    count++;
+                }
+            } catch (final AttributeEncodingException e) {
+                if (isIgnoringUnencodableAttributes()) {
+                    log.debug("{} Unable to encode attribute {}", getLogPrefix(), attribute.getId(), e);
+                } else {
+                    throw e;
+                }
+            }
+        }
+        
+        return count;
+    }
+    
 }
\ No newline at end of file
diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml1/profile/impl/AddAttributeStatementToAssertion.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml1/profile/impl/AddAttributeStatementToAssertion.java
index 499a632..ed2fe75 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml1/profile/impl/AddAttributeStatementToAssertion.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml1/profile/impl/AddAttributeStatementToAssertion.java
@@ -19,7 +19,6 @@ package net.shibboleth.idp.saml.saml1.profile.impl;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Properties;
 import java.util.Set;
 import java.util.function.Function;
 
@@ -29,9 +28,7 @@ import javax.annotation.Nullable;
 import net.shibboleth.idp.attribute.AttributeEncoder;
 import net.shibboleth.idp.attribute.AttributeEncodingException;
 import net.shibboleth.idp.attribute.IdPAttribute;
-import net.shibboleth.idp.attribute.transcoding.AttributeTranscoder;
 import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
-import net.shibboleth.idp.attribute.transcoding.TranscoderSupport;
 import net.shibboleth.idp.profile.IdPEventIds;
 import net.shibboleth.idp.saml.attribute.encoding.SAML1AttributeEncoder;
 import net.shibboleth.idp.saml.profile.impl.BaseAddAttributeStatementToAssertion;
@@ -73,7 +70,7 @@ import com.google.common.collect.Collections2;
  * @event {@link EventIds#INVALID_MSG_CTX}
  * @event {@link IdPEventIds#UNABLE_ENCODE_ATTRIBUTE}
  */
-public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementToAssertion {
+public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementToAssertion<Attribute> {
 
     /** Class logger. */
     @Nonnull private final Logger log = LoggerFactory.getLogger(AddAttributeStatementToAssertion.class);
@@ -186,33 +183,15 @@ public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementT
 
         log.debug("{} Attempting to encode attribute {} as a SAML 1 Attribute", getLogPrefix(), attribute.getId());
         
-        final Collection<Properties> transcodingRules = registry.getTranscodingProperties(attribute, Attribute.class);
-        if (transcodingRules.isEmpty()) {
-            log.debug("{} Attribute {} does not have any transcoding rules, nothing to do", getLogPrefix(),
-                    attribute.getId());
-            // TODO: add return once legacy code is removed
-        }
-        
         boolean added = false;
-        
-        for (final Properties rules : transcodingRules) {
-            try {
-                final AttributeTranscoder<Attribute> transcoder = TranscoderSupport.getTranscoder(rules);
-                final Attribute encodedAttribute =
-                        transcoder.encode(profileRequestContext, attribute, Attribute.class, rules);
-                if (encodedAttribute != null) {
-                    results.add(encodedAttribute);
-                }
-            } catch (final AttributeEncodingException e) {
-                if (isIgnoringUnencodableAttributes()) {
-                    log.debug("{} Unable to encode attribute {} as SAML 1 attribute", getLogPrefix(),
-                            attribute.getId(), e);
-                } else {
-                    throw e;
-                }
-            }
+
+        // Uses the new registry.
+        if (super.encodeAttribute(registry, profileRequestContext, attribute, Attribute.class, results) > 0) {
+            added = true;
         }
         
+        // TODO: migrate legacy code into an alternate registry call via resolver
+        
         final Set<AttributeEncoder<?>> encoders = attribute.getEncoders();
         if (encoders.isEmpty()) {
             log.debug("{} Attribute {} does not have any encoders, nothing to do", getLogPrefix(), attribute.getId());
diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml2/profile/impl/AddAttributeStatementToAssertion.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml2/profile/impl/AddAttributeStatementToAssertion.java
index 6ecd63c..72b94bd 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml2/profile/impl/AddAttributeStatementToAssertion.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml2/profile/impl/AddAttributeStatementToAssertion.java
@@ -19,7 +19,6 @@ package net.shibboleth.idp.saml.saml2.profile.impl;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Properties;
 import java.util.Set;
 import java.util.function.Function;
 
@@ -29,9 +28,7 @@ import javax.annotation.Nullable;
 import net.shibboleth.idp.attribute.AttributeEncoder;
 import net.shibboleth.idp.attribute.AttributeEncodingException;
 import net.shibboleth.idp.attribute.IdPAttribute;
-import net.shibboleth.idp.attribute.transcoding.AttributeTranscoder;
 import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
-import net.shibboleth.idp.attribute.transcoding.TranscoderSupport;
 import net.shibboleth.idp.profile.IdPEventIds;
 import net.shibboleth.idp.saml.attribute.encoding.SAML2AttributeEncoder;
 import net.shibboleth.idp.saml.profile.impl.BaseAddAttributeStatementToAssertion;
@@ -74,7 +71,7 @@ import com.google.common.collect.Collections2;
  * @event {@link EventIds#INVALID_MSG_CTX}
  * @event {@link IdPEventIds#UNABLE_ENCODE_ATTRIBUTE}
  */
-public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementToAssertion {
+public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementToAssertion<Attribute> {
 
     /** Class logger. */
     @Nonnull private final Logger log = LoggerFactory.getLogger(AddAttributeStatementToAssertion.class);
@@ -188,33 +185,15 @@ public class AddAttributeStatementToAssertion extends BaseAddAttributeStatementT
 
         log.debug("{} Attempting to encode attribute {} as a SAML 2 Attribute", getLogPrefix(), attribute.getId());
         
-        final Collection<Properties> transcodingRules = registry.getTranscodingProperties(attribute, Attribute.class);
-        if (transcodingRules.isEmpty()) {
-            log.debug("{} Attribute {} does not have any transcoding rules, nothing to do", getLogPrefix(),
-                    attribute.getId());
-            // TODO: add return once legacy code is removed
-        }
-        
         boolean added = false;
-        
-        for (final Properties rules : transcodingRules) {
-            try {
-                final AttributeTranscoder<Attribute> transcoder = TranscoderSupport.getTranscoder(rules);
-                final Attribute encodedAttribute =
-                        transcoder.encode(profileRequestContext, attribute, Attribute.class, rules);
-                if (encodedAttribute != null) {
-                    results.add(encodedAttribute);
-                }
-            } catch (final AttributeEncodingException e) {
-                if (isIgnoringUnencodableAttributes()) {
-                    log.debug("{} Unable to encode attribute {} as SAML 2 attribute", getLogPrefix(),
-                            attribute.getId(), e);
-                } else {
-                    throw e;
-                }
-            }
+
+        // Uses the new registry.
+        if (super.encodeAttribute(registry, profileRequestContext, attribute, Attribute.class, results) > 0) {
+            added = true;
         }
         
+        // TODO: migrate legacy code into an alternate registry call via resolver
+        
         final Set<AttributeEncoder<?>> encoders = attribute.getEncoders();
         if (encoders.isEmpty()) {
             log.debug("{} Attribute {} does not have any encoders, nothing to do", getLogPrefix(), attribute.getId());

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


More information about the commits mailing list