noel 2003/06/06 16:51:38
Modified: src/java/org/apache/james/transport/matchers
RemoteAddrInNetwork.java
RemoteAddrNotInNetwork.java
Added: src/java/org/apache/james/transport/matchers
AbstractNetworkMatcher.java
Log:
New AbstractNetworkMatcher class using NetMatcher. RemoteAddr[Not]InNetwork are now
subclasses.
Revision Changes Path
1.9 +6 -57
jakarta-james/src/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
Index: RemoteAddrInNetwork.java
===================================================================
RCS file:
/home/cvs/jakarta-james/src/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- RemoteAddrInNetwork.java 8 Mar 2003 21:14:10 -0000 1.8
+++ RemoteAddrInNetwork.java 6 Jun 2003 23:51:38 -0000 1.9
@@ -58,72 +58,21 @@
package org.apache.james.transport.matchers;
-import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
import javax.mail.MessagingException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.util.Collection;
-import java.util.Iterator;
-import java.util.StringTokenizer;
-import java.util.Vector;
/**
* Checks the IP address of the sending server against a comma-
- * delimited list of IP addresses or domain names.
- * <P>Networks should be indicated with a wildcard *, e.g. 192.168.*
- * <br>Note: The wildcard can go at any level, the matcher will match if the
- * sending host's IP address (as a String based on the octet representation)
- * starts with the String indicated in the configuration file, excluding the
- * wildcard.
- * <p>Multiple addresses can be indicated, e.g: '127.0.0.1,192.168.*,domain.tld'
+ * delimited list of IP addresses, domain names or sub-nets.
+ *
+ * <p>See AbstractNetworkMatcher for details on how to specify
+ * entries.</p>
*
- * @author Serge Knystautas <[EMAIL PROTECTED]>
*/
-public class RemoteAddrInNetwork extends GenericMatcher {
- private Collection networks = null;
-
- public void init() throws MessagingException {
- StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
- networks = new Vector();
- while (st.hasMoreTokens()) {
- String addr = st.nextToken();
- if (addr.equals("127.0.0.1")) {
- //Add address of local machine as a match
- try {
- InetAddress localaddr = InetAddress.getLocalHost();
- networks.add(localaddr.getHostAddress());
- } catch (UnknownHostException uhe) {
- }
- }
-
- try {
- if (addr.endsWith("*")) {
- addr = addr.substring(0, addr.length() - 1);
- }
- else {
- addr = InetAddress.getByName(addr).getHostAddress();
- }
- networks.add(addr);
- } catch (UnknownHostException uhe) {
- log("Cannot match against invalid domain: " + uhe.getMessage());
- }
- }
- }
-
+public class RemoteAddrInNetwork extends AbstractNetworkMatcher {
public Collection match(Mail mail) {
- String host = mail.getRemoteAddr();
- //Check to see whether it's in any of the networks... needs to be smarter to
- // support subnets better
- for (Iterator i = networks.iterator(); i.hasNext(); ) {
- String networkAddr = i.next().toString();
- if (host.startsWith(networkAddr)) {
- //This is in this network... that's all we need for a match
- return mail.getRecipients();
- }
- }
- //Could not match this to any network
- return null;
+ return matchNetwork(mail.getRemoteAddr()) ? mail.getRecipients() : null;
}
}
1.9 +6 -57
jakarta-james/src/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
Index: RemoteAddrNotInNetwork.java
===================================================================
RCS file:
/home/cvs/jakarta-james/src/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- RemoteAddrNotInNetwork.java 8 Mar 2003 21:14:10 -0000 1.8
+++ RemoteAddrNotInNetwork.java 6 Jun 2003 23:51:38 -0000 1.9
@@ -58,72 +58,21 @@
package org.apache.james.transport.matchers;
-import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
import javax.mail.MessagingException;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.util.Collection;
-import java.util.Iterator;
-import java.util.StringTokenizer;
-import java.util.Vector;
/**
* Checks the IP address of the sending server against a comma-
- * delimited list of IP addresses or domain names.
- * <P>Networks should be indicated with a wildcard *, e.g. 192.168.*
- * <br>Note: The wildcard can go at any level, the matcher will match if the
- * sending host's IP address (as a String based on the octet representation)
- * starts with the String indicated in the configuration file, excluding the
- * wildcard.
- * <p>Multiple addresses can be indicated, e.g: '127.0.0.1,192.168.*,domain.tld'
+ * delimited list of IP addresses, domain names or sub-nets.
+ *
+ * <p>See AbstractNetworkMatcher for details on how to specify
+ * entries.</p>
*
- * @author Serge Knystautas <[EMAIL PROTECTED]>
*/
-public class RemoteAddrNotInNetwork extends GenericMatcher {
- private Collection networks = null;
-
- public void init() throws MessagingException {
- StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
- networks = new Vector();
- while (st.hasMoreTokens()) {
- String addr = st.nextToken();
- if (addr.equals("127.0.0.1")) {
- //Add address of local machine as a match
- try {
- InetAddress localaddr = InetAddress.getLocalHost();
- networks.add(localaddr.getHostAddress());
- } catch (UnknownHostException uhe) {
- }
- }
-
- try {
- if (addr.endsWith("*")) {
- addr = addr.substring(0, addr.length() - 1);
- }
- else {
- addr = InetAddress.getByName(addr).getHostAddress();
- }
- networks.add(addr);
- } catch (UnknownHostException uhe) {
- log("Cannot match against invalid domain: " + uhe.getMessage());
- }
- }
- }
-
+public class RemoteAddrNotInNetwork extends AbstractNetworkMatcher {
public Collection match(Mail mail) {
- String host = mail.getRemoteAddr();
- //Check to see whether it's in any of the networks... needs to be smarter to
- // support subnets better
- for (Iterator i = networks.iterator(); i.hasNext(); ) {
- String invalidNetwork = i.next().toString();
- if (host.startsWith(invalidNetwork)) {
- //This is in this network... that's all we need for a failed match
- return null;
- }
- }
- //Could not match this to any network
- return mail.getRecipients();
+ return matchNetwork(mail.getRemoteAddr()) ? null : mail.getRecipients();
}
}
1.1
jakarta-james/src/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
Index: AbstractNetworkMatcher.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache", "Jakarta", "JAMES" and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
package org.apache.james.transport.matchers;
import org.apache.james.util.NetMatcher;
import javax.mail.MessagingException;
import java.util.StringTokenizer;
import java.util.Collection;
/**
* AbstractNetworkMatcher makes writing IP Address matchers easier.
*
* AbstractNetworkMatcher provides a means for checking to see whether
* a particular IP address or domain is within a set of subnets
* These subnets may be expressed in one of several formats:
*
* Format Example
* explicit address 127.0.0.1
* address with a wildcard 127.0.0.*
* domain name myHost.com
* domain name + prefix-length myHost.com/24
* domain name + mask myHost.com/255.255.255.0
* IP address + prefix-length 127.0.0.0/8
* IP + mask 127.0.0.0/255.0.0.0
*
* For more information, see also: RFC 1518 and RFC 1519.
*
* @version $ID$
*/
public abstract class AbstractNetworkMatcher extends
org.apache.mailet.GenericMatcher {
/**
* This is a Network Matcher that should be configured to contain
* authorized networks
*/
private NetMatcher authorizedNetworks = null;
public void init() throws MessagingException {
authorizedNetworks = new NetMatcher() {
protected void log(String s) {
AbstractNetworkMatcher.this.log(s);
}
};
authorizedNetworks.initInetNetworks(allowedNetworks());
log("Authorized addresses: " + authorizedNetworks.toString());
}
protected Collection allowedNetworks() {
StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
Collection networks = new java.util.ArrayList();
while (st.hasMoreTokens()) networks.add(st.nextToken());
return networks;
}
protected boolean matchNetwork(java.net.InetAddress addr) {
return authorizedNetworks.matchInetNetwork(addr);
}
protected boolean matchNetwork(String addr) {
return authorizedNetworks.matchInetNetwork(addr);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]