1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.seasar.cubby.validator.validators; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
import java.util.regex.Matcher; |
22 | |
import java.util.regex.Pattern; |
23 | |
|
24 | |
import org.seasar.cubby.action.MessageInfo; |
25 | |
import org.seasar.cubby.internal.util.StringUtils; |
26 | |
import org.seasar.cubby.validator.ScalarFieldValidator; |
27 | |
import org.seasar.cubby.validator.ValidationContext; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class EmailValidator implements ScalarFieldValidator { |
55 | |
|
56 | |
private static final String SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; |
57 | |
private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; |
58 | |
private static final String QUOTED_USER = "(\"[^\"]*\")"; |
59 | |
private static final String ATOM = VALID_CHARS + '+'; |
60 | |
private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")"; |
61 | |
|
62 | |
private static final String LEGAL_ASCII_PATTERN = "^[\\x00-\\x7F]+$"; |
63 | |
private static final String EMAIL_PATTERN = "^(.+)@(.+)$"; |
64 | |
private static final String IP_DOMAIN_PATTERN = "^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$"; |
65 | |
|
66 | |
private static final String USER_PATTERN = "^" + WORD + "(\\." + WORD |
67 | |
+ ")*$"; |
68 | |
private static final String DOMAIN_PATTERN = "^" + ATOM + "(\\." + ATOM |
69 | |
+ ")*$"; |
70 | |
private static final String ATOM_PATTERN = "(" + ATOM + ")"; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private final String messageKey; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public EmailValidator() { |
81 | 1 | this("valid.email"); |
82 | 1 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 1 | public EmailValidator(final String messageKey) { |
90 | 1 | this.messageKey = messageKey; |
91 | 1 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void validate(final ValidationContext context, final Object value) { |
97 | 16 | if (value == null) { |
98 | 1 | return; |
99 | |
} |
100 | 15 | if (value instanceof String) { |
101 | 15 | final String email = (String) value; |
102 | 15 | if (StringUtils.isEmpty(email)) { |
103 | 1 | return; |
104 | |
} |
105 | |
|
106 | 14 | boolean match = !email.endsWith("."); |
107 | 14 | if (match) { |
108 | 13 | final Pattern pattern = Pattern.compile(LEGAL_ASCII_PATTERN); |
109 | 13 | final Matcher matchAsciiPat = pattern.matcher(email); |
110 | 13 | match = matchAsciiPat.matches(); |
111 | |
} |
112 | |
|
113 | 14 | if (match) { |
114 | 12 | final Pattern pattern = Pattern.compile(EMAIL_PATTERN); |
115 | 12 | final Matcher matcher = pattern.matcher(email); |
116 | 12 | match = matcher.find(); |
117 | 12 | if (match) { |
118 | 10 | if (isValidUser(matcher.group(1)) |
119 | |
&& isValidDomain(matcher.group(2))) { |
120 | 3 | return; |
121 | |
} |
122 | |
} |
123 | |
} |
124 | |
} |
125 | |
|
126 | 11 | final MessageInfo messageInfo = new MessageInfo(); |
127 | 11 | messageInfo.setKey(this.messageKey); |
128 | 11 | context.addMessageInfo(messageInfo); |
129 | 11 | } |
130 | |
|
131 | |
private boolean isValidDomain(final String domain) { |
132 | 8 | Pattern pattern = Pattern.compile(IP_DOMAIN_PATTERN); |
133 | 8 | final Matcher ipAddressMatcher = pattern.matcher(domain); |
134 | |
|
135 | 8 | if (ipAddressMatcher.find()) { |
136 | 2 | if (isValidIpAddress(ipAddressMatcher)) { |
137 | 1 | return true; |
138 | |
} |
139 | |
} else { |
140 | 6 | pattern = Pattern.compile(DOMAIN_PATTERN); |
141 | 6 | final Matcher domainMatcher = pattern.matcher(domain); |
142 | 6 | if (domainMatcher.matches()) { |
143 | 6 | if (isValidSymbolicDomain(domain)) { |
144 | 2 | return true; |
145 | |
} |
146 | |
} |
147 | |
} |
148 | 5 | return false; |
149 | |
} |
150 | |
|
151 | |
private boolean isValidUser(final String user) { |
152 | 10 | final Pattern pattern = Pattern.compile(USER_PATTERN); |
153 | 10 | final Matcher userMatcher = pattern.matcher(user); |
154 | 10 | return userMatcher.matches(); |
155 | |
} |
156 | |
|
157 | |
private boolean isValidIpAddress(final Matcher ipAddressMatcher) { |
158 | 9 | for (int i = 1; i <= 4; i++) { |
159 | 8 | final String ipSegment = ipAddressMatcher.group(i); |
160 | 8 | if (ipSegment == null || ipSegment.length() <= 0) { |
161 | 0 | return false; |
162 | |
} |
163 | |
|
164 | 8 | int iIpSegment = 0; |
165 | |
|
166 | |
try { |
167 | 8 | iIpSegment = Integer.parseInt(ipSegment); |
168 | 0 | } catch (final NumberFormatException e) { |
169 | 0 | return false; |
170 | 8 | } |
171 | |
|
172 | 8 | if (iIpSegment > 255) { |
173 | 1 | return false; |
174 | |
} |
175 | |
|
176 | |
} |
177 | 1 | return true; |
178 | |
} |
179 | |
|
180 | |
private boolean isValidSymbolicDomain(final String domain) { |
181 | 6 | final List<String> domainSegments = new ArrayList<String>(); |
182 | 6 | boolean match = true; |
183 | |
|
184 | 6 | final Pattern pattern = Pattern.compile(ATOM_PATTERN); |
185 | |
String domainSegment; |
186 | 6 | String currentDomain = domain; |
187 | 23 | while (match) { |
188 | 17 | final Matcher atomMatcher = pattern.matcher(currentDomain); |
189 | 17 | match = atomMatcher.find(); |
190 | 17 | if (match) { |
191 | 11 | domainSegment = atomMatcher.group(1); |
192 | 11 | domainSegments.add(domainSegment); |
193 | 11 | final int domainSegmentLength = domainSegment.length() + 1; |
194 | 11 | currentDomain = domainSegmentLength >= currentDomain.length() ? "" |
195 | |
: currentDomain.substring(domainSegmentLength); |
196 | |
} |
197 | 17 | } |
198 | |
|
199 | 6 | final int size = domainSegments.size(); |
200 | 6 | if (size > 0) { |
201 | 6 | final String end = domainSegments.get(size - 1); |
202 | 6 | if (end.length() < 2 || end.length() > 4) { |
203 | 2 | return false; |
204 | |
} |
205 | |
} |
206 | |
|
207 | 4 | if (size < 2) { |
208 | 2 | return false; |
209 | |
} |
210 | |
|
211 | 2 | return true; |
212 | |
} |
213 | |
|
214 | |
} |