1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
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 | 1 | private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX); |
50 | 1 | private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile(IP_DOMAIN_REGEX); |
51 | 1 | 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 | |
|
60 | |
|
61 | |
|
62 | 1 | private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false, false); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 1 | private static final EmailValidator EMAIL_VALIDATOR_WITH_TLD = new EmailValidator(false, true); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 1 | private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true, false); |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 1 | private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD = new EmailValidator(true, true); |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static EmailValidator getInstance() { |
89 | 85 | return EMAIL_VALIDATOR; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public static EmailValidator getInstance(boolean allowLocal, boolean allowTld) { |
101 | 5 | if(allowLocal) { |
102 | 2 | if (allowTld) { |
103 | 1 | return EMAIL_VALIDATOR_WITH_LOCAL_WITH_TLD; |
104 | |
} else { |
105 | 1 | return EMAIL_VALIDATOR_WITH_LOCAL; |
106 | |
} |
107 | |
} else { |
108 | 3 | if (allowTld) { |
109 | 2 | return EMAIL_VALIDATOR_WITH_TLD; |
110 | |
} else { |
111 | 1 | return EMAIL_VALIDATOR; |
112 | |
} |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public static EmailValidator getInstance(boolean allowLocal) { |
124 | 2 | return getInstance(allowLocal, false); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
protected EmailValidator(boolean allowLocal, boolean allowTld) { |
134 | 4 | super(); |
135 | 4 | this.allowLocal = allowLocal; |
136 | 4 | this.allowTld = allowTld; |
137 | 4 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
protected EmailValidator(boolean allowLocal) { |
145 | 0 | super(); |
146 | 0 | this.allowLocal = allowLocal; |
147 | 0 | this.allowTld = false; |
148 | 0 | } |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public boolean isValid(String email) { |
158 | 217 | if (email == null) { |
159 | 0 | return false; |
160 | |
} |
161 | |
|
162 | 217 | if (email.endsWith(".")) { |
163 | 4 | return false; |
164 | |
} |
165 | |
|
166 | |
|
167 | 213 | Matcher emailMatcher = EMAIL_PATTERN.matcher(email); |
168 | 213 | if (!emailMatcher.matches()) { |
169 | 4 | return false; |
170 | |
} |
171 | |
|
172 | 209 | if (!isValidUser(emailMatcher.group(1))) { |
173 | 78 | return false; |
174 | |
} |
175 | |
|
176 | 131 | if (!isValidDomain(emailMatcher.group(2))) { |
177 | 41 | return false; |
178 | |
} |
179 | |
|
180 | 90 | return true; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
protected boolean isValidDomain(String domain) { |
190 | |
|
191 | 131 | Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain); |
192 | |
|
193 | 131 | if (ipDomainMatcher.matches()) { |
194 | 2 | InetAddressValidator inetAddressValidator = |
195 | |
InetAddressValidator.getInstance(); |
196 | 2 | return inetAddressValidator.isValid(ipDomainMatcher.group(1)); |
197 | |
} |
198 | |
|
199 | 129 | DomainValidator domainValidator = |
200 | |
DomainValidator.getInstance(allowLocal); |
201 | 129 | if (allowTld) { |
202 | 3 | return domainValidator.isValid(domain) || (!domain.startsWith(".") && domainValidator.isValidTld(domain)); |
203 | |
} else { |
204 | 126 | return domainValidator.isValid(domain); |
205 | |
} |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
protected boolean isValidUser(String user) { |
215 | |
|
216 | 209 | if (user == null || user.length() > MAX_USERNAME_LEN) { |
217 | 2 | return false; |
218 | |
} |
219 | |
|
220 | 207 | return USER_PATTERN.matcher(user).matches(); |
221 | |
} |
222 | |
|
223 | |
} |