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