Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor validator format string mechanism
iay committed Oct 10, 2017
1 parent 65bb512 commit b2824d7
Showing 5 changed files with 94 additions and 80 deletions.
@@ -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 <code>"value rejected: '%s'"</code>.
*/
@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);
}

}
@@ -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 <V> type of the object to be validated
*/
public class RejectAllValidator<V> extends BaseValidator implements Validator<V> {

/**
* 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");
}
public class RejectAllValidator<V> extends BaseLocalValidator implements Validator<V> {

@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;
}

@@ -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 <V> type of the object to be validated
*/
public class ValidatorSequence<V> extends BaseValidator implements Validator<V> {
public class ValidatorSequence<V> extends BaseLocalValidator implements Validator<V> {

/** The list of validators to apply. */
@Nonnull
@@ -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 <code>Validator</code>s 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
@@ -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 <code>Validator</code> that rejects {@link String} values matching a regular expression.
@@ -31,45 +29,11 @@
*/
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);
addErrorMessage(e, item, stageId);
return Action.DONE;
} else {
return Action.CONTINUE;

0 comments on commit b2824d7

Please sign in to comment.