View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines;
18  
19  import java.io.Serializable;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * <p>Perform email validations.</p>
25   * <p>
26   * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
27   * http://javascript.internet.com
28   * </p>
29   * <p>
30   * This implementation is not guaranteed to catch all possible errors in an email address.
31   * </p>.
32   *
33   * @version $Revision: 1723573 $
34   * @since Validator 1.4
35   */
36  public class EmailValidator implements Serializable {
37  
38      private static final long serialVersionUID = 1705927040799295880L;
39  
40      private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]";
41      private static final String VALID_CHARS = "(\\\\.)|[^\\s" + SPECIAL_CHARS + "]";
42      private static final String QUOTED_USER = "(\"(\\\\\"|[^\"])*\")";
43      private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
44  
45      private static final String EMAIL_REGEX = "^\\s*?(.+)@(.+?)\\s*$";
46      private static final String IP_DOMAIN_REGEX = "^\\[(.*)\\]$";
47      private static final String USER_REGEX = "^\\s*" + WORD + "(\\." + WORD + ")*$";
48  
49      private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
50      private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile(IP_DOMAIN_REGEX);
51      private static final Pattern USER_PATTERN = Pattern.compile(USER_REGEX);
52  
53      private static final int MAX_USERNAME_LEN = 64;
54  
55      private final boolean allowLocal;
56      private final boolean allowTld;
57  
58      /**
59       * Singleton instance of this class, which
60       *  doesn't consider local addresses as valid.
61       */
62      private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false, false);
63  
64      /**
65       * Singleton instance of this class, which
66       *  doesn't consider local addresses as valid.
67       */
68      private static final EmailValidator EMAIL_VALIDATOR_WITH_TLD = new EmailValidator(false, true);
69  
70      /**
71       * Singleton instance of this class, which does
72       *  consider local addresses valid.
73       */
74      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true, false);
75  
76  
77      /**
78       * Singleton instance of this class, which does
79       *  consider local addresses valid.
80       */
81      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD = new EmailValidator(true, true);
82  
83      /**
84       * Returns the Singleton instance of this validator.
85       *
86       * @return singleton instance of this validator.
87       */
88      public static EmailValidator getInstance() {
89          return EMAIL_VALIDATOR;
90      }
91  
92      /**
93       * Returns the Singleton instance of this validator,
94       *  with local validation as required.
95       *
96       * @param allowLocal Should local addresses be considered valid?
97       * @param allowTld Should TLDs be allowed?
98       * @return singleton instance of this validator
99       */
100     public static EmailValidator getInstance(boolean allowLocal, boolean allowTld) {
101         if(allowLocal) {
102             if (allowTld) {
103                 return EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD;
104             } else {
105                 return EMAIL_VALIDATOR_WITH_LOCAL;
106             }
107         } else {
108             if (allowTld) {
109                 return EMAIL_VALIDATOR_WITH_TLD;
110             } else {
111                 return EMAIL_VALIDATOR;
112             }
113         }
114     }
115 
116     /**
117      * Returns the Singleton instance of this validator,
118      *  with local validation as required.
119      *
120      * @param allowLocal Should local addresses be considered valid?
121      * @return singleton instance of this validator
122      */
123     public static EmailValidator getInstance(boolean allowLocal) {
124         return getInstance(allowLocal, false);
125     }
126 
127     /**
128      * Protected constructor for subclasses to use.
129      *
130      * @param allowLocal Should local addresses be considered valid?
131      * @param allowTld Should TLDs be allowed?
132      */
133     protected EmailValidator(boolean allowLocal, boolean allowTld) {
134         super();
135         this.allowLocal = allowLocal;
136         this.allowTld = allowTld;
137     }
138 
139     /**
140      * Protected constructor for subclasses to use.
141      *
142      * @param allowLocal Should local addresses be considered valid?
143      */
144     protected EmailValidator(boolean allowLocal) {
145         super();
146         this.allowLocal = allowLocal;
147         this.allowTld = false;
148     }
149 
150     /**
151      * <p>Checks if a field has a valid e-mail address.</p>
152      *
153      * @param email The value validation is being performed on.  A <code>null</code>
154      *              value is considered invalid.
155      * @return true if the email address is valid.
156      */
157     public boolean isValid(String email) {
158         if (email == null) {
159             return false;
160         }
161 
162         if (email.endsWith(".")) { // check this first - it's cheap!
163             return false;
164         }
165 
166         // Check the whole email address structure
167         Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
168         if (!emailMatcher.matches()) {
169             return false;
170         }
171 
172         if (!isValidUser(emailMatcher.group(1))) {
173             return false;
174         }
175 
176         if (!isValidDomain(emailMatcher.group(2))) {
177             return false;
178         }
179 
180         return true;
181     }
182 
183     /**
184      * Returns true if the domain component of an email address is valid.
185      *
186      * @param domain being validated, may be in IDN format
187      * @return true if the email address's domain is valid.
188      */
189     protected boolean isValidDomain(String domain) {
190         // see if domain is an IP address in brackets
191         Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
192 
193         if (ipDomainMatcher.matches()) {
194             InetAddressValidator inetAddressValidator =
195                     InetAddressValidator.getInstance();
196             return inetAddressValidator.isValid(ipDomainMatcher.group(1));
197         }
198         // Domain is symbolic name
199         DomainValidator domainValidator =
200                 DomainValidator.getInstance(allowLocal);
201         if (allowTld) {
202             return domainValidator.isValid(domain) || (!domain.startsWith(".") && domainValidator.isValidTld(domain));
203         } else {
204             return domainValidator.isValid(domain);
205         }
206     }
207 
208     /**
209      * Returns true if the user component of an email address is valid.
210      *
211      * @param user being validated
212      * @return true if the user name is valid.
213      */
214     protected boolean isValidUser(String user) {
215         
216         if (user == null || user.length() > MAX_USERNAME_LEN) {
217             return false;
218         }
219         
220         return USER_PATTERN.matcher(user).matches();
221     }
222 
223 }