X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fusermgt%2FRegexpNameValidator.java;fp=security%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fusermgt%2FRegexpNameValidator.java;h=fdc220f22d03a8bc24e20e1b1f85d7d7b1daf328;hb=162af365e45e54e5e8d656be276914df2005eaec;hp=0000000000000000000000000000000000000000;hpb=cf25a3646c663c5aadbd2de8c23840550b18125b;p=utils diff --git a/security/src/main/java/org/wamblee/usermgt/RegexpNameValidator.java b/security/src/main/java/org/wamblee/usermgt/RegexpNameValidator.java new file mode 100644 index 00000000..fdc220f2 --- /dev/null +++ b/security/src/main/java/org/wamblee/usermgt/RegexpNameValidator.java @@ -0,0 +1,83 @@ +/* + * Copyright 2005 the original author or authors. + * + * 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 org.wamblee.usermgt; + +import org.wamblee.usermgt.UserMgtException.Reason; + +/** + * Validation of names based on a regular expression. + */ +public class RegexpNameValidator implements NameValidator { + + /** + * Convenience pattern for an id. + */ + public static final String ID_PATTERN = "[a-zA-Z]+[a-zA-Z0-9]*"; + + /** + * Convenience pattern for a password consisting of at least 6 characters. + */ + public static final String PASSWORD_PATTERN = ".{6}.*"; + + /** + * Pattern to use. + */ + private String _pattern; + + /** + * Reason to use when validation fails. + */ + private Reason _reason; + + /** + * Message to report. + */ + private String _message; + + /** + * Validates a regular expression. + * @param aPattern Pattern that names must comply to. + * @param aReason Reason to report when validation fails. + * @param aMessage Message to report. + */ + public RegexpNameValidator(String aPattern, Reason aReason, String aMessage) { + _pattern = aPattern; + _reason = aReason; + _message = aMessage; + } + + /** + * Convenience constructor with all string parameters. Useful for configuration + * in Spring. + * @param aPattern Pattern to use. + * @param aReason Reason. + * @param aMessage Message. + */ + public RegexpNameValidator(String aPattern, String aReason, String aMessage) { + this(aPattern, Reason.valueOf(aReason), aMessage); + } + + /* (non-Javadoc) + * @see org.wamblee.usermgt.NameValidator#validate(java.lang.String) + */ + public void validate(String aName) throws UserMgtException { + if ( !aName.matches(_pattern)) { + throw new UserMgtException(_reason, _message); + } + } + +}