[java-identity-provider] branch feature/IDP-1434 updated: Fork transcoder handling for object, class, and bean lookup cases.
Scott Cantor
cantor.2 at osu.edu
Wed May 8 16:18:19 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=40ad688d3b9322f0f2e0bdd8b25ecd3a6c7b5962
The following commit(s) were added to refs/heads/feature/IDP-1434 by this push:
new 40ad688 Fork transcoder handling for object, class, and bean lookup cases.
40ad688 is described below
commit 40ad688d3b9322f0f2e0bdd8b25ecd3a6c7b5962
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed May 8 16:18:16 2019 -0400
Fork transcoder handling for object, class, and bean lookup cases.
---
.../transcoding/AttributeTranscoderRegistry.java | 3 +
.../idp/attribute/transcoding/TranscodingRule.java | 51 ++++++++++++++
.../impl/AttributeTranscoderRegistryImpl.java | 82 +++++++++++++++-------
.../impl/AttributeTranscoderRegistryImplTest.java | 6 +-
4 files changed, 114 insertions(+), 28 deletions(-)
diff --git a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
index f061901..2594818 100644
--- a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
+++ b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
@@ -42,6 +42,9 @@ public interface AttributeTranscoderRegistry extends IdentifiedComponent {
/** Property name for accessing {@link AttributeTranscoder} object to use. */
@Nonnull @NotEmpty static final String PROP_TRANSCODER = "transcoder";
+
+ /** Property name for identifying an {@link AttributeTranscoder} class to build. */
+ @Nonnull @NotEmpty static final String PROP_TRANSCODER_CLASS = "transcoder_class";
/** Property name for accessing an activation condition object to apply. */
@Nonnull @NotEmpty static final String PROP_CONDITION = "activationCondition";
diff --git a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/TranscodingRule.java b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/TranscodingRule.java
index 5e3e036..b6d9134 100644
--- a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/TranscodingRule.java
+++ b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/TranscodingRule.java
@@ -17,12 +17,17 @@
package net.shibboleth.idp.attribute.transcoding;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import org.springframework.core.io.Resource;
+
import net.shibboleth.utilities.java.support.annotation.ParameterName;
import net.shibboleth.utilities.java.support.annotation.constraint.Live;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
@@ -57,6 +62,31 @@ public class TranscodingRule {
}
/**
+ * Constructor.
+ *
+ * @param properties a property set to initialize the map
+ *
+ * <p>The rule MUST contain at least:</p>
+ * <ul>
+ * <li>
+ * {@link AttributeTranscoderRegistry#PROP_ID} - internal attribute ID to map to/from
+ * </li>
+ * <li>
+ * {@link AttributeTranscoderRegistry#PROP_TRANSCODER} - {@link AttributeTranscoder} class name
+ * </li>
+ * </ul>
+ */
+ public TranscodingRule(@Nonnull @NonnullElements @ParameterName(name="properties") final Properties properties) {
+ rule = new HashMap<>(properties.size());
+ properties.forEach(
+ (k,v) -> {
+ if (k instanceof String && v != null) {
+ rule.put((String) k, v);
+ }
+ });
+ }
+
+ /**
* Access the underlying mapping rule.
*
* @return the map representing the rule
@@ -104,4 +134,25 @@ public class TranscodingRule {
}
}
+ /**
+ * Build a new rule from a property set resource.
+ *
+ * @param resource a property set to initialize the map
+ *
+ * @return the new rule
+ *
+ * @throws IOException if an error occurs
+ */
+ @Nonnull public static TranscodingRule fromResource(
+ @Nonnull @ParameterName(name="resource") final Resource resource) throws IOException {
+
+ final Properties props = new Properties();
+
+ try (final InputStream is = resource.getInputStream()) {
+ props.load(is);
+ }
+
+ return new TranscodingRule(props);
+ }
+
}
\ No newline at end of file
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 fffe8c6..24262d3 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
@@ -58,6 +58,9 @@ import com.google.common.collect.Multimap;
public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponent<AttributeTranscoderRegistry>
implements AttributeTranscoderRegistry {
+ /** Bean name for identifying an {@link AttributeTranscoder} object to install. */
+ @Nonnull @NotEmpty static final String PROP_TRANSCODER_BEAN = "transcoder_bean";
+
/** Class logger. */
@Nonnull private final Logger log = LoggerFactory.getLogger(AttributeTranscoderRegistryImpl.class);
@@ -125,10 +128,8 @@ public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponen
for (final TranscodingRule mapping : Collections2.filter(mappings, Predicates.notNull())) {
- final Object prop = mapping.getMap().get(PROP_ID);
- final String internalId = StringSupport.trimOrNull(prop instanceof String ? (String) prop : null);
+ final String internalId = StringSupport.trimOrNull(mapping.get(PROP_ID, String.class));
if (internalId != null) {
-
final Predicate activationCondition = buildActivationCondition(mapping.getMap());
if (activationCondition != null) {
mapping.getMap().put(PROP_CONDITION, activationCondition);
@@ -136,7 +137,13 @@ public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponen
mapping.getMap().remove(PROP_CONDITION);
}
- addMapping(internalId, mapping.getMap());
+ final AttributeTranscoder transcoder = buildAttributeTranscoder(mapping);
+ if (transcoder != null) {
+ mapping.getMap().put(PROP_TRANSCODER, transcoder);
+ addMapping(internalId, transcoder, mapping.getMap());
+ } else {
+ log.warn("Unable to locate or build an AttributeTranscoder in rule for {}", internalId);
+ }
}
}
}
@@ -195,40 +202,63 @@ public class AttributeTranscoderRegistryImpl extends AbstractServiceableComponen
}
/**
- * Add a mapping between an {@link IdPAttribute} name and a set of transcoding rules.
+ * Get the appropriate {@link AttributeTranscoder} to use.
*
- * <p>The rules MUST contain at least:</p>
- * <ul>
- * <li>{@link #PROP_TRANSCODER} - an {@link AttributeTranscoder} instance supporting the type</li>
- * </ul>
+ * @param rule transcoding rule
*
- * @param id name of the {@link IdPAttribute} to map to/from
- * @param ruleset transcoding rules
+ * @return a transcoder to install under the ruleset's {@link #PROP_TRANSCODER}
*/
- private void addMapping(@Nonnull @NotEmpty final String id, @Nonnull final Map<String,Object> ruleset) {
-
- Object transcoder = ruleset.get(PROP_TRANSCODER);
- if (transcoder instanceof String) {
+ @Nullable private AttributeTranscoder buildAttributeTranscoder(@Nonnull final TranscodingRule rule) {
+
+ AttributeTranscoder transcoder = rule.get(PROP_TRANSCODER, AttributeTranscoder.class);
+ if (transcoder != null) {
+ return transcoder;
+ }
+
+ final String type = rule.get(PROP_TRANSCODER_CLASS, String.class);
+ if (type != null) {
try {
- transcoder = Class.forName((String) transcoder).getDeclaredConstructor().newInstance();
- ((AttributeTranscoder) transcoder).initialize();
+ transcoder = (AttributeTranscoder) Class.forName(type).getDeclaredConstructor().newInstance();
+ transcoder.initialize();
+ return transcoder;
} catch (final InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException
| ClassNotFoundException | ComponentInitializationException e) {
- log.warn("Unable to create AttributeTranscoder of specified type {} in transcoding rule for {}",
- transcoder, id, e);
- return;
+ log.warn("Unable to create AttributeTranscoder of specified type {}", type, e);
+ return null;
}
- } else if (!(transcoder instanceof AttributeTranscoder)) {
- log.warn("Transcoding rule for {} missing {} property", id, PROP_TRANSCODER);
- return;
}
+
+ final String id = rule.get(PROP_TRANSCODER_BEAN, String.class);
+ if (id != null) {
+ try {
+ transcoder = getApplicationContext().getBean(id, AttributeTranscoder.class);
+ transcoder.initialize();
+ return transcoder;
+ } catch (final Exception e) {
+ log.warn("Unable to locate AttributeTranscoder bean named {}", id, e);
+ return null;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Add a mapping between an {@link IdPAttribute} name and a set of transcoding rules.
+ *
+ * @param id name of the {@link IdPAttribute} to map to/from
+ * @param transcoder the transcoder for this rule
+ * @param ruleset transcoding rules
+ */
+ private void addMapping(@Nonnull @NotEmpty final String id, @Nonnull final AttributeTranscoder transcoder,
+ @Nonnull final Map<String,Object> ruleset) {
+
final TranscodingRule copy = new TranscodingRule(ruleset);
- copy.getMap().put(PROP_TRANSCODER, transcoder);
- final Class<?> type = ((AttributeTranscoder) transcoder).getEncodedType();
- final String targetName = ((AttributeTranscoder) transcoder).getEncodedName(copy);
+ final Class<?> type = transcoder.getEncodedType();
+ final String targetName = transcoder.getEncodedName(copy);
if (targetName != null) {
log.debug("Attribute mapping: {} <-> {} via {}", id, targetName, transcoder.getClass().getSimpleName());
diff --git a/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImplTest.java b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImplTest.java
index d0741db..6bb0629 100644
--- a/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImplTest.java
+++ b/idp-attribute-impl/src/test/java/net/shibboleth/idp/attribute/transcoding/impl/AttributeTranscoderRegistryImplTest.java
@@ -68,7 +68,8 @@ public class AttributeTranscoderRegistryImplTest {
final Map<String,Object> ruleset2 = new HashMap<>();
ruleset2.put(AttributeTranscoderRegistry.PROP_ID, "foo");
- ruleset2.put(AttributeTranscoderRegistry.PROP_TRANSCODER, "net.shibboleth.idp.attribute.transcoding.impl.PairTranscoder");
+ ruleset2.put(AttributeTranscoderRegistry.PROP_TRANSCODER_CLASS,
+ "net.shibboleth.idp.attribute.transcoding.impl.PairTranscoder");
ruleset2.put("name", "baz");
final Map<String,Object> ruleset3 = new HashMap<>();
@@ -79,7 +80,8 @@ public class AttributeTranscoderRegistryImplTest {
final Map<String,Object> ruleset4 = new HashMap<>();
ruleset4.put(AttributeTranscoderRegistry.PROP_ID, "foo2");
- ruleset4.put(AttributeTranscoderRegistry.PROP_TRANSCODER, "net.shibboleth.idp.attribute.transcoding.impl.PairTranscoder");
+ ruleset4.put(AttributeTranscoderRegistry.PROP_TRANSCODER_CLASS,
+ "net.shibboleth.idp.attribute.transcoding.impl.PairTranscoder");
ruleset4.put("name", "baz");
registry.setTranscoderRegistry(Arrays.asList(
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list