[java-opensaml COMMIT] in /trunk/opensaml-saml-impl/src: main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequ...
noreply at shibboleth.net
noreply at shibboleth.net
Tue Nov 18 16:59:12 EST 2014
Author: putmanb
Date: Tue Nov 18 16:59:12 2014
New Revision: 4150
URL: http://svn.shibboleth.net/view/java-opensaml?rev=4150&view=rev
Log:
Flesh out the regex request URL builder for dynamic metadata resolver use.
Added:
trunk/opensaml-saml-impl/src/test/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilderTest.java (with props)
Modified:
trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilder.java
Modified: trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilder.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilder.java?rev=4150&r1=4149&r2=4150&view=diff
==============================================================================
--- trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilder.java (original)
+++ trunk/opensaml-saml-impl/src/main/java/org/opensaml/saml/metadata/resolver/impl/RegexRequestURLBuilder.java Tue Nov 18 16:59:12 2014
@@ -17,8 +17,14 @@
package org.opensaml.saml.metadata.resolver.impl;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
import net.shibboleth.utilities.java.support.logic.Constraint;
@@ -27,27 +33,97 @@
import com.google.common.base.Function;
/**
- *
+ * Function which produces a URL by evaluating a supplied regular expression against the entity ID, and applying the
+ * result to a supplied replacement string.
+ *
+ * <p>
+ * The function uses standard Java regular expression components from the <code>java.util.regex</code> package.
+ * It is therefore helpful to have an understanding of the use of these Java classes.
+ * </p>
+ *
+ * <p>
+ * The rutime logic is effectively:
+ * <blockquote><pre>
+ * Pattern pattern = Pattern.compile(regex);
+ * Matcher matcher = pattern.matcher(entityID);
+ * if (matcher.matches()) {
+ * return matcher.replaceAll(replacement);
+ * else {
+ * return null;
+ * }
+ * </pre></blockquote>
+ * </p>
+ *
+ * <p>
+ * For supported regular expression syntax see {@link Pattern}. For details on the replacement operation,
+ * see {@link Matcher#replaceAll(String)}.
+ * </p>
+ *
+ * <p>
+ * It is expected that the typical use case is that the supplied replacement string will be a combination of
+ * literal text combined with back references to the regular expression match groups, e.g. $1, $2, etc.
+ * </p>
+ *
+ * <p>
+ * If the regular expression does not match the entity ID, or if there is an error in evaluating the
+ * regular expression, then null is returned.
+ * </p>
+ *
*/
public class RegexRequestURLBuilder implements Function<String, String> {
- private String pattern;
+ /** Logger. */
+ private final Logger log = LoggerFactory.getLogger(RegexRequestURLBuilder.class);
+ /** The compiled pattern. */
+ private Pattern pattern;
+
+ /** The replacement template. */
private String template;
+ /**
+ * Constructor.
+ *
+ * @param regex the regular expression against which to evaluate the entity ID
+ * @param replacement the the replacement template string.
+ */
public RegexRequestURLBuilder(@Nonnull @NotEmpty final String regex, @Nonnull @NotEmpty final String replacement) {
- pattern = Constraint.isNotNull(StringSupport.trimOrNull(regex), "Regex was null or empty");
- template = Constraint.isNotNull(StringSupport.trimOrNull(replacement), "Replacement template was null or empty");
+ String regexTemp = Constraint.isNotNull(StringSupport.trimOrNull(regex), "Regex was null or empty");
+ /*
+ // TODO: Defensively add start and end anchors if not supplied?
+ if (!regexTemp.startsWith("^")) {
+ regexTemp = "^" + regexTemp;
+ }
+ if (!regexTemp.endsWith("$")) {
+ regexTemp = regexTemp + "$";
+ }
+ */
+ pattern = Pattern.compile(regexTemp);
+
+ template = Constraint.isNotNull(StringSupport.trimOrNull(replacement),
+ "Replacement template was null or empty");
}
/** {@inheritDoc} */
@Nullable public String apply(@Nonnull String entityID) {
Constraint.isNotNull(entityID, "Entity ID was null");
- //TODO logging, exception handling
-
- //TODO not sure if this approach is fundamentally right
- return entityID.replaceAll(pattern, template);
+ try {
+ Matcher matcher = pattern.matcher(entityID);
+ if (matcher.matches()) {
+ String result = matcher.replaceAll(template);
+ log.debug("Regular expression '{}' matched successfully against entity ID '{}', returning '{}'",
+ pattern.pattern(), entityID, result);
[... 14 lines stripped ...]
More information about the commits
mailing list