From 85385f6704923e167b98b206472bd2bfebf42167 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 3 Oct 2017 16:25:38 +0100 Subject: [PATCH] Add AcceptStringRegexValidator --- .../string/AcceptStringRegexValidator.java | 87 +++++++++++++++++++ .../uk/org/iay/incommon/mda/beans.xml | 3 + .../AcceptStringRegexValidatorTest.java | 47 ++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidator.java create mode 100644 src/test/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidatorTest.java diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidator.java new file mode 100644 index 0000000..b6850cb --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidator.java @@ -0,0 +1,87 @@ +/* + * Licensed 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 uk.org.iay.incommon.mda.validate.string; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.Nonnull; + +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.validate.BaseValidator; +import net.shibboleth.metadata.validate.Validator; +import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit; +import net.shibboleth.utilities.java.support.component.ComponentInitializationException; +import net.shibboleth.utilities.java.support.component.ComponentSupport; + +/** + * A Validator that accepts {@link String} values matching a regular expression. + * + * This validator returns {@link net.shibboleth.metadata.validate.Validator.Action#DONE} + * if the entire value is matched by the regular expression, thus terminating any validator sequence. + */ +public class AcceptStringRegexValidator extends BaseValidator implements Validator { + + /** Regular expression to be accepted by this validator. */ + @NonnullAfterInit + private String regex; + + /** Compiled regular expression to use in match operations. */ + @NonnullAfterInit + private Pattern pattern; + + /** + * Returns the regular expression. + * + * @return Returns the regular expression. + */ + @NonnullAfterInit + public String getRegex() { + return regex; + } + + /** + * Sets the regular expression to be accepted. + * + * @param r the regular expression to set. + */ + public void setRegex(@Nonnull final String r) { + ComponentSupport.ifDestroyedThrowDestroyedComponentException(this); + ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this); + + regex = r; + } + + @Override + public Action validate(@Nonnull final String e, @Nonnull final Item item, @Nonnull final String stageId) { + final Matcher matcher = pattern.matcher(e); + if (matcher.matches()) { + return Action.DONE; + } else { + return Action.CONTINUE; + } + } + + @Override + protected void doInitialize() throws ComponentInitializationException { + super.doInitialize(); + + if (getRegex() == null) { + throw new ComponentInitializationException("regular expression to be matched can not be null"); + } + + pattern = Pattern.compile(regex); + } +} diff --git a/src/main/resources/uk/org/iay/incommon/mda/beans.xml b/src/main/resources/uk/org/iay/incommon/mda/beans.xml index fe4bcc6..2cb8c13 100644 --- a/src/main/resources/uk/org/iay/incommon/mda/beans.xml +++ b/src/main/resources/uk/org/iay/incommon/mda/beans.xml @@ -38,6 +38,9 @@ + + diff --git a/src/test/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidatorTest.java b/src/test/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidatorTest.java new file mode 100644 index 0000000..92d0abd --- /dev/null +++ b/src/test/java/uk/org/iay/incommon/mda/validate/string/AcceptStringRegexValidatorTest.java @@ -0,0 +1,47 @@ + +package uk.org.iay.incommon.mda.validate.string; + +import java.util.List; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import net.shibboleth.metadata.ErrorStatus; +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.validate.Validator; +import net.shibboleth.metadata.validate.Validator.Action; +import uk.org.ukfederation.mda.MockItem; + +public class AcceptStringRegexValidatorTest { + + @Test + public void validateMatch() throws Exception { + final AcceptStringRegexValidator v = new AcceptStringRegexValidator(); + v.setId("comp"); + v.setRegex("a*b"); + v.initialize(); + + final Item item = new MockItem("content"); + final Validator.Action action = v.validate("aaaab", item, "stage"); + Assert.assertEquals(action, Action.DONE); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 0); + } + + @Test + public void validateMismatch() throws Exception { + final AcceptStringValueValidator v = new AcceptStringValueValidator(); + v.setId("comp"); + v.setValue("a*b"); + v.initialize(); + + final Item item = new MockItem("content"); + final Validator.Action action = v.validate("aaaaaaabc", item, "stage"); + Assert.assertEquals(action, Action.CONTINUE); + + final List errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 0); + } + +}