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   * @since 1.4
34   */
35  public class EmailValidator implements Serializable {
36  
37      private static final long serialVersionUID = 1705927040799295880L;
38  
39      private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]";
40      private static final String VALID_CHARS = "(\\\\.)|[^\\s" + SPECIAL_CHARS + "]";
41      private static final String QUOTED_USER = "(\"(\\\\\"|[^\"])*\")";
42      private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
43  
44      private static final String EMAIL_REGEX = "^(.+)@(\\S+)$";
45      private static final String IP_DOMAIN_REGEX = "^\\[(.*)\\]$";
46      private static final String USER_REGEX = "^" + WORD + "(\\." + WORD + ")*$";
47  
48      private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
49      private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile(IP_DOMAIN_REGEX);
50      private static final Pattern USER_PATTERN = Pattern.compile(USER_REGEX);
51  
52      private static final int MAX_USERNAME_LEN = 64;
53  
54      /**
55       * Singleton instance of this class, which
56       *  doesn't consider local addresses as valid.
57       */
58      private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false, false);
59  
60      /**
61       * Singleton instance of this class, which
62       *  doesn't consider local addresses as valid.
63       */
64      private static final EmailValidator EMAIL_VALIDATOR_WITH_TLD = new EmailValidator(false, true);
65  
66      /**
67       * Singleton instance of this class, which does
68       *  consider local addresses valid.
69       */
70      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true, false);
71  
72      /**
73       * Singleton instance of this class, which does
74       *  consider local addresses valid.
75       */
76      private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD = new EmailValidator(true, true);
77  
78      /**
79       * Returns the Singleton instance of this validator.
80       *
81       * @return singleton instance of this validator.
82       */
83      public static EmailValidator getInstance() {
84          return EMAIL_VALIDATOR;
85      }
86  
87      /**
88       * Returns the Singleton instance of this validator,
89       *  with local validation as required.
90       *
91       * @param allowLocal Should local addresses be considered valid?
92       * @return singleton instance of this validator
93       */
94      public static EmailValidator getInstance(final boolean allowLocal) {
95          return getInstance(allowLocal, false);
96      }
97  
98      /**
99       * Returns the Singleton instance of this validator,
100      *  with local validation as required.
101      *
102      * @param allowLocal Should local addresses be considered valid?
103      * @param allowTld Should TLDs be allowed?
104      * @return singleton instance of this validator
105      */
106     public static EmailValidator getInstance(final boolean allowLocal, final boolean allowTld) {
107         if (allowLocal) {
108             if (allowTld) {
109                 return EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD;
110             }
111             return EMAIL_VALIDATOR_WITH_LOCAL;
112         }
113         if (allowTld) {
114             return EMAIL_VALIDATOR_WITH_TLD;
115         }
116         return EMAIL_VALIDATOR;
117     }
118 
119     private final boolean allowTld;
120 
121     private final DomainValidator domainValidator;
122 
123     /**
124      * Protected constructor for subclasses to use.
125      *
126      * @param allowLocal Should local addresses be considered valid?
127      */
128     protected EmailValidator(final boolean allowLocal) {
129         this(allowLocal, false);
130     }
131 
132     /**
133      * Protected constructor for subclasses to use.
134      *
135      * @param allowLocal Should local addresses be considered valid?
136      * @param allowTld Should TLDs be allowed?
137      */
138     protected EmailValidator(final boolean allowLocal, final boolean allowTld) {
139         this.allowTld = allowTld;
140         this.domainValidator = DomainValidator.getInstance(allowLocal);
141     }
142 
143     /**
144      * constructor for creating instances with the specified domainValidator
145      *
146      * @param allowLocal Should local addresses be considered valid?
147      * @param allowTld Should TLDs be allowed?
148      * @param domainValidator allow override of the DomainValidator.
149      * The instance must have the same allowLocal setting.
150      * @since 1.7
151      */
152     public EmailValidator(final boolean allowLocal, final boolean allowTld, final DomainValidator domainValidator) {
153         this.allowTld = allowTld;
154         if (domainValidator == null) {
155             throw new IllegalArgumentException("DomainValidator cannot be null");
156         }
157         if (domainValidator.isAllowLocal() != allowLocal) {
158             throw new IllegalArgumentException("DomainValidator must agree with allowLocal setting");
159         }
160         this.domainValidator = domainValidator;
161     }
162 
163     /**
164      * <p>Checks if a field has a valid e-mail address.</p>
165      *
166      * @param email The value validation is being performed on.  A <code>null</code>
167      *              value is considered invalid.
168      * @return true if the email address is valid.
169      */
170     public boolean isValid(final String email) {
171         if (email == null) {
172             return false;
173         }
174 
175         if (email.endsWith(".")) { // check this first - it's cheap!
176             return false;
177         }
178 
179         // Check the whole email address structure
180         final Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
181         if (!emailMatcher.matches()) {
182             return false;
183         }
184 
185         if (!isValidUser(emailMatcher.group(1))) {
186             return false;
187         }
188 
189         if (!isValidDomain(emailMatcher.group(2))) {
190             return false;
191         }
192 
193         return true;
194     }
195 
196     /**
197      * Returns true if the domain component of an email address is valid.
198      *
199      * @param domain being validated, may be in IDN format
200      * @return true if the email address's domain is valid.
201      */
202     protected boolean isValidDomain(final String domain) {
203         // see if domain is an IP address in brackets
204         final Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
205 
206         if (ipDomainMatcher.matches()) {
207             final InetAddressValidator inetAddressValidator =
208                     InetAddressValidator.getInstance();
209             return inetAddressValidator.isValid(ipDomainMatcher.group(1));
210         }
211         // Domain is symbolic name
212         if (allowTld) {
213             return domainValidator.isValid(domain) || !domain.startsWith(".") && domainValidator.isValidTld(domain);
214         }
215         return domainValidator.isValid(domain);
216     }
217 
218     /**
219      * Returns true if the user component of an email address is valid.
220      *
221      * @param user being validated
222      * @return true if the user name is valid.
223      */
224     protected boolean isValidUser(final String user) {
225 
226         if (user == null || user.length() > MAX_USERNAME_LEN) {
227             return false;
228         }
229 
230         return USER_PATTERN.matcher(user).matches();
231     }
232 
233 }