KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > AbstractNetworkMatcher


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.transport.matchers;
19
20 import org.apache.james.util.NetMatcher;
21 import javax.mail.MessagingException JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 /**
26   * AbstractNetworkMatcher makes writing IP Address matchers easier.
27   *
28   * AbstractNetworkMatcher provides a means for checking to see whether
29   * a particular IP address or domain is within a set of subnets
30   * These subnets may be expressed in one of several formats:
31   *
32   * Format Example
33   * explicit address 127.0.0.1
34   * address with a wildcard 127.0.0.*
35   * domain name myHost.com
36   * domain name + prefix-length myHost.com/24
37   * domain name + mask myHost.com/255.255.255.0
38   * IP address + prefix-length 127.0.0.0/8
39   * IP + mask 127.0.0.0/255.0.0.0
40   *
41   * For more information, see also: RFC 1518 and RFC 1519.
42   *
43   * @version $ID$
44   */

45 public abstract class AbstractNetworkMatcher extends org.apache.mailet.GenericMatcher {
46
47     /**
48      * This is a Network Matcher that should be configured to contain
49      * authorized networks
50      */

51     private NetMatcher authorizedNetworks = null;
52
53     public void init() throws MessagingException JavaDoc {
54         Collection JavaDoc nets = allowedNetworks();
55         if (nets != null) {
56             authorizedNetworks = new NetMatcher() {
57                 protected void log(String JavaDoc s) {
58                     AbstractNetworkMatcher.this.log(s);
59                 }
60             };
61             authorizedNetworks.initInetNetworks(allowedNetworks());
62             log("Authorized addresses: " + authorizedNetworks.toString());
63         }
64     }
65
66     protected Collection JavaDoc allowedNetworks() {
67         Collection JavaDoc networks = null;
68         if (getCondition() != null) {
69             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getCondition(), ", ", false);
70             networks = new java.util.ArrayList JavaDoc();
71             while (st.hasMoreTokens()) networks.add(st.nextToken());
72         }
73         return networks;
74     }
75
76     protected boolean matchNetwork(java.net.InetAddress JavaDoc addr) {
77         return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
78     }
79
80     protected boolean matchNetwork(String JavaDoc addr) {
81         return authorizedNetworks == null ? false : authorizedNetworks.matchInetNetwork(addr);
82     }
83 }
84
Popular Tags