View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.transport.matchers;
21  
22  import org.apache.james.util.NetMatcher;
23  import javax.mail.MessagingException;
24  import java.util.StringTokenizer;
25  import java.util.Collection;
26  
27  /***
28    * AbstractNetworkMatcher makes writing IP Address matchers easier.
29    *
30    * AbstractNetworkMatcher provides a means for checking to see whether
31    * a particular IP address or domain is within a set of subnets
32    * These subnets may be expressed in one of several formats:
33    * 
34    *     Format                          Example
35    *     explicit address                127.0.0.1
36    *     address with a wildcard         127.0.0.*
37    *     domain name                     myHost.com
38    *     domain name + prefix-length     myHost.com/24
39    *     domain name + mask              myHost.com/255.255.255.0
40    *     IP address + prefix-length      127.0.0.0/8
41    *     IP + mask                       127.0.0.0/255.0.0.0
42    *
43    * For more information, see also: RFC 1518 and RFC 1519.
44    * 
45    * @version $ID$
46    */
47  public abstract class AbstractNetworkMatcher extends org.apache.mailet.GenericMatcher {
48  
49      /***
50       * This is a Network Matcher that should be configured to contain
51       * authorized networks
52       */
53      private NetMatcher authorizedNetworks = null;
54  
55      public void init() throws MessagingException {
56          Collection nets = allowedNetworks();
57          if (nets != null) {
58              authorizedNetworks = new NetMatcher() {
59                  protected void log(String s) {
60                      AbstractNetworkMatcher.this.log(s);
61                  }
62              };
63              authorizedNetworks.initInetNetworks(allowedNetworks());
64              log("Authorized addresses: " + authorizedNetworks.toString());
65          }
66      }
67  
68      protected Collection allowedNetworks() {
69          Collection networks = null;
70          if (getCondition() != null) {
71              StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
72              networks = new java.util.ArrayList();
73              while (st.hasMoreTokens()) networks.add(st.nextToken());
74          }
75          return networks;
76      }
77  
78      protected boolean matchNetwork(java.net.InetAddress addr) {
79          return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
80      }
81  
82      protected boolean matchNetwork(String addr) {
83          return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
84      }
85  }