1 17 18 package org.apache.james.transport.matchers; 19 20 import org.apache.mailet.GenericMatcher; 21 import org.apache.mailet.Mail; 22 import org.apache.mailet.MailAddress; 23 24 import java.lang.NumberFormatException ; 25 26 import java.util.Collection ; 27 import java.util.StringTokenizer ; 28 29 import javax.mail.*; 30 import javax.mail.internet.*; 31 32 50 public class CompareNumericHeaderValue extends GenericMatcher { 51 52 private String headerName = null; 53 54 private int comparisonOperator; 55 private final static int LT = -2; 56 private final static int LE = -1; 57 private final static int EQ = 0; 58 private final static int GE = +1; 59 private final static int GT = +2; 60 61 private Double headerValue; 62 63 public void init() throws MessagingException { 64 StringTokenizer st = new StringTokenizer (getCondition(), " \t", false); 65 if (st.hasMoreTokens()) { 66 headerName = st.nextToken().trim(); 67 } 68 else { 69 throw new MessagingException("Missing headerName"); 70 } 71 if (st.hasMoreTokens()) { 72 String comparisonOperatorString = st.nextToken().trim(); 73 if (comparisonOperatorString.equals("<") 74 || comparisonOperatorString.equals("LT")) { 75 comparisonOperator = LT; 76 } 77 else if (comparisonOperatorString.equals("<=") 78 || comparisonOperatorString.equals("=<") 79 || comparisonOperatorString.equals("LE")) { 80 comparisonOperator = LE; 81 } 82 else if (comparisonOperatorString.equals("==") 83 || comparisonOperatorString.equals("=") 84 || comparisonOperatorString.equals("EQ")) { 85 comparisonOperator = EQ; 86 } 87 else if (comparisonOperatorString.equals(">=") 88 || comparisonOperatorString.equals("=>") 89 || comparisonOperatorString.equals("GE")) { 90 comparisonOperator = GE; 91 } 92 else if (comparisonOperatorString.equals(">") 93 || comparisonOperatorString.equals("GT")) { 94 comparisonOperator = GT; 95 } 96 else { 97 throw new MessagingException("Bad comparisonOperator: \"" + comparisonOperatorString + "\""); 98 } 99 } 100 else { 101 throw new MessagingException("Missing comparisonOperator"); 102 } 103 if (st.hasMoreTokens()) { 104 String headerValueString = st.nextToken().trim(); 105 try { 106 headerValue = Double.valueOf(headerValueString); 107 } 108 catch (NumberFormatException nfe) { 109 throw new MessagingException("Bad header comparison value: \"" 110 + headerValueString + "\"", nfe); 111 } 112 } 113 else { 114 throw new MessagingException("Missing headerValue threshold"); 115 } 116 } 117 118 public Collection match(Mail mail) throws MessagingException { 119 if (headerName == null) { 120 throw new IllegalStateException ("Null headerName"); 122 } 123 124 MimeMessage message = (MimeMessage) mail.getMessage(); 125 126 String [] headerArray = message.getHeader(headerName); 127 if (headerArray != null && headerArray.length > 0) { 128 try { 129 int comparison = Double.valueOf(headerArray[0].trim()).compareTo(headerValue); 130 switch (comparisonOperator) { 131 case LT: 132 if (comparison < 0) { 133 return mail.getRecipients(); 134 } 135 break; 136 case LE: 137 if (comparison <= 0) { 138 return mail.getRecipients(); 139 } 140 break; 141 case EQ: 142 if (comparison == 0) { 143 return mail.getRecipients(); 144 } 145 break; 146 case GE: 147 if (comparison >= 0) { 148 return mail.getRecipients(); 149 } 150 break; 151 case GT: 152 if (comparison > 0) { 153 return mail.getRecipients(); 154 } 155 break; 156 default: 157 throw new IllegalStateException ("Unknown comparisonOperator" + comparisonOperator); 159 } 160 } 161 catch (NumberFormatException nfe) { 162 throw new MessagingException("Bad header value found in message: \"" + headerArray[0] + "\"", nfe); 163 } 164 } 165 166 return null; 167 } 168 } 169 | Popular Tags |