1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.tests;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.ListIterator;
26
27 import org.apache.jsieve.Arguments;
28 import org.apache.jsieve.SieveContext;
29 import org.apache.jsieve.StringListArgument;
30 import org.apache.jsieve.TagArgument;
31 import org.apache.jsieve.comparators.ComparatorNames;
32 import org.apache.jsieve.comparators.ComparatorUtils;
33 import org.apache.jsieve.comparators.MatchTypeTags;
34 import org.apache.jsieve.exception.SieveException;
35 import org.apache.jsieve.mail.MailAdapter;
36
37
38
39
40 public class Header extends AbstractTest implements ComparatorTags,
41 MatchTypeTags, ComparatorNames {
42
43
44
45
46 public Header() {
47 super();
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 protected boolean executeBasic(MailAdapter mail, Arguments arguments,
68 SieveContext context) throws SieveException {
69 String comparator = null;
70 String matchType = null;
71 List headerNames = null;
72 List keys = null;
73
74 ListIterator argumentsIter = arguments.getArgumentList().listIterator();
75 boolean stop = false;
76
77
78 while (!stop && argumentsIter.hasNext()) {
79 Object argument = argumentsIter.next();
80 if (argument instanceof TagArgument) {
81 String tag = ((TagArgument) argument).getTag();
82
83 if (null == comparator && tag.equals(COMPARATOR_TAG)) {
84
85 if (argumentsIter.hasNext()) {
86 argument = argumentsIter.next();
87 if (argument instanceof StringListArgument) {
88 List stringList = ((StringListArgument) argument)
89 .getList();
90 if (stringList.size() != 1)
91 throw context.getCoordinate().syntaxException(
92 "Expecting exactly one String");
93 comparator = (String) stringList.get(0);
94 } else
95 throw context.getCoordinate().syntaxException(
96 "Expecting a StringList");
97 }
98 }
99
100 else if (null == matchType
101 && (tag.equals(IS_TAG) || tag.equals(CONTAINS_TAG) || tag
102 .equals(MATCHES_TAG)))
103 matchType = tag;
104 else
105 throw context.getCoordinate().syntaxException(
106 "Found unexpected TagArgument: \"" + tag + "\"");
107 } else {
108
109 argumentsIter.previous();
110 stop = true;
111 }
112 }
113
114
115 if (argumentsIter.hasNext()) {
116 Object argument = argumentsIter.next();
117 if (argument instanceof StringListArgument)
118 headerNames = ((StringListArgument) argument).getList();
119 }
120 if (null == headerNames)
121 throw context.getCoordinate().syntaxException(
122 "Expecting a StringListof header names");
123
124
125 if (argumentsIter.hasNext()) {
126 Object argument = argumentsIter.next();
127 if (argument instanceof StringListArgument)
128 keys = ((StringListArgument) argument).getList();
129 }
130 if (null == keys)
131 throw context.getCoordinate().syntaxException(
132 "Expecting a StringList of keys");
133
134 if (argumentsIter.hasNext())
135 throw context.getCoordinate().syntaxException(
136 "Found unexpected arguments");
137
138 return match(mail, (comparator == null ? ASCII_CASEMAP_COMPARATOR
139 : comparator), (matchType == null ? IS_TAG : matchType),
140 headerNames, keys, context);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 protected boolean match(MailAdapter mail, String comparator,
157 String matchType, List headerNames, List keys, SieveContext context)
158 throws SieveException {
159
160 boolean isMatched = false;
161 Iterator headerNamesIter = headerNames.iterator();
162 while (!isMatched && headerNamesIter.hasNext()) {
163 isMatched = match(comparator, matchType, mail
164 .getMatchingHeader((String) headerNamesIter.next()), keys,
165 context);
166 }
167 return isMatched;
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 protected boolean match(String comparator, String matchType,
183 List headerValues, List keys, SieveContext context)
184 throws SieveException {
185
186
187
188
189
190 if (headerValues.isEmpty())
191 if (matchType.equals(CONTAINS_TAG)) {
192
193 headerValues = new ArrayList(headerValues);
194 headerValues.add("");
195 } else {
196 return false;
197 }
198
199 boolean isMatched = false;
200 Iterator headerValuesIter = headerValues.iterator();
201 while (!isMatched && headerValuesIter.hasNext()) {
202 isMatched = match(comparator, matchType, (String) headerValuesIter
203 .next(), keys, context);
204 }
205 return isMatched;
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220 protected boolean match(String comparator, String matchType,
221 String headerValue, List keys, SieveContext context)
222 throws SieveException {
223
224 boolean isMatched = false;
225 Iterator keysIter = keys.iterator();
226 while (!isMatched && keysIter.hasNext()) {
227 final String nextKey = (String) keysIter.next();
228 isMatched = ComparatorUtils.match(comparator, matchType,
229 headerValue, nextKey, context);
230 }
231 return isMatched;
232 }
233
234
235
236
237
238 protected void validateArguments(Arguments arguments, SieveContext context)
239 throws SieveException {
240 if (arguments.hasTests())
241 throw context.getCoordinate().syntaxException(
242 "Found unexpected tests");
243 }
244
245 }