/* ====================================================================
 * 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 apache@apache.org.
 *
 * 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);
    }
}

@@ -4140,16 +4140,93 @@
 ption %7B%0A
+        Collection nets = allowedNetworks();%0A        if (nets != null) %7B%0A    
         
@@ -4261,24 +4261,28 @@
 Matcher() %7B%0A
+    
             
@@ -4328,16 +4328,20 @@
         
+    
 Abstract
@@ -4380,16 +4380,20 @@
         
+    
 %7D%0A      
@@ -4394,16 +4394,20 @@
         
+ 
+   
 %7D;%0A     
@@ -4401,32 +4401,36 @@
      %7D;%0A        
+  
+  
 authorizedNetwor
@@ -4465,24 +4465,28 @@
 etworks());%0A
+    
         log(
@@ -4540,24 +4540,34 @@
 oString());%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -4602,24 +4602,102 @@
 etworks() %7B%0A
+        Collection networks = null;%0A        if (getCondition() != null) %7B%0A    
         Stri
@@ -4767,34 +4767,27 @@
 e);%0A        
-Collection
+   
  networks = 
@@ -4809,24 +4809,28 @@
 rrayList();%0A
+    
         whil
@@ -4878,24 +4878,34 @@
 xtToken());%0A
+        %7D%0A
         retu
@@ -4994,32 +4994,69 @@
 %7B%0A        return
+ authorizedNetworks == null ? false :
  authorizedNetwo
@@ -5146,32 +5146,69 @@
 %7B%0A        return
+ authorizedNetworks == null ? false :
  authorizedNetwo

Handle case where no networks are provided, rather than NPE