[java-identity-provider] 03/27: Some refactoring and indirection.

Scott Cantor cantor.2 at osu.edu
Fri May 3 14:31:53 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=6b5b2dd8fa0aa210a51740987e72b4613f5b14cf

commit 6b5b2dd8fa0aa210a51740987e72b4613f5b14cf
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Apr 8 23:01:37 2019 -0400

    Some refactoring and indirection.
---
 .../impl/AttributeTranscoderRegistryImpl.java      | 116 ++++++++++++++-------
 .../impl/AttributeTranscoderRegistryTest.java      |   6 +-
 .../attribute/transcoding/impl/PairTranscoder.java |  88 ++++++++++++++++
 3 files changed, 169 insertions(+), 41 deletions(-)

diff --git a/idp-attribute-impl/src/main/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImpl.java b/idp-attribute-impl/src/main/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImpl.java
index 6216fab..24d8e1f 100644
--- a/idp-attribute-impl/src/main/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImpl.java
+++ b/idp-attribute-impl/src/main/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImpl.java
@@ -17,6 +17,7 @@
 
 package net.shibboleth.idp.attribute.transcoding.impl;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -32,6 +33,7 @@ import net.shibboleth.idp.attribute.IdPAttribute;
 import net.shibboleth.idp.attribute.transcoding.AttributeTranscoder;
 import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
 import net.shibboleth.utilities.java.support.logic.Constraint;
@@ -110,44 +112,7 @@ public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponen
             if (internalId != null && entry.getValue() != null && !entry.getValue().isEmpty()) {
 
                 for (final Properties props : Collections2.filter(entry.getValue(), Predicates.notNull())) {
-
-                    final Object type = props.get(PROP_TYPE);
-                    final Object transcoder = props.get(PROP_TRANSCODER);
-                    
-                    if (type instanceof Class && transcoder instanceof AttributeTranscoder) {
-                        final String targetName = ((AttributeTranscoder) transcoder).getEncodedName(props);
-                        if (targetName != null) {
-                            
-                            final Properties copy = new Properties();
-                            copy.putAll(props);
-                            
-                            // Install mapping back to IdPAttribute's name.
-                            copy.setProperty(PROP_ID, internalId);
-                            
-                            Multimap<Class<?>,Properties> rulesetsForIdPName = transcodingRegistry.get(internalId);
-                            if (rulesetsForIdPName == null) {
-                                rulesetsForIdPName = ArrayListMultimap.create();
-                                transcodingRegistry.put(internalId, rulesetsForIdPName);
-                            }
-                            
-                            rulesetsForIdPName.put((Class) type, copy);
-
-                            Multimap<Class<?>,Properties> rulesetsForEncodedName = transcodingRegistry.get(targetName);
-                            if (rulesetsForEncodedName == null) {
-                                rulesetsForEncodedName = ArrayListMultimap.create();
-                                transcodingRegistry.put(targetName, rulesetsForEncodedName);
-                            }
-                            
-                            rulesetsForEncodedName.put((Class) type, copy);
-                            
-                        } else {
-                            log.warn("Transcoding rule for {} into type {} did not produce an encoded name",
-                                    internalId, ((Class) type).getName());
-                        }
-                    } else {
-                        log.warn("Transcoding rule for {} missing or invalid {} or {} properties", internalId,
-                                PROP_TYPE, PROP_TRANSCODER);
-                    }
+                    addMapping(internalId, props);
                 }
             }
         }
@@ -191,4 +156,79 @@ public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponen
         return Collections.emptyList();
     }
     
+    /**
+     * Add a mapping between an {@link IdPAttribute} name and a set of transcoding rules.
+     * 
+     * <p>The rules MUST contain at least:</p>
+     * <ul>
+     *  <li>{@link #PROP_TYPE} - a source/target class for the transcoding rules</li>
+     *  <li>{@link #PROP_TRANSCODER} - an {@link AttributeTranscoder} instance supporting the type</li>
+     * </ul>
+     * 
+     * @param id name of the {@link IdPAttribute} to map to/from
+     * @param ruleset transcoding rules
+     */
+    private void addMapping(@Nonnull @NotEmpty final String id, @Nonnull final Properties ruleset) {
+        Object type = ruleset.get(PROP_TYPE);
+        Object transcoder = ruleset.get(PROP_TRANSCODER);
+        
+        if (type instanceof String) {
+            try {
+                type = Class.forName((String) type);
+            } catch (final ClassNotFoundException e) {
+                log.warn("Target class type {} not found in transcoding rule for {}", type, id);
+                return;
+            }
+        } else if (type == null) {
+            log.warn("Transcoding rule for {} missing {} property", id, PROP_TYPE);
+        }
+        
+        if (transcoder instanceof String) {
+            try {
+                transcoder = Class.forName((String) transcoder).getDeclaredConstructor().newInstance();
+            } catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
+                    | InvocationTargetException | NoSuchMethodException | SecurityException
+                    | ClassNotFoundException e) {
+                log.warn("Unable to create AttributeTranscoder of specified type {} in transcoding rule for {}",
+                        transcoder, id, e);
+                return;
+            }
+        } else if (!(transcoder instanceof AttributeTranscoder)) {
+            log.warn("Transcoding rule for {} missing {} property", id, PROP_TRANSCODER);
+        }
+
+        final String targetName = ((AttributeTranscoder) transcoder).getEncodedName(ruleset);
+        if (targetName != null) {
+
+            final Properties copy = new Properties();
+            copy.putAll(ruleset);
+
+            copy.put(PROP_TRANSCODER, transcoder);
+            copy.put(PROP_TYPE, type);
+            
+            // Install mapping back to IdPAttribute's name.
+            copy.setProperty(PROP_ID, id);
+            
+            Multimap<Class<?>,Properties> rulesetsForIdPName = transcodingRegistry.get(id);
+            if (rulesetsForIdPName == null) {
+                rulesetsForIdPName = ArrayListMultimap.create();
+                transcodingRegistry.put(id, rulesetsForIdPName);
+            }
+            
+            rulesetsForIdPName.put((Class) type, copy);
+
+            Multimap<Class<?>,Properties> rulesetsForEncodedName = transcodingRegistry.get(targetName);
+            if (rulesetsForEncodedName == null) {
+                rulesetsForEncodedName = ArrayListMultimap.create();
+                transcodingRegistry.put(targetName, rulesetsForEncodedName);
+            }
+            
+            rulesetsForEncodedName.put((Class) type, copy);
+            
+        } else {
+            log.warn("Transcoding rule for {} into type {} did not produce an encoded name",
+                    id, ((Class) type).getName());
+        }
+    }
+
 }
\ No newline at end of file
diff --git a/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryTest.java b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryTest.java
index 02b58a9..689c036 100644
--- a/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryTest.java
+++ b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryTest.java
@@ -70,8 +70,8 @@ public class AttributeTranscoderRegistryTest {
         ruleset1.setProperty("name", "bar");
         
         final Properties ruleset2 = new Properties();
-        ruleset2.put(AttributeTranscoderRegistry.PROP_TYPE, Pair.class);
-        ruleset2.put(AttributeTranscoderRegistry.PROP_TRANSCODER, transcoder);
+        ruleset2.put(AttributeTranscoderRegistry.PROP_TYPE, "net.shibboleth.utilities.java.support.collection.Pair");
+        ruleset2.put(AttributeTranscoderRegistry.PROP_TRANSCODER, "net.shibboleth.idp.attribute.transcoding.impl.PairTranscoder");
         ruleset2.setProperty("name", "baz");
         
         mappings.put("foo", Arrays.asList(ruleset1, ruleset2));
@@ -281,7 +281,7 @@ public class AttributeTranscoderRegistryTest {
         Assert.assertTrue(attributes.get(1).getValues().isEmpty());
     }
     
-    private static class PairTranscoder extends AbstractAttributeTranscoder<Pair<String,Object>> {
+    public class PairTranscoder extends AbstractAttributeTranscoder<Pair<String,Object>> {
 
         /** {@inheritDoc} */
         public String getEncodedName(Properties properties) {
diff --git a/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/PairTranscoder.java b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/PairTranscoder.java
new file mode 100644
index 0000000..9cb0384
--- /dev/null
+++ b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/PairTranscoder.java
@@ -0,0 +1,88 @@
+/*
+ * 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.attribute.transcoding.impl;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import org.opensaml.profile.context.ProfileRequestContext;
+
+import net.shibboleth.idp.attribute.AttributeDecodingException;
+import net.shibboleth.idp.attribute.AttributeEncodingException;
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.StringAttributeValue;
+import net.shibboleth.idp.attribute.transcoding.AbstractAttributeTranscoder;
+import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
+import net.shibboleth.utilities.java.support.collection.Pair;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ * Sample transcoder for tests. 
+ */
+public class PairTranscoder extends AbstractAttributeTranscoder<Pair<String,Object>> {
+
+    /** {@inheritDoc} */
+    public String getEncodedName(Properties properties) {
+        if (properties.containsKey("name")) {
+            return "{Pair}" + properties.getProperty("name");
+        } else {
+            return null;
+        }
+    }
+
+    /** {@inheritDoc} */
+    public Pair<String,Object> encode(ProfileRequestContext profileRequestContext, IdPAttribute attribute, Properties properties)
+            throws AttributeEncodingException {
+        
+        final String name = StringSupport.trimOrNull(properties.getProperty("name"));
+        if (name == null) {
+            throw new AttributeEncodingException("No name property");
+        }
+        
+        if (attribute.getValues().isEmpty() || !canEncodeValue(attribute, attribute.getValues().get(0))) {
+            return new Pair<>(name, null);
+        } else {
+            return new Pair<>(name, attribute.getValues().get(0).getValue());
+        }
+    }
+
+    /** {@inheritDoc} */
+    public IdPAttribute decode(ProfileRequestContext profileRequestContext, Pair<String,Object> input, Properties properties)
+            throws AttributeDecodingException {
+       
+        final String id = StringSupport.trimOrNull(properties.getProperty(AttributeTranscoderRegistry.PROP_ID));
+        if (id == null) {
+            throw new AttributeDecodingException("No id property");
+        }
+        
+        final IdPAttribute idattr = new IdPAttribute(id);
+        
+        if (input.getSecond() instanceof String) {
+            idattr.setValues(Collections.singletonList(StringAttributeValue.valueOf((String) input.getSecond())));
+        }
+        
+        return idattr;
+    }
+
+    /** {@inheritDoc} */
+    protected boolean canEncodeValue(IdPAttribute idpAttribute, IdPAttributeValue value) {
+        return value instanceof StringAttributeValue;
+    }
+    
+}
\ 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