View Javadoc

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.Iterator;
23  import java.util.List;
24  
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          Iterator headerNamesIter = ((StringListArgument) arguments
51                  .getArgumentList().get(0)).getList().iterator();
52  
53          boolean found = true;
54          while (found && headerNamesIter.hasNext()) {
55              List headers = mail.getMatchingHeader((String) headerNamesIter
56                      .next());
57              found = found && !headers.isEmpty();
58          }
59          return found;
60      }
61  
62      /**
63       * @see org.apache.jsieve.tests.AbstractTest#validateArguments(Arguments,
64       *      SieveContext)
65       */
66      protected void validateArguments(Arguments arguments, SieveContext context)
67              throws SieveException {
68          List argumentsList = arguments.getArgumentList();
69          if (1 != argumentsList.size())
70              throw context.getCoordinate().syntaxException(
71                      "Expecting exactly one argument");
72  
73          if (!(argumentsList.get(0) instanceof StringListArgument))
74              throw context.getCoordinate().syntaxException(
75                      "Expecting a StringList");
76  
77          if (arguments.hasTests())
78              throw context.getCoordinate().syntaxException(
79                      "Found unexpected tests");
80      }
81  
82  }