01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.transport.matchers;
19:
20: import org.apache.james.util.NetMatcher;
21: import javax.mail.MessagingException;
22: import java.util.StringTokenizer;
23: import java.util.Collection;
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
46: org.apache.mailet.GenericMatcher {
47:
48: /**
49: * This is a Network Matcher that should be configured to contain
50: * authorized networks
51: */
52: private NetMatcher authorizedNetworks = null;
53:
54: public void init() throws MessagingException {
55: Collection nets = allowedNetworks();
56: if (nets != null) {
57: authorizedNetworks = new NetMatcher() {
58: protected void log(String s) {
59: AbstractNetworkMatcher.this .log(s);
60: }
61: };
62: authorizedNetworks.initInetNetworks(allowedNetworks());
63: log("Authorized addresses: "
64: + authorizedNetworks.toString());
65: }
66: }
67:
68: protected Collection allowedNetworks() {
69: Collection networks = null;
70: if (getCondition() != null) {
71: StringTokenizer st = new StringTokenizer(getCondition(),
72: ", ", false);
73: networks = new java.util.ArrayList();
74: while (st.hasMoreTokens())
75: networks.add(st.nextToken());
76: }
77: return networks;
78: }
79:
80: protected boolean matchNetwork(java.net.InetAddress addr) {
81: return authorizedNetworks == null ? false : authorizedNetworks
82: .matchInetNetwork(addr);
83: }
84:
85: protected boolean matchNetwork(String addr) {
86: return authorizedNetworks == null ? false : authorizedNetworks
87: .matchInetNetwork(addr);
88: }
89: }
|