1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.ipfilter;
17
18 import org.jboss.netty.logging.InternalLogger;
19 import org.jboss.netty.logging.InternalLoggerFactory;
20 import org.jboss.netty.util.internal.StringUtil;
21
22 import java.net.UnknownHostException;
23 import java.util.ArrayList;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class IpFilterRuleList extends ArrayList<IpFilterRule> {
48 private static final long serialVersionUID = -6164162941749588780L;
49
50 private static final InternalLogger logger = InternalLoggerFactory.getInstance(IpFilterRuleList.class);
51
52
53
54
55
56
57 public IpFilterRuleList(String rules) {
58 parseRules(rules);
59 }
60
61 private void parseRules(String rules) {
62 String[] ruless = StringUtil.split(rules, ',');
63 for (String rule : ruless) {
64 parseRule(rule.trim());
65 }
66 }
67
68 private void parseRule(String rule) {
69 if (rule == null || rule.length() == 0) {
70 return;
71 }
72 if (!(rule.startsWith("+") || rule.startsWith("-"))) {
73 if (logger.isErrorEnabled()) {
74 logger.error("syntax error in ip filter rule:" + rule);
75 }
76 return;
77 }
78
79 boolean allow = rule.startsWith("+");
80 if (rule.charAt(1) == 'n' || rule.charAt(1) == 'i') {
81 add(new PatternRule(allow, rule.substring(1)));
82 } else if (rule.charAt(1) == 'c') {
83 try {
84 add(new IpSubnetFilterRule(allow, rule.substring(3)));
85 } catch (UnknownHostException e) {
86 if (logger.isErrorEnabled()) {
87 logger.error("error parsing ip filter " + rule, e);
88 }
89 }
90 } else {
91 if (logger.isErrorEnabled()) {
92 logger.error("syntax error in ip filter rule:" + rule);
93 }
94 }
95 }
96 }