james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [4/4] james-project git commit: MAILET-129 Simplify NetworkMatchers
Date Thu, 08 Sep 2016 14:32:14 GMT
MAILET-129 Simplify NetworkMatchers


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e17d5679
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e17d5679
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e17d5679

Branch: refs/heads/master
Commit: e17d5679156d6f57891ea2c1d268594a6ee4498b
Parents: 0a41878
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Wed Sep 7 16:59:46 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Thu Sep 8 16:29:58 2016 +0200

----------------------------------------------------------------------
 .../library/netmatcher/NetMatcher.java          | 10 +++++
 .../matchers/AbstractNetworkMatcher.java        | 39 +++-----------------
 .../transport/matchers/RemoteAddrInNetwork.java |  6 ++-
 .../matchers/RemoteAddrNotInNetwork.java        |  6 ++-
 .../matchers/RemoteAddrInNetworkTest.java       |  8 ++--
 .../matchers/RemoteAddrNotInNetworkTest.java    |  8 ++--
 6 files changed, 33 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
----------------------------------------------------------------------
diff --git a/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
b/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
index 6816a7e..fc93531 100644
--- a/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
+++ b/server/dns-service/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/netmatcher/NetMatcher.java
@@ -23,6 +23,7 @@ import java.net.UnknownHostException;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -30,6 +31,8 @@ import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.library.inetnetwork.InetNetworkBuilder;
 import org.apache.james.dnsservice.library.inetnetwork.model.InetNetwork;
 
+import com.google.common.base.Splitter;
+
 /**
  * NetMatcher Class is used to check if an ipAddress match a network.
  * 
@@ -37,6 +40,7 @@ import org.apache.james.dnsservice.library.inetnetwork.model.InetNetwork;
  * address or domain name is within a set of subnets.
  */
 public class NetMatcher {
+    public static final String NETS_SEPARATOR = ", ";
 
     /**
      * The DNS Service used to build InetNetworks.
@@ -74,6 +78,12 @@ public class NetMatcher {
         initInetNetworks(nets);
     }
 
+    public NetMatcher(String commaSeparatedNets, DNSService dnsServer) {
+        this.dnsServer = dnsServer;
+        List<String> nets = Splitter.on(NETS_SEPARATOR).splitToList(commaSeparatedNets);
+        initInetNetworks(nets);
+    }
+
     /**
      * The given String may represent an IP address or a host name.
      * 

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
index 3739a4f..083dd77 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/AbstractNetworkMatcher.java
@@ -18,9 +18,6 @@
  ****************************************************************/
 package org.apache.james.transport.matchers;
 
-import java.util.Collection;
-import java.util.StringTokenizer;
-
 import javax.inject.Inject;
 import javax.mail.MessagingException;
 
@@ -41,31 +38,21 @@ import org.apache.mailet.base.GenericMatcher;
  * </p>
  * <p>
  * This abstract network matcher needs to be implemented by a concrete class.<br>
- * The implementing concrete class will call the allowedNetworks or matchNetwork
- * methods.
+ * The implementing concrete class will call the matchNetwork method.
  * </p>
  * 
  * @see org.apache.james.dnsservice.library.netmatcher.NetMatcher
  */
 public abstract class AbstractNetworkMatcher extends GenericMatcher {
 
-    /**
-     * This is a Network Matcher that should be configured to contain authorized
-     * networks
-     */
-    private NetMatcher authorizedNetworks = null;
+    private NetMatcher authorizedNetworks;
 
-    /**
-     * The DNSService
-     */
     private DNSService dnsServer;
 
+    @Override
     public void init() throws MessagingException {
-
-        Collection<String> nets = allowedNetworks();
-
-        if (nets != null) {
-            authorizedNetworks = new NetMatcher(allowedNetworks(), dnsServer) {
+        if (getCondition() != null) {
+            authorizedNetworks = new NetMatcher(getCondition(), dnsServer) {
                 protected void log(String s) {
                     AbstractNetworkMatcher.this.log(s);
                 }
@@ -74,26 +61,10 @@ public abstract class AbstractNetworkMatcher extends GenericMatcher {
         }
     }
 
-    protected Collection<String> allowedNetworks() {
-        Collection<String> networks = null;
-        if (getCondition() != null) {
-            StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
-            networks = new java.util.ArrayList<String>();
-            while (st.hasMoreTokens())
-                networks.add(st.nextToken());
-        }
-        return networks;
-    }
-
     protected boolean matchNetwork(String addr) {
         return authorizedNetworks != null && authorizedNetworks.matchInetNetwork(addr);
     }
 
-    /**
-     * Injection setter for the DNSService.
-     * 
-     * @param dnsService
-     */
     @Inject
     public void setDNSService(DNSService dnsService) {
         this.dnsServer = dnsService;

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
index 8f7a480..a75f6c2 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrInNetwork.java
@@ -34,7 +34,11 @@ import java.util.Collection;
  * </p>
  */
 public class RemoteAddrInNetwork extends AbstractNetworkMatcher {
+    @Override
     public Collection<MailAddress> match(Mail mail) {
-        return matchNetwork(mail.getRemoteAddr()) ? mail.getRecipients() : null;
+        if (matchNetwork(mail.getRemoteAddr())) {
+            return mail.getRecipients();
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
index 47471e5..331bc07 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/RemoteAddrNotInNetwork.java
@@ -34,7 +34,11 @@ import java.util.Collection;
  * </p>
  */
 public class RemoteAddrNotInNetwork extends AbstractNetworkMatcher {
+    @Override
     public Collection<MailAddress> match(Mail mail) {
-        return matchNetwork(mail.getRemoteAddr()) ? null : mail.getRecipients();
+        if (! matchNetwork(mail.getRemoteAddr())) {
+            return mail.getRecipients();
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
index 336fbe9..8c67ee6 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrInNetworkTest.java
@@ -58,7 +58,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenOnSameNetwork() throws MessagingException {
+    public void shouldMatchWhenOnSameNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.200.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -67,7 +67,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenOnDifferentNetwork() throws MessagingException {
+    public void shouldNotMatchWhenOnDifferentNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.1.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -76,7 +76,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenNoCondition() throws MessagingException {
+    public void shouldNotMatchWhenNoCondition() throws MessagingException {
         FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
         RemoteAddrInNetwork testee = new RemoteAddrInNetwork();
         testee.init(matcherConfig);
@@ -87,7 +87,7 @@ public class RemoteAddrInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenInvalidAddress() throws MessagingException {
+    public void shouldNotMatchWhenInvalidAddress() throws MessagingException {
         fakeMail.setRemoteAddr("invalid");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);

http://git-wip-us.apache.org/repos/asf/james-project/blob/e17d5679/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
index 2605084..c1a4dbd 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/RemoteAddrNotInNetworkTest.java
@@ -58,7 +58,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenOnDifferentNetwork() throws MessagingException {
+    public void shouldMatchWhenOnDifferentNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.0.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -67,7 +67,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldNotMatchWhenOnSameNetwork() throws MessagingException {
+    public void shouldNotMatchWhenOnSameNetwork() throws MessagingException {
         fakeMail.setRemoteAddr("192.168.200.1");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);
@@ -76,7 +76,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenNoCondition() throws MessagingException {
+    public void shouldMatchWhenNoCondition() throws MessagingException {
         FakeMatcherConfig matcherConfig = new FakeMatcherConfig("", FakeMailContext.defaultContext());
         RemoteAddrNotInNetwork testee = new RemoteAddrNotInNetwork();
         testee.init(matcherConfig);
@@ -87,7 +87,7 @@ public class RemoteAddrNotInNetworkTest {
     }
 
     @Test
-    public void matchShouldMatchWhenInvalidAddress() throws MessagingException {
+    public void shouldMatchWhenInvalidAddress() throws MessagingException {
         fakeMail.setRemoteAddr("invalid");
 
         Collection<MailAddress> actual = matcher.match(fakeMail);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message