FileDocCategorySizeDatePackage
CompareNumericHeaderValue.javaAPI DocApache James 2.3.17001Fri Jan 12 12:56:32 GMT 2007org.apache.james.transport.matchers

CompareNumericHeaderValue

public class CompareNumericHeaderValue extends org.apache.mailet.GenericMatcher

Matches mails containing a header with a numeric value whose comparison with the specified value is true. If the header is missing in the message, there will be no match

Configuration string: The headerName, a comparison operator and the numeric headerValue to compare with, space or tab delimited.

The comparison operators are: <, <=, ==, >=, >; another set of operators is: LT, LE, EQ, GE, GT. Also the following operators are accepted: =<, =, =>.

Example:


<mailet match="CompareNumericHeaderValue=X-MessageIsSpamProbability > 0.9" class="ToProcessor">
<processor> spam </processor>
</mailet>
version
CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
since
2.2.0

Fields Summary
private String
headerName
private int
comparisonOperator
private static final int
LT
private static final int
LE
private static final int
EQ
private static final int
GE
private static final int
GT
private Double
headerValue
Constructors Summary
Methods Summary
public voidinit()


         
        StringTokenizer st = new StringTokenizer(getCondition(), " \t", false);
        if (st.hasMoreTokens()) {
            headerName = st.nextToken().trim();
        }
        else {
            throw new MessagingException("Missing headerName");
        }
        if (st.hasMoreTokens()) {
            String comparisonOperatorString = st.nextToken().trim();
            if (comparisonOperatorString.equals("<")
                || comparisonOperatorString.equals("LT")) {
                comparisonOperator = LT;
            }
            else if (comparisonOperatorString.equals("<=")
                     || comparisonOperatorString.equals("=<")
                     || comparisonOperatorString.equals("LE")) {
                comparisonOperator = LE;
            }
            else if (comparisonOperatorString.equals("==")
                     || comparisonOperatorString.equals("=")
                     || comparisonOperatorString.equals("EQ")) {
                comparisonOperator = EQ;
            }
            else if (comparisonOperatorString.equals(">=")
                     || comparisonOperatorString.equals("=>")
                     || comparisonOperatorString.equals("GE")) {
                comparisonOperator = GE;
            }
            else if (comparisonOperatorString.equals(">")
                     || comparisonOperatorString.equals("GT")) {
                comparisonOperator = GT;
            }
            else {
                throw new MessagingException("Bad comparisonOperator: \"" + comparisonOperatorString + "\"");
            }
        }
        else {
            throw new MessagingException("Missing comparisonOperator");
        }
        if (st.hasMoreTokens()) {
            String headerValueString = st.nextToken().trim();
            try {
                headerValue = Double.valueOf(headerValueString);
            }
            catch (NumberFormatException nfe) {
                throw new MessagingException("Bad header comparison value: \""
                                             + headerValueString + "\"", nfe);
            }
        }
        else {
            throw new MessagingException("Missing headerValue threshold");
        }
    
public java.util.Collectionmatch(org.apache.mailet.Mail mail)

        if (headerName == null) {
            // should never get here
            throw new IllegalStateException("Null headerName");
        }
        
        MimeMessage message = (MimeMessage) mail.getMessage();
        
        String [] headerArray = message.getHeader(headerName);
        if (headerArray != null && headerArray.length > 0) {
            try {
                int comparison = Double.valueOf(headerArray[0].trim()).compareTo(headerValue);
                switch (comparisonOperator) {
                    case LT:
                        if (comparison < 0) {
                            return mail.getRecipients();
                        }
                        break;
                    case LE:
                        if (comparison <= 0) {
                            return mail.getRecipients();
                        }
                        break;
                    case EQ:
                        if (comparison == 0) {
                            return mail.getRecipients();
                        }
                        break;
                    case GE:
                        if (comparison >= 0) {
                            return mail.getRecipients();
                        }
                        break;
                    case GT:
                        if (comparison > 0) {
                            return mail.getRecipients();
                        }
                        break;
                    default:
                        // should never get here
                        throw new IllegalStateException("Unknown comparisonOperator" + comparisonOperator);
                }
            }
            catch (NumberFormatException nfe) {
                throw new MessagingException("Bad header value found in message: \"" + headerArray[0] + "\"", nfe);
            }
        }
        
        return null;