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: 1715080 $
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 final boolean allowLocal;
54      private final boolean allowTld;
55  
56      /**
57       * Singleton instance of this class, which
58       *  doesn't consider local addresses as valid.
59       */
60      private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false, false);
61  
62      /**
63       * Singleton instance of this class, which
64       *  doesn't consider local addresses as valid.
65       */
66      private static final EmailValidator EMAIL_VALIDATOR_WITH_TLD = new EmailValidator(false, true);
67  
68      /**
69       * Singleton instance of this class, which does
70       *  consider local addresses valid.
71       */
72      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true, false);
73  
74  
75      /**
76       * Singleton instance of this class, which does
77       *  consider local addresses valid.
78       */
79      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD = new EmailValidator(true, true);
80  
81      /**
82       * Returns the Singleton instance of this validator.
83       *
84       * @return singleton instance of this validator.
85       */
86      public static EmailValidator getInstance() {
87          return EMAIL_VALIDATOR;
88      }
89  
90      /**
91       * Returns the Singleton instance of this validator,
92       *  with local validation as required.
93       *
94       * @param allowLocal Should local addresses be considered valid?
95       * @return singleton instance of this validator
96       */
97      public static EmailValidator getInstance(boolean allowLocal, boolean allowTld) {
98          if(allowLocal) {
99              if (allowTld) {
100                 return EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD;
101             } else {
102                 return EMAIL_VALIDATOR_WITH_LOCAL;
103             }
104         } else {
105             if (allowTld) {
106                 return EMAIL_VALIDATOR_WITH_TLD;
107             } else {
108                 return EMAIL_VALIDATOR;
109             }
110         }
111     }
112 
113     /**
114      * Returns the Singleton instance of this validator,
115      *  with local validation as required.
116      *
117      * @param allowLocal Should local addresses be considered valid?
118      * @return singleton instance of this validator
119      */
120     public static EmailValidator getInstance(boolean allowLocal) {
121         return getInstance(allowLocal, false);
122     }
123 
124     /**
125      * Protected constructor for subclasses to use.
126      *
127      * @param allowLocal Should local addresses be considered valid?
128      */
129     protected EmailValidator(boolean allowLocal, boolean allowTld) {
130         super();
131         this.allowLocal = allowLocal;
132         this.allowTld = allowTld;
133     }
134 
135     /**
136      * Protected constructor for subclasses to use.
137      *
138      * @param allowLocal Should local addresses be considered valid?
139      */
140     protected EmailValidator(boolean allowLocal) {
141         super();
142         this.allowLocal = allowLocal;
143         this.allowTld = false;
144     }
145 
146     /**
147      * <p>Checks if a field has a valid e-mail address.</p>
148      *
149      * @param email The value validation is being performed on.  A <code>null</code>
150      *              value is considered invalid.
151      * @return true if the email address is valid.
152      */
153     public boolean isValid(String email) {
154         if (email == null) {
155             return false;
156         }
157 
158         if (email.endsWith(".")) { // check this first - it's cheap!
159             return false;
160         }
161 
162         // Check the whole email address structure
163         Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
164         if (!emailMatcher.matches()) {
165             return false;
166         }
167 
168         if (!isValidUser(emailMatcher.group(1))) {
169             return false;
170         }
171 
172         if (!isValidDomain(emailMatcher.group(2))) {
173             return false;
174         }
175 
176         return true;
177     }
178 
179     /**
180      * Returns true if the domain component of an email address is valid.
181      *
182      * @param domain being validated, may be in IDN format
183      * @return true if the email address's domain is valid.
184      */
185     protected boolean isValidDomain(String domain) {
186         // see if domain is an IP address in brackets
187         Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
188 
189         if (ipDomainMatcher.matches()) {
190             InetAddressValidator inetAddressValidator =
191                     InetAddressValidator.getInstance();
192             return inetAddressValidator.isValid(ipDomainMatcher.group(1));
193         }
194         // Domain is symbolic name
195         DomainValidator domainValidator =
196                 DomainValidator.getInstance(allowLocal);
197         if (allowTld) {
198             return domainValidator.isValid(domain) || domainValidator.isValidTld(domain);
199         } else {
200             return domainValidator.isValid(domain);
201         }
202     }
203 
204     /**
205      * Returns true if the user component of an email address is valid.
206      *
207      * @param user being validated
208      * @return true if the user name is valid.
209      */
210     protected boolean isValidUser(String user) {
211         
212         if (user == null || user.length() > 64) {
213             return false;
214         }
215         
216         return USER_PATTERN.matcher(user).matches();
217     }
218 
219 }