Repository: james-project
Updated Branches:
  refs/heads/master b738594d1 -> 4ac57ec9e


JAMES-1773 HasHeader should not parse conditions on each received e-mail

I did not use Guava for refactoring as it is not present in this project.

The idea is to introduce specific objects for conditions, that will be parsed 
once and for all.


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

Branch: refs/heads/master
Commit: 4ac57ec9e13b11901dc94b0f190e438953447cbc
Parents: 80a2ca8
Author: Benoit Tellier <[email protected]>
Authored: Tue Aug 9 14:42:31 2016 +0700
Committer: Benoit Tellier <[email protected]>
Committed: Thu Aug 18 17:18:15 2016 +0700

----------------------------------------------------------------------
 .../james/transport/matchers/HasHeader.java     | 117 ++++++++++---------
 1 file changed, 63 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4ac57ec9/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeader.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeader.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeader.java
index 00b336d..c8484d9 100644
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeader.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeader.java
@@ -26,8 +26,10 @@ import org.apache.mailet.base.GenericMatcher;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.List;
 import java.util.StringTokenizer;
 
 /**
@@ -38,73 +40,80 @@ import java.util.StringTokenizer;
  */
 public class HasHeader extends GenericMatcher {
 
-    private LinkedList<String> conditionline_ = new LinkedList<String>();
+    private static final String CONDITION_SEPARATOR = "+";
+    private static final String HEADER_VALUE_SEPARATOR = "=";
 
-    // set headernames and values
-    public void init() throws MessagingException {
-        StringTokenizer st = new StringTokenizer(getCondition(), "+");
-        conditionline_ = new LinkedList<String>();
+    private interface HeaderCondition {
+        boolean isMatching(MimeMessage mimeMessage) throws MessagingException;
+    }
+
+    private static class HeaderNameCondition implements HeaderCondition {
+        private final String headerName;
+
+        public HeaderNameCondition(String headerName) {
+            this.headerName = headerName;
+        }
 
-        // separates the headernames from the matchline
-        while (st.hasMoreTokens()) {
-            String condition = st.nextToken().trim();
-            conditionline_.add(condition);
+        @Override
+        public boolean isMatching(MimeMessage mimeMessage) throws 
MessagingException {
+            String[] headerArray = mimeMessage.getHeader(headerName);
+            return headerArray != null && headerArray.length > 0;
         }
     }
 
-    public Collection<MailAddress> match(Mail mail) throws 
javax.mail.MessagingException {
-        boolean match = false;
-        MimeMessage message = mail.getMessage();
-
-        for (String element : conditionline_) {
-            StringTokenizer st = new StringTokenizer(element, "=", false);
-            String header;
-
-            // read the headername
-            if (st.hasMoreTokens()) {
-                header = st.nextToken().trim();
-            } else {
-                throw new MessagingException("Missing headerName");
-            }
+    private static class HeaderValueCondition implements HeaderCondition {
+        private final String headerName;
+        private final String headerValue;
 
-            // try to read headervalue
-            String headerValue;
-            if (st.hasMoreTokens()) {
-                headerValue = st.nextToken().trim();
-            } else {
-                headerValue = null;
-            }
+        public HeaderValueCondition(String headerName, String headerValue) {
+            this.headerName = headerName;
+            this.headerValue = headerValue;
+        }
 
-            // find headername in Mailheaders
-            String[] headerArray = message.getHeader(header);
+        @Override
+        public boolean isMatching(MimeMessage mimeMessage) throws 
MessagingException {
+            String[] headerArray = mimeMessage.getHeader(headerName);
             if (headerArray != null && headerArray.length > 0) {
-                // if there is the headername specified without the headervalue
-                // only the existence of the headername ist performed
-                if (headerValue != null) {
-                    //
-                    if (headerArray[0].trim().equalsIgnoreCase(headerValue)) {
-                        // headername and value found and match to the 
condition
-                        match = true;
-                    } else {
-                        // headername and value found but the value does not 
match the condition
-                        match = false;
-                        // if one condition fails the matcher returns false
-                        break;
+                for (String value : headerArray) {
+                    if (headerValue.equals(value)) {
+                        return true;
                     }
-                } else {
-                    // just the headername is specified
-                    match = true;
                 }
-            } else {
-                // no headername is found
-                match = false;
-                // if one condition fails the matcher returns false
-                break;
             }
+            return false;
+        }
+    }
+
+    private List<HeaderCondition> headerConditions;
+
+    public void init() throws MessagingException {
+        headerConditions = new ArrayList<HeaderCondition>();
+        StringTokenizer conditionTokenizer = new 
StringTokenizer(getCondition(), CONDITION_SEPARATOR);
+        while (conditionTokenizer.hasMoreTokens()) {
+            
headerConditions.add(parseHeaderCondition(conditionTokenizer.nextToken().trim()));
         }
+    }
 
-        return (match) ? mail.getRecipients() : null;
+    private HeaderCondition parseHeaderCondition(String element) throws 
MessagingException {
+        StringTokenizer valueSeparatorTokenizer = new StringTokenizer(element, 
HEADER_VALUE_SEPARATOR, false);
+        if (!valueSeparatorTokenizer.hasMoreElements()) {
+            throw new MessagingException("Missing headerName");
+        }
+        String headerName = valueSeparatorTokenizer.nextToken().trim();
+        if (valueSeparatorTokenizer.hasMoreTokens()) {
+           return new HeaderValueCondition(headerName, 
valueSeparatorTokenizer.nextToken().trim());
+        } else {
+            return new HeaderNameCondition(headerName);
+        }
+    }
 
+    public Collection<MailAddress> match(Mail mail) throws 
javax.mail.MessagingException {
+        for (HeaderCondition headerCondition : headerConditions) {
+            if (!headerCondition.isMatching(mail.getMessage())) {
+                return null;
+            }
+        }
+        return mail.getRecipients();
     }
 } 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to