1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.valjax;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.beans.factory.InitializingBean;
24
25 import java.io.BufferedReader;
26 import java.io.InputStreamReader;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 /***
32 * Provides validation for a configured black list file.
33 *
34 * <p>Implements {@link #afterPropertiesSet afterPropertiesSet}
35 * using the black list file from {@link #setBlackListFile
36 * setBlacklistFile } and loads the contents into a {@link HashSet} that
37 * is used to validate values against. Each line in the blacklist is its own
38 * entry in the {@link HashSet}.
39 *
40 * <p>Concrete implementations of this class need to follow the contract
41 * stated in the {@link org.springframework.validation.Validator Validator} interface.
42 * Concrete classes can also provide there own blacklist files, which will
43 * override the default blacklist file.</p>
44 *
45 * <p>This is a one <code>blackListFile</code> per <code>Validator</code> setup. You can
46 * use the default blacklist file or you can supply your own, but you can not
47 * use both.
48 *
49 * @see org.springframework.validation.Validator
50 *
51 * @author Zach Legein
52 */
53 public abstract class AbstractBlackListValidator extends AbstractValidator implements InitializingBean {
54 protected static final Log log = LogFactory.getLog(AbstractBlackListValidator.class);
55 private final Set<String> blackListSet = new HashSet<String>();
56 private String blackListFile;
57
58 /***
59 * Validates whether an email is on the blacklist or not.
60 *
61 * <p>Strips the email address using the '@' symbol
62 * and check each piece to see if it is on the blacklist.</p>
63 *
64 * @param value The <code>String</code> to check.
65 * @return Whether value is on the blacklist or not.
66 */
67 public Boolean isBlackListEmail(String value) {
68 if (value.indexOf('@') > -1) {
69 for (String str : value.split("@")) {
70 if (blackListSet.contains(str)) {
71 return false;
72 }
73 }
74 }
75
76 return true;
77 }
78
79 /***
80 * Validates whether an value is on the blacklist or not.
81 *
82 * @param value The input to validate.
83 * @return Whether <code>value</code> is on the blacklist or not.
84 */
85 public Boolean isBlackListValue(String value) {
86 return !blackListSet.contains(value);
87
88 }
89
90 /***
91 * Loads the blackListFile in a {@link HashSet}
92 * that will be used to validate against.
93 *
94 * <p>BlackListFile is taken from the claspath fed into an
95 * {@link InputStreamReader} as an {@link java.io.InputStream InputStream}.
96 * Concrete implementation of this class can supply their own blackList
97 * file or can use the default, but you can not use both.</p>
98 */
99 public void afterPropertiesSet() {
100 BufferedReader reader = null;
101
102 try {
103 ClassLoader loader = AbstractBlackListValidator.class.getClassLoader();
104
105 reader = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(blackListFile)));
106
107 String line;
108
109
110 while ((line = reader.readLine()) != null) {
111 blackListSet.addAll(Arrays.asList(line.toLowerCase().split(",")));
112 }
113 }
114 catch (Exception ex) {
115 throw new IllegalArgumentException("Error loading blacklist file: " + blackListFile, ex);
116 }
117 finally {
118 if (reader != null) {
119 try {
120 reader.close();
121 }
122 catch (Exception e) {
123 log.error("Closing of reader failed", e);
124 }
125 }
126 }
127 }
128
129 /***
130 * The name of the blacklist file to load from the classpath.
131 *
132 * @param blackListFile The blacklist file to load.
133 */
134 public void setBlackListFile(String blackListFile) {
135 this.blackListFile = blackListFile;
136 }
137 }