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.ListIterator;
23  
24  import org.apache.jsieve.Argument;
25  import org.apache.jsieve.Arguments;
26  import org.apache.jsieve.NumberArgument;
27  import org.apache.jsieve.SieveContext;
28  import org.apache.jsieve.TagArgument;
29  import org.apache.jsieve.exception.SieveException;
30  import org.apache.jsieve.exception.SyntaxException;
31  import org.apache.jsieve.mail.MailAdapter;
32  import org.apache.jsieve.mail.SieveMailException;
33  
34  /**
35   * Class Size implements the Size Test as defined in RFC 3028, section 5.9.
36   */
37  public class Size extends AbstractTest {
38  
39      /**
40       * Constructor for Size.
41       */
42      public Size() {
43          super();
44      }
45  
46      /**
47       * @see org.apache.jsieve.tests.AbstractTest#executeBasic(MailAdapter,
48       *      Arguments, SieveContext)
49       *      <p>
50       *      From RFC 3028, Section 5.9...
51       *      </p>
52       *      <code>  
53       *    Syntax: size &lt;&quote;:over"&quote; / &quote;:under&quote;&gt; &lt;limit: number&gt;
54       * </code>
55       */
56      protected boolean executeBasic(MailAdapter mail, Arguments arguments,
57              SieveContext context) throws SyntaxException, SieveMailException {
58          String comparator = null;
59          Integer size = null;
60          ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
61  
62          // First argument MUST be a tag of ":under" or ":over"
63          if (argumentsIter.hasNext()) {
64              Argument argument = argumentsIter.next();
65              if (argument instanceof TagArgument) {
66                  final String tag = ((TagArgument) argument).getTag();
67                  if (tag.equals(":under") || tag.equals(":over"))
68                      comparator = tag;
69                  else
70                      throw context.getCoordinate().syntaxException(
71                              new StringBuilder("Found unexpected TagArgument: \"").append(tag).append("\""));
72              }
73          }
74          if (null == comparator)
75              throw context.getCoordinate().syntaxException("Expecting a Tag");
76  
77          // Second argument MUST be a number
78          if (argumentsIter.hasNext()) {
79              final Argument argument = argumentsIter.next();
80              if (argument instanceof NumberArgument)
81                  size = ((NumberArgument) argument).getInteger();
82          }
83          if (null == size)
84              throw context.getCoordinate().syntaxException("Expecting a Number");
85  
86          // There MUST NOT be any further arguments
87          if (argumentsIter.hasNext())
88              throw context.getCoordinate().syntaxException(
89                      "Found unexpected argument(s)");
90  
91          return test(mail, comparator, size.intValue());
92      }
93  
94      /**
95       * Method test.
96       * 
97       * @param mail
98       * @param comparator
99       * @param size
100      * @return boolean
101      * @throws SieveMailException
102      */
103     protected boolean test(MailAdapter mail, String comparator, int size)
104             throws SieveMailException {
105         boolean isTestPassed = false;
106         if (comparator.equals(":over"))
107             isTestPassed = testOver(mail, size);
108         else if (comparator.equals(":under"))
109             isTestPassed = testUnder(mail, size);
110         return isTestPassed;
111     }
112 
113     /**
114      * Method testUnder.
115      * 
116      * @param mail
117      * @param size
118      * @return boolean
119      * @throws SieveMailException
120      */
121     protected boolean testUnder(MailAdapter mail, int size)
122             throws SieveMailException {
123         return mail.getSize() < size;
124     }
125 
126     /**
127      * Method testOver.
128      * 
129      * @param mail
130      * @param size
131      * @return boolean
132      * @throws SieveMailException
133      */
134     protected boolean testOver(MailAdapter mail, int size)
135             throws SieveMailException {
136         return mail.getSize() > size;
137     }
138 
139     /**
140      * @see org.apache.jsieve.tests.AbstractTest#validateArguments(Arguments,
141      *      SieveContext)
142      */
143     protected void validateArguments(Arguments arguments, SieveContext context)
144             throws SieveException {
145         // All done in executeBasic()
146     }
147 
148 }