Hello Developer-List,
i had to rewrite an existing matcher (HasHeader) and found a TODO in the
source that would match my requirements.
Here my changes for your review.
-------------------------------------------------------------------------------
package org.apache.james.transport.matchers;
import org.apache.mailet.GenericMatcher;
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]
*
*/
public class HasHeaderWithValue extends GenericMatcher {
private LinkedList conditionline_ = new LinkedList();
public void init() throws MessagingException {
StringTokenizer st = new StringTokenizer(getCondition(), "+");
conditionline_ = new LinkedList();
while (st.hasMoreTokens()) {
String condition = st.nextToken().trim();
conditionline_.add(condition);
}
}
public Collection match(Mail mail) throws javax.mail.MessagingException
{
boolean match = false;
MimeMessage message = (MimeMessage) mail.getMessage();
header:
for (Iterator it=conditionline_.iterator(); it.hasNext(); ) {
String element = (String)it.next();
StringTokenizer st = new StringTokenizer(element, "=",
false);
String header = new String();
if (st.hasMoreTokens()) {
header = st.nextToken().trim();
} else {
throw new MessagingException("Missing
headerName");
}
String headerValue = new String();
if (st.hasMoreTokens()) {
headerValue = st.nextToken().trim();
} else {
headerValue = null;
}
String [] headerArray = message.getHeader(header);
if (headerArray != null && headerArray.length > 0) {
if (headerValue != null) {
if
(headerArray[0].trim().equalsIgnoreCase(headerValue)) {
match = true;
} else {
match = false;
break header;
};
} else {
match = true;
}
} else {
match = false;
break header;
}
}
return (match) ? mail.getRecipients() : null;
}
}
-------------------------------------------------------------------------------
best regards
Frank
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]