1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.jsieve.tests;
21
22 import java.util.List;
23
24 import org.apache.jsieve.Argument;
25 import org.apache.jsieve.Arguments;
26 import org.apache.jsieve.SieveContext;
27 import org.apache.jsieve.StringListArgument;
28 import org.apache.jsieve.exception.SieveException;
29 import org.apache.jsieve.mail.MailAdapter;
30
31 /**
32 * Class Exists implements the Exists Test as defined in RFC 3028, section 5.5.
33 */
34 public class Exists extends AbstractTest {
35
36 /**
37 * Constructor for Exists.
38 */
39 public Exists() {
40 super();
41 }
42
43 /**
44 * @see org.apache.jsieve.tests.AbstractTest#executeBasic(MailAdapter,
45 * Arguments, SieveContext)
46 */
47 protected boolean executeBasic(MailAdapter mail, Arguments arguments,
48 SieveContext context) throws SieveException {
49
50 final List<String> argumentList = ((StringListArgument) arguments
51 .getArgumentList().get(0)).getList();
52
53 boolean found = true;
54 for (final String arg:argumentList) {
55 List<String> headers = mail.getMatchingHeader(arg);
56 found = found && !headers.isEmpty();
57 if (!found) {
58 break;
59 }
60 }
61 return found;
62 }
63
64 /**
65 * @see org.apache.jsieve.tests.AbstractTest#validateArguments(Arguments,
66 * SieveContext)
67 */
68 protected void validateArguments(Arguments arguments, SieveContext context)
69 throws SieveException {
70 List<Argument> argumentsList = arguments.getArgumentList();
71 if (1 != argumentsList.size())
72 throw context.getCoordinate().syntaxException(
73 "Expecting exactly one argument");
74
75 if (!(argumentsList.get(0) instanceof StringListArgument))
76 throw context.getCoordinate().syntaxException(
77 "Expecting a StringList");
78
79 if (arguments.hasTests())
80 throw context.getCoordinate().syntaxException(
81 "Found unexpected tests");
82 }
83
84 }