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  
21  
22  package org.apache.james.transport.matchers;
23  
24  import org.apache.avalon.framework.service.ServiceManager;
25  import org.apache.james.Constants;
26  import org.apache.james.api.dnsservice.DNSService;
27  import org.apache.james.api.dnsservice.util.NetMatcher;
28  
29  import javax.mail.MessagingException;
30  import java.util.StringTokenizer;
31  import java.util.Collection;
32  
33  /**
34    * AbstractNetworkMatcher makes writing IP Address matchers easier.
35    *
36    * AbstractNetworkMatcher provides a means for checking to see whether
37    * a particular IP address or domain is within a set of subnets
38    * These subnets may be expressed in one of several formats:
39    * 
40    *     Format                          Example
41    *     explicit address                127.0.0.1
42    *     address with a wildcard         127.0.0.*
43    *     domain name                     myHost.com
44    *     domain name + prefix-length     myHost.com/24
45    *     domain name + mask              myHost.com/255.255.255.0
46    *     IP address + prefix-length      127.0.0.0/8
47    *     IP + mask                       127.0.0.0/255.0.0.0
48    *
49    * For more information, see also: RFC 1518 and RFC 1519.
50    * 
51    * @version $ID$
52    */
53  public abstract class AbstractNetworkMatcher extends org.apache.mailet.base.GenericMatcher {
54  
55      /**
56       * This is a Network Matcher that should be configured to contain
57       * authorized networks
58       */
59      private NetMatcher authorizedNetworks = null;
60      
61      /**
62       * The DNSService
63       */
64      private DNSService dnsServer;
65      
66      /**
67       * The ServiceManger
68       */
69      private ServiceManager compMgr;
70  
71      public void init() throws MessagingException {
72          
73          setServiceManager((ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER));
74          
75          try {
76              // Instantiate DNSService
77              setDNSServer((DNSService) compMgr.lookup(DNSService.ROLE));
78          } catch (Exception e) {
79              throw new MessagingException("Failed to retrieve DNSService:" + e.getMessage());
80          }
81          
82          Collection nets = allowedNetworks();
83          
84          if (nets != null) {
85              authorizedNetworks = new NetMatcher(allowedNetworks(),dnsServer) {
86                  protected void log(String s) {
87                      AbstractNetworkMatcher.this.log(s);
88                  }
89              };
90              authorizedNetworks.initInetNetworks(allowedNetworks());
91              log("Authorized addresses: " + authorizedNetworks.toString());
92          }
93      }
94  
95      protected Collection allowedNetworks() {
96          Collection networks = null;
97          if (getCondition() != null) {
98              StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
99              networks = new java.util.ArrayList();
100             while (st.hasMoreTokens()) networks.add(st.nextToken());
101         }
102         return networks;
103     }
104 
105     protected boolean matchNetwork(java.net.InetAddress addr) {
106         return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
107     }
108 
109     protected boolean matchNetwork(String addr) {
110         return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
111     }
112     
113     
114     private void setDNSServer(DNSService dnsServer) {
115         this.dnsServer = dnsServer;
116     }
117     
118     private void setServiceManager(ServiceManager compMgr) {
119         this.compMgr = compMgr;
120     }
121 }