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