View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.ipfilter;
17  
18  import java.net.UnknownHostException;
19  import java.util.ArrayList;
20  
21  import org.jboss.netty.logging.InternalLogger;
22  import org.jboss.netty.logging.InternalLoggerFactory;
23  
24  /**
25   * The Class IpFilterRuleList is a helper class to generate a List of Rules from a string.
26   * In case of parse errors no exceptions are thrown. The error is logged.
27   * <br>
28   * Rule List Syntax:
29   * <br>
30   * <pre>
31   * RuleList ::= Rule[,Rule]*
32   * Rule ::= AllowRule | BlockRule
33   * AllowRule ::= +Filter
34   * BlockRule ::= -Filter
35   * Filter ::= PatternFilter | CIDRFilter
36   * PatternFilter ::= @see PatternRule
37   * CIDRFilter ::= c:CIDRFilter
38   * CIDRFilter ::= @see CIDR.newCIDR(String)
39   * </pre>
40   * <br>
41   * Example: allow only localhost:
42   * <br>
43   * new IPFilterRuleHandler().addAll(new IpFilterRuleList("+n:localhost, -n:*"));
44   * <br>
45   */
46  public class IpFilterRuleList extends ArrayList<IpFilterRule> {
47      private static final long serialVersionUID = -6164162941749588780L;
48  
49      private static final InternalLogger logger = InternalLoggerFactory.getInstance(IpFilterRuleList.class);
50  
51      /**
52       * Instantiates a new ip filter rule list.
53       *
54       * @param rules the rules
55       */
56      public IpFilterRuleList(String rules) {
57          parseRules(rules);
58      }
59  
60      private void parseRules(String rules) {
61          String[] ruless = rules.split(",");
62          for (String rule : ruless) {
63              parseRule(rule.trim());
64          }
65      }
66  
67      private void parseRule(String rule) {
68          if (rule == null || rule.length() == 0) {
69              return;
70          }
71          if (!(rule.startsWith("+") || rule.startsWith("-"))) {
72              if (logger.isErrorEnabled()) {
73                  logger.error("syntax error in ip filter rule:" + rule);
74              }
75              return;
76          }
77  
78          boolean allow = rule.startsWith("+");
79          if (rule.charAt(1) == 'n' || rule.charAt(1) == 'i') {
80              this.add(new PatternRule(allow, rule.substring(1)));
81          } else if (rule.charAt(1) == 'c') {
82              try {
83                  this.add(new IpSubnetFilterRule(allow, rule.substring(3)));
84              } catch (UnknownHostException e) {
85                  if (logger.isErrorEnabled()) {
86                      logger.error("error parsing ip filter " + rule, e);
87                  }
88              }
89          } else {
90              if (logger.isErrorEnabled()) {
91                  logger.error("syntax error in ip filter rule:" + rule);
92              }
93          }
94      }
95  }