KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > CompareNumericHeaderValue


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

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 JavaDoc;
25
26 import java.util.Collection JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.mail.*;
30 import javax.mail.internet.*;
31
32 /**
33  * <P>Matches mails containing a header with a numeric value whose comparison with the specified value is true.
34  * If the header is missing in the message, there will be <I>no match</I></P>
35  * <P>Configuration string: The headerName, a comparison operator and the numeric headerValue
36  * to compare with, <I>space or tab delimited</I>.</P>
37  * <P>The comparison operators are: <CODE>&lt, &lt=, ==, &gt=, &gt</CODE>;
38  * another set of operators is: <CODE>LT, LE, EQ, GE, GT</CODE>.
39  * Also the following operators are accepted: <CODE>=&lt, =, =&gt</CODE>.</P>
40  * <P>Example:</P>
41  * <PRE><CODE>
42  * &lt;mailet match="CompareNumericHeaderValue=X-MessageIsSpamProbability > 0.9" class="ToProcessor"&gt;
43  * &lt;processor&gt; spam &lt;/processor&gt;
44  * &lt;/mailet&gt;
45  * </CODE></PRE>
46  *
47  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:21 $
48  * @since 2.2.0
49  */

50 public class CompareNumericHeaderValue extends GenericMatcher {
51
52     private String JavaDoc 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 JavaDoc headerValue;
62
63     public void init() throws MessagingException {
64         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc 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 JavaDoc headerValueString = st.nextToken().trim();
105             try {
106                 headerValue = Double.valueOf(headerValueString);
107             }
108             catch (NumberFormatException JavaDoc 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 JavaDoc match(Mail mail) throws MessagingException {
119         if (headerName == null) {
120             // should never get here
121
throw new IllegalStateException JavaDoc("Null headerName");
122         }
123         
124         MimeMessage message = (MimeMessage) mail.getMessage();
125         
126         String JavaDoc [] 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                         // should never get here
158
throw new IllegalStateException JavaDoc("Unknown comparisonOperator" + comparisonOperator);
159                 }
160             }
161             catch (NumberFormatException JavaDoc 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