Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactor validator format string mechanism
Showing
5 changed files
with
94 additions
and
80 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/uk/org/iay/incommon/mda/validate/BaseLocalValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters