1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.matchers;
23
24 import org.apache.mailet.base.GenericMatcher;
25 import org.apache.mailet.Mail;
26
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.StringTokenizer;
31
32 import javax.mail.MessagingException;
33 import javax.mail.internet.MimeMessage;
34
35
36
37
38
39
40
41 public class HasHeader extends GenericMatcher {
42
43 private LinkedList conditionline_ = new LinkedList();
44
45
46 public void init() throws MessagingException {
47 StringTokenizer st = new StringTokenizer(getCondition(), "+");
48 conditionline_ = new LinkedList();
49
50
51 while (st.hasMoreTokens()) {
52 String condition = st.nextToken().trim();
53 conditionline_.add(condition);
54 }
55 }
56
57 public Collection match(Mail mail) throws javax.mail.MessagingException {
58 boolean match = false;
59 MimeMessage message = mail.getMessage();
60
61
62 header:
63
64 for (Iterator it=conditionline_.iterator(); it.hasNext(); ) {
65 String element = (String)it.next();
66 StringTokenizer st = new StringTokenizer(element, "=", false);
67 String header = new String();
68
69
70 if (st.hasMoreTokens()) {
71 header = st.nextToken().trim();
72 } else {
73 throw new MessagingException("Missing headerName");
74 }
75
76
77 String headerValue = new String();
78 if (st.hasMoreTokens()) {
79 headerValue = st.nextToken().trim();
80 } else {
81 headerValue = null;
82 }
83
84
85 String [] headerArray = message.getHeader(header);
86 if (headerArray != null && headerArray.length > 0) {
87
88
89 if (headerValue != null) {
90
91 if (headerArray[0].trim().equalsIgnoreCase(headerValue)) {
92
93 match = true;
94 } else {
95
96 match = false;
97
98 break header;
99 };
100 } else {
101
102 match = true;
103 }
104 } else {
105
106 match = false;
107
108 break header;
109 }
110 }
111
112 return (match) ? mail.getRecipients() : null;
113
114 }
115 }
116