View Javadoc
1   package net.avcompris.examples.shared3.core.impl;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static org.apache.commons.lang3.StringUtils.isBlank;
5   
6   import java.util.regex.Pattern;
7   
8   import javax.annotation.Nullable;
9   
10  import net.avcompris.commons3.api.exception.InvalidPasswordException;
11  import net.avcompris.commons3.api.exception.InvalidRoleException;
12  import net.avcompris.commons3.api.exception.InvalidUsernameException;
13  import net.avcompris.commons3.api.exception.ReservedUsernameException;
14  import net.avcompris.examples.shared3.Role;
15  import net.avcompris.examples.shared3.Constants.ReservedUsername;
16  
17  public abstract class Validations {
18  
19  	private static final Pattern USERNAME_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9-_]*$");
20  
21  	public static void assertValidUsername(final String username)
22  			throws InvalidUsernameException, ReservedUsernameException {
23  
24  		checkNotNull(username, "username");
25  
26  		if (ReservedUsername.ME.label().contentEquals(username)) {
27  
28  			throw new ReservedUsernameException(username);
29  		}
30  
31  		try {
32  
33  			assertNotBlank("Username", username);
34  			assertMaxLength("Username", 40, username);
35  			assertASCII("Username", username);
36  			assertRegexp("Username", USERNAME_PATTERN, username);
37  
38  		} catch (final ValidationException e) {
39  
40  			throw new InvalidUsernameException(e.getMessage());
41  		}
42  	}
43  
44  	public static void assertNonSuperadminUsername(@Nullable final String username) throws ReservedUsernameException {
45  
46  		if (ReservedUsername.SUPERADMIN.label().contentEquals(username)) {
47  
48  			throw new ReservedUsernameException(username);
49  		}
50  	}
51  
52  	public static void assertValidPassword(final String password) throws InvalidPasswordException {
53  
54  		checkNotNull(password, "password");
55  
56  		try {
57  
58  			assertNotBlank("Password", password);
59  			assertMaxLength("Password", 40, password);
60  			assertASCII("Password", password);
61  			// assertRegexp("Password", PASSWORD_PATTERN, password);
62  
63  		} catch (final ValidationException e) {
64  
65  			throw new InvalidPasswordException(e.getMessage());
66  		}
67  	}
68  
69  	public static void assertValidRole(final Role role) throws InvalidRoleException {
70  
71  		// do nothing
72  	}
73  
74  	private static class ValidationException extends Exception {
75  
76  		private static final long serialVersionUID = -8064040017766344235L;
77  
78  		ValidationException(final String message) {
79  
80  			super(message);
81  		}
82  	}
83  
84  	private static void assertNotBlank(final String label, @Nullable final String s) throws ValidationException {
85  
86  		if (isBlank(s)) {
87  
88  			throw new ValidationException(label + " must at least contain some characters.");
89  		}
90  	}
91  
92  	private static void assertMaxLength(final String label, final int maxLength, final String s)
93  			throws ValidationException {
94  
95  		if (s.length() > maxLength) {
96  
97  			throw new ValidationException(
98  					label + " should contain at most " + maxLength + " characters, but was: " + s.length());
99  		}
100 	}
101 
102 	private static void assertASCII(final String label, final String s) throws ValidationException {
103 
104 		for (final char c : s.toCharArray()) {
105 
106 			if (c <= 32 || c > 126) {
107 				throw new ValidationException(
108 						label + " must only contain ASCII non space characters: (" + ((int) c) + "): " + c);
109 			}
110 		}
111 	}
112 
113 	private static void assertRegexp(final String label, final Pattern pattern, final String s)
114 			throws ValidationException {
115 
116 		if (!pattern.matcher(s).matches()) {
117 
118 			throw new ValidationException(label + " should match the regular expression: " + pattern.pattern());
119 		}
120 	}
121 }