@@ 9-27 (lines=19) @@ | ||
6 | import org.springframework.validation.Errors; |
|
7 | import org.springframework.validation.Validator; |
|
8 | ||
9 | @Component |
|
10 | public class EmailValidator implements Validator { |
|
11 | private static final Pattern EMAIL_PATTERN = |
|
12 | Pattern.compile( |
|
13 | "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" |
|
14 | + "((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"); |
|
15 | ||
16 | @Override |
|
17 | public boolean supports(final Class<?> aClass) { |
|
18 | return String.class.equals(aClass); |
|
19 | } |
|
20 | ||
21 | @Override |
|
22 | public void validate(final Object obj, final Errors errors) { |
|
23 | final String email = (String) obj; |
|
24 | if (email == null || email.trim().isEmpty()) { |
|
25 | errors.reject("email", "Invalid empty email address"); |
|
26 | return; |
|
27 | } |
|
28 | final Matcher matcher = EMAIL_PATTERN.matcher(email); |
|
29 | if (!matcher.matches()) { |
|
30 | errors.reject("email", "Invalid email address (" + email + ")"); |
@@ 9-26 (lines=18) @@ | ||
6 | import org.springframework.validation.Errors; |
|
7 | import org.springframework.validation.Validator; |
|
8 | ||
9 | @Component |
|
10 | class CertificateValidator implements Validator { |
|
11 | private static final Pattern CERTIFICATE_PATTERN = Pattern.compile("^[A-Z]{4}[0-9]{4}$"); |
|
12 | ||
13 | @Override |
|
14 | public boolean supports(final Class<?> aClass) { |
|
15 | return String.class.equals(aClass); |
|
16 | } |
|
17 | ||
18 | @Override |
|
19 | public void validate(final Object obj, final Errors errors) { |
|
20 | final String certificate = (String) obj; |
|
21 | if (certificate == null || certificate.trim().isEmpty()) { |
|
22 | errors.reject("certificate", "Invalid empty certificate"); |
|
23 | return; |
|
24 | } |
|
25 | final Matcher matcher = CERTIFICATE_PATTERN.matcher(certificate); |
|
26 | if (!matcher.matches()) { |
|
27 | errors.reject("certificate", "Invalid certificate code (" + certificate + ")"); |
|
28 | } |
|
29 | } |