7d31dbc619f55a1e43c943ebff089b277473eb9b
[utils] / security / impl / src / main / java / org / wamblee / security / authentication / RegexpNameValidator.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authentication;
17
18 import org.wamblee.security.authentication.UserMgtException.Reason;
19
20 /**
21  * Validation of names based on a regular expression.
22  * 
23  * @author Erik Brakkee
24  */
25 public class RegexpNameValidator implements NameValidator {
26     /**
27      * Convenience pattern for an id.
28      */
29     public static final String ID_PATTERN = "[a-zA-Z]+[a-zA-Z0-9]*";
30
31     /**
32      * Convenience pattern for a password consisting of at least 6 characters.
33      */
34     public static final String PASSWORD_PATTERN = ".{6}.*";
35
36     /**
37      * Pattern to use.
38      */
39     private String pattern;
40
41     /**
42      * Reason to use when validation fails.
43      */
44     private Reason reason;
45
46     /**
47      * Message to report.
48      */
49     private String message;
50
51     /**
52      * Validates a regular expression.
53      * 
54      * @param aPattern
55      *            Pattern that names must comply to.
56      * @param aReason
57      *            Reason to report when validation fails.
58      * @param aMessage
59      *            Message to report.
60      */
61     public RegexpNameValidator(String aPattern, Reason aReason, String aMessage) {
62         pattern = aPattern;
63         reason = aReason;
64         message = aMessage;
65     }
66
67     /**
68      * Convenience constructor with all string parameters. Useful for
69      * configuration in Spring.
70      * 
71      * @param aPattern
72      *            Pattern to use.
73      * @param aReason
74      *            Reason.
75      * @param aMessage
76      *            Message.
77      */
78     public RegexpNameValidator(String aPattern, String aReason, String aMessage) {
79         this(aPattern, Reason.valueOf(aReason), aMessage);
80     }
81
82     /*
83      * (non-Javadoc)
84      * 
85      * @see org.wamblee.usermgt.NameValidator#validate(java.lang.String)
86      */
87     @Override
88     public boolean validate(String aName) {
89         return aName.matches(pattern);
90     }
91 }