From 3026ba9041261563fe101cadc518b4eed1dd2954 Mon Sep 17 00:00:00 2001 From: Ian Young <ian@iay.org.uk> Date: Mon, 9 Oct 2017 17:57:04 +0100 Subject: [PATCH] Add RejectStringRegexValidator --- .../string/RejectStringRegexValidator.java | 78 +++++++++++++++++++ .../uk/org/iay/incommon/mda/beans.xml | 3 + .../RejectStringRegexValidatorTest.java | 70 +++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/main/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidator.java create mode 100644 src/test/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidatorTest.java diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidator.java new file mode 100644 index 0000000..40c872e --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidator.java @@ -0,0 +1,78 @@ +/* + * 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 javax.annotation.Nonnull; + +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.validate.Validator; +import net.shibboleth.utilities.java.support.component.ComponentSupport; +import net.shibboleth.utilities.java.support.logic.Constraint; + +/** + * A <code>Validator</code> that rejects {@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 RejectStringRegexValidator extends BaseStringRegexValidator implements Validator<String> { + + /** + * Message format string. + * + * The generated message is formatted using this with the object being validated passed + * as an argument. + * + * Defaults to <code>"value rejected: '%s'"</code>. + */ + @Nonnull + private String message = "value rejected: '%s'"; + + /** + * Returns the message format string. + * + * @return the message format string + */ + @Nonnull + public String getMessage() { + return message; + } + + /** + * Set the message format string. + * + * @param newMessage the new message format string + */ + public void setMessage(@Nonnull final String newMessage) { + ComponentSupport.ifDestroyedThrowDestroyedComponentException(this); + ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this); + + message = Constraint.isNotNull(newMessage, "message format string may not be null"); + } + + @Override + public Action validate(@Nonnull final String e, @Nonnull final Item<?> item, @Nonnull final String stageId) { + final Matcher matcher = getPattern().matcher(e); + if (matcher.matches()) { + final String mess = String.format(message, e); + addError(mess, item, stageId); + return Action.DONE; + } else { + return Action.CONTINUE; + } + } +} 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 2cb8c13..2ddb356 100644 --- a/src/main/resources/uk/org/iay/incommon/mda/beans.xml +++ b/src/main/resources/uk/org/iay/incommon/mda/beans.xml @@ -47,4 +47,7 @@ <bean id="inc.RejectAllValidator" abstract="true" class="uk.org.iay.incommon.mda.validate.RejectAllValidator"/> + <bean id="inc.RejectStringRegexValidator" abstract="true" + class="uk.org.iay.incommon.mda.validate.string.RejectStringRegexValidator"/> + </beans> diff --git a/src/test/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidatorTest.java b/src/test/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidatorTest.java new file mode 100644 index 0000000..f93e09e --- /dev/null +++ b/src/test/java/uk/org/iay/incommon/mda/validate/string/RejectStringRegexValidatorTest.java @@ -0,0 +1,70 @@ + +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 RejectStringRegexValidatorTest { + + @Test + public void validateMatch() throws Exception { + final RejectStringRegexValidator v = new RejectStringRegexValidator(); + v.setId("comp"); + v.setRegex("a*b"); + v.setMessage("the lazy fox jumped over the %s"); + v.initialize(); + + final Item<String> item = new MockItem("content"); + final Validator.Action action = v.validate("aaaab", item, "stage"); + Assert.assertEquals(action, Action.DONE); + + final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 1); + final ErrorStatus err = errs.get(0); + Assert.assertTrue(err.getStatusMessage().contains("the lazy fox")); + Assert.assertTrue(err.getStatusMessage().contains("aaaab")); + } + + @Test + public void whitespaceMatch() throws Exception { + final RejectStringRegexValidator v = new RejectStringRegexValidator(); + v.setId("comp"); + v.setRegex(".*\\s.*"); + v.setMessage("the lazy fox jumped over the '%s'"); + v.initialize(); + + final Item<String> item = new MockItem("content"); + final Validator.Action action = v.validate(" example.org", item, "stage"); + Assert.assertEquals(action, Action.DONE); + + final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 1); + final ErrorStatus err = errs.get(0); + Assert.assertTrue(err.getStatusMessage().contains("the lazy fox")); + Assert.assertTrue(err.getStatusMessage().contains("' example.org'")); + } + + @Test + public void validateMismatch() throws Exception { + final AcceptStringValueValidator v = new AcceptStringValueValidator(); + v.setId("comp"); + v.setValue("a*b"); + v.initialize(); + + final Item<String> item = new MockItem("content"); + final Validator.Action action = v.validate("aaaaaaabc", item, "stage"); + Assert.assertEquals(action, Action.CONTINUE); + + final List<ErrorStatus> errs = item.getItemMetadata().get(ErrorStatus.class); + Assert.assertEquals(errs.size(), 0); + } + +}