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 * https://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;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.apache.commons.validator.routines.InetAddressValidator;
23
24 /**
25 * <p>Perform email validations.</p>
26 * <p>
27 * This class is a Singleton; you can retrieve the instance via the getInstance() method.
28 * </p>
29 * <p>
30 * Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
31 * https://javascript.internet.com
32 * </p>
33 * <p>
34 * This implementation is not guaranteed to catch all possible errors in an email address.
35 * For example, an address like nobody@noplace.somedog will pass validator, even though there
36 * is no TLD "somedog"
37 * </p>.
38 *
39 * @since 1.1
40 * @deprecated Use the new EmailValidator in the routines package. This class
41 * will be removed in a future release.
42 */
43 @Deprecated
44 public class EmailValidator {
45
46 private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]";
47 private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
48 private static final String QUOTED_USER = "(\"[^\"]*\")";
49 private static final String ATOM = VALID_CHARS + '+';
50 private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
51
52 // NOT USED private static final Pattern LEGAL_ASCII_PATTERN = Pattern.compile("^\\p{ASCII}+$");
53 // NOT USED private static final Pattern EMAIL_PATTERN = Pattern.compile("^(.+)@(.+)$");
54 private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile("^\\[(.*)\\]$");
55 private static final Pattern TLD_PATTERN = Pattern.compile("^([a-zA-Z]+)$");
56
57 private static final Pattern USER_PATTERN = Pattern.compile("^\\s*" + WORD + "(\\." + WORD + ")*$");
58 private static final Pattern DOMAIN_PATTERN = Pattern.compile("^" + ATOM + "(\\." + ATOM + ")*\\s*$");
59 private static final Pattern ATOM_PATTERN = Pattern.compile("(" + ATOM + ")");
60
61 /**
62 * Singleton instance of this class.
63 */
64 private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
65
66 /**
67 * Returns the Singleton instance of this validator.
68 * @return singleton instance of this validator.
69 */
70 public static EmailValidator getInstance() {
71 return EMAIL_VALIDATOR;
72 }
73
74 /**
75 * Protected constructor for subclasses to use.
76 */
77 protected EmailValidator() {
78 }
79
80 /**
81 * <p>Checks if a field has a valid e-mail address.</p>
82 *
83 * @param email The value validation is being performed on. A {@code null}
84 * value is considered invalid.
85 * @return true if the email address is valid.
86 */
87 public boolean isValid(final String email) {
88 return org.apache.commons.validator.routines.EmailValidator.getInstance().isValid(email);
89 }
90
91 /**
92 * Returns true if the domain component of an email address is valid.
93 * @param domain being validated.
94 * @return true if the email address's domain is valid.
95 */
96 protected boolean isValidDomain(final String domain) {
97 boolean symbolic = false;
98
99 // see if domain is an IP address in brackets
100 final Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
101
102 if (ipDomainMatcher.matches()) {
103 final InetAddressValidator inetAddressValidator =
104 InetAddressValidator.getInstance();
105 if (inetAddressValidator.isValid(ipDomainMatcher.group(1))) {
106 return true;
107 }
108 } else {
109 // Domain is symbolic name
110 symbolic = DOMAIN_PATTERN.matcher(domain).matches();
111 }
112
113 if (!symbolic || !isValidSymbolicDomain(domain)) {
114 return false;
115 }
116
117 return true;
118 }
119
120 /**
121 * Validates an IP address. Returns true if valid.
122 * @param ipAddress IP address
123 * @return true if the ip address is valid.
124 */
125 protected boolean isValidIpAddress(final String ipAddress) {
126 final Matcher ipAddressMatcher = IP_DOMAIN_PATTERN.matcher(ipAddress);
127 for (int i = 1; i <= 4; i++) { // CHECKSTYLE IGNORE MagicNumber
128 final String ipSegment = ipAddressMatcher.group(i);
129 if (ipSegment == null || ipSegment.isEmpty()) {
130 return false;
131 }
132
133 int iIpSegment = 0;
134
135 try {
136 iIpSegment = Integer.parseInt(ipSegment);
137 } catch (final NumberFormatException e) {
138 return false;
139 }
140
141 if (iIpSegment > 255) { // CHECKSTYLE IGNORE MagicNumber
142 return false;
143 }
144
145 }
146 return true;
147 }
148
149 /**
150 * Validates a symbolic domain name. Returns true if it's valid.
151 * @param domain symbolic domain name
152 * @return true if the symbolic domain name is valid.
153 */
154 protected boolean isValidSymbolicDomain(String domain) {
155 final String[] domainSegment = new String[10]; // CHECKSTYLE IGNORE MagicNumber
156 boolean match = true;
157 int i = 0;
158 final Matcher atomMatcher = ATOM_PATTERN.matcher(domain);
159 while (match) {
160 match = atomMatcher.matches();
161 if (match) {
162 domainSegment[i] = atomMatcher.group(1);
163 final int l = domainSegment[i].length() + 1;
164 domain =
165 l >= domain.length()
166 ? ""
167 : domain.substring(l);
168
169 i++;
170 }
171 }
172
173 final int len = i;
174
175 // Make sure there's a host name preceding the domain.
176 if (len < 2) {
177 return false;
178 }
179
180 final String tld = domainSegment[len - 1];
181 if (tld.length() <= 1 || ! TLD_PATTERN.matcher(tld).matches()) {
182 return false;
183 }
184
185 return true;
186 }
187
188 /**
189 * Returns true if the user component of an email address is valid.
190 * @param user being validated
191 * @return true if the username is valid.
192 */
193 protected boolean isValidUser(final String user) {
194 return USER_PATTERN.matcher(user).matches();
195 }
196
197 /**
198 * Recursively remove comments, and replace with a single space. The simpler regexps in the Email Addressing FAQ are imperfect - they will miss escaped
199 * chars in atoms, for example. Derived From Mail::RFC822::Address
200 *
201 * @param emailStr The email address
202 * @return address with comments removed.
203 */
204 protected String stripComments(final String emailStr) {
205 String result = emailStr;
206 final String commentPat = "^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/";
207 final Pattern commentMatcher = Pattern.compile(commentPat);
208
209 while (commentMatcher.matcher(result).matches()) {
210 result = result.replaceFirst(commentPat, "\1 ");
211 }
212 return result;
213 }
214 }