diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/BaseLocalValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/BaseLocalValidator.java new file mode 100644 index 0000000..6fe7033 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/mda/validate/BaseLocalValidator.java @@ -0,0 +1,88 @@ +/* + * 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; + +import javax.annotation.Nonnull; + +import net.shibboleth.metadata.ErrorStatus; +import net.shibboleth.metadata.Item; +import net.shibboleth.metadata.validate.BaseValidator; +import net.shibboleth.utilities.java.support.component.ComponentSupport; +import net.shibboleth.utilities.java.support.logic.Constraint; + +/** + * An extended version of {@link BaseValidator} containing methods to be + * moved to the upstream implementation when possible. + */ +public abstract class BaseLocalValidator extends BaseValidator { + + /** + * Message format string. + * + * The generated message is formatted using this with the object being validated passed + * as an argument. + * + * Defaults to "value rejected: '%s'". + */ + @Nonnull + private String message = "value rejected: '%s'"; + + /** + * Constructor. + * + */ + public BaseLocalValidator() { + super(); + } + + /** + * 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"); + } + + /** + * Add an {@link ErrorStatus} to the given {@link Item}. + * + * The status message included in the {@link ErrorStatus} is generated + * by formatting the provided value with the {@link #message} field. + * + * @param extra extra value to include in the status metadata + * @param item {@link Item} to add the status metadata to + * @param stageId component identifier for the calling stage + */ + protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item item, + @Nonnull final String stageId) { + final String mess = String.format(getMessage(), extra); + addError(mess, item, stageId); + } + +} diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/RejectAllValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/RejectAllValidator.java index 716cd90..8556ec6 100644 --- a/src/main/java/uk/org/iay/incommon/mda/validate/RejectAllValidator.java +++ b/src/main/java/uk/org/iay/incommon/mda/validate/RejectAllValidator.java @@ -17,10 +17,7 @@ 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.component.ComponentSupport; -import net.shibboleth.utilities.java.support.logic.Constraint; /** * A {@link Validator} which rejects any value, returning @@ -32,45 +29,11 @@ * * @param type of the object to be validated */ -public class RejectAllValidator extends BaseValidator implements Validator { - - /** - * Message format string. - * - * The generated message is formatted using this with the object being validated passed - * as an argument. - * - * Defaults to "value rejected: '%s'". - */ - @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"); - } +public class RejectAllValidator extends BaseLocalValidator implements Validator { @Override public Action validate(@Nonnull final V e, @Nonnull final Item item, @Nonnull final String stageId) { - final String mess = String.format(message, e); - addError(mess, item, stageId); + addErrorMessage(e, item, stageId); return Action.DONE; } diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java b/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java index db944e3..d80a216 100644 --- a/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java +++ b/src/main/java/uk/org/iay/incommon/mda/validate/ValidatorSequence.java @@ -25,7 +25,6 @@ import net.shibboleth.metadata.Item; import net.shibboleth.metadata.pipeline.StageProcessingException; -import net.shibboleth.metadata.validate.BaseValidator; import net.shibboleth.metadata.validate.Validator; import net.shibboleth.utilities.java.support.component.ComponentInitializationException; import net.shibboleth.utilities.java.support.component.ComponentSupport; @@ -40,7 +39,7 @@ * * @param type of the object to be validated */ -public class ValidatorSequence extends BaseValidator implements Validator { +public class ValidatorSequence extends BaseLocalValidator implements Validator { /** The list of validators to apply. */ @Nonnull diff --git a/src/main/java/uk/org/iay/incommon/mda/validate/string/BaseStringRegexValidator.java b/src/main/java/uk/org/iay/incommon/mda/validate/string/BaseStringRegexValidator.java index 234e858..ea5c3c0 100644 --- a/src/main/java/uk/org/iay/incommon/mda/validate/string/BaseStringRegexValidator.java +++ b/src/main/java/uk/org/iay/incommon/mda/validate/string/BaseStringRegexValidator.java @@ -18,15 +18,15 @@ import javax.annotation.Nonnull; -import net.shibboleth.metadata.validate.BaseValidator; 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; +import uk.org.iay.incommon.mda.validate.BaseLocalValidator; /** * A base class for Validators that match {@link String} values against a regular expression. */ -public abstract class BaseStringRegexValidator extends BaseValidator { +public abstract class BaseStringRegexValidator extends BaseLocalValidator { /** Regular expression to be accepted by this validator. */ @NonnullAfterInit 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 index 40c872e..398f62f 100644 --- 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 @@ -20,8 +20,6 @@ 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 Validator that rejects {@link String} values matching a regular expression. @@ -31,45 +29,11 @@ */ public class RejectStringRegexValidator extends BaseStringRegexValidator implements Validator { - /** - * Message format string. - * - * The generated message is formatted using this with the object being validated passed - * as an argument. - * - * Defaults to "value rejected: '%s'". - */ - @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); + addErrorMessage(e, item, stageId); return Action.DONE; } else { return Action.CONTINUE;