Author: rdonkin
Date: Tue Feb 26 14:58:32 2008
New Revision: 631419

URL: http://svn.apache.org/viewvc?rev=631419&view=rev
Log:
JAMES-836 The HasHeader-Matcher should check for headername and value. 
Contributed by Frank Fitzke https://issues.apache.org/jira/browse/JAMES-836.

Modified:
    
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/matchers/HasHeader.java

Modified: 
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/matchers/HasHeader.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/matchers/HasHeader.java?rev=631419&r1=631418&r2=631419&view=diff
==============================================================================
--- 
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/matchers/HasHeader.java
 (original)
+++ 
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/matchers/HasHeader.java
 Tue Feb 26 14:58:32 2008
@@ -25,21 +25,92 @@
 import org.apache.mailet.Mail;
 
 import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.StringTokenizer;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 
 /**
- * use: <mailet match="HasHeader=<header>" class="..." />
- *
- * This matcher simply checks to see if the header named is present.
- * If complements the AddHeader mailet.
- *
- * TODO: support lists of headers and values, e.g, match="{<header>[=value]}+"
- *       [will require a complete rewrite from the current trivial one-liner]
- *
+ * use: <mailet match="{<header>[=value]}+" class="..." />
+ * 
+ * This matcher checks if the header named is present. If complements the
+ * AddHeader mailet.
  */
 public class HasHeader extends GenericMatcher {
 
+    private LinkedList conditionline_ = new LinkedList();
+
+    // set headernames and values
+    public void init() throws MessagingException {
+        StringTokenizer st = new StringTokenizer(getCondition(), "+");
+        conditionline_ = new LinkedList();
+
+        // separates the headernames from the matchline
+        while (st.hasMoreTokens()) {
+            String condition = st.nextToken().trim();
+            conditionline_.add(condition);
+        }
+    }
+
     public Collection match(Mail mail) throws javax.mail.MessagingException {
-        return (mail.getMessage().getHeader(getCondition(), null) != null) ? 
mail.getRecipients() : null;
+        boolean match = false;
+        MimeMessage message = (MimeMessage) mail.getMessage();
+
+
+        header:
+            // goes through the headernames one by one
+            for (Iterator it=conditionline_.iterator(); it.hasNext(); ) {
+                String element = (String)it.next();
+                StringTokenizer st = new StringTokenizer(element, "=", false);
+                String header = new String();
+
+                // read the headername
+                if (st.hasMoreTokens()) {
+                    header = st.nextToken().trim();
+                } else {
+                    throw new MessagingException("Missing headerName");
+                }
+
+                // try to read headervalue
+                String headerValue = new String();
+                if (st.hasMoreTokens()) {
+                    headerValue = st.nextToken().trim();
+                } else {
+                    headerValue = null;
+                }
+
+                // find headername in Mailheaders
+                String [] headerArray = message.getHeader(header);
+                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 header;
+                        };
+                    } else {
+                        // just the headername is specified
+                        match = true;
+                    }
+                } else {
+                    // no headername is found
+                    match = false;
+                    // if one condition fails the matcher returns false
+                    break header;
+                }
+            }
+
+        return (match) ? mail.getRecipients() : null;
+
     }
-}
+} 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to