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.commands;
21  
22  import org.apache.jsieve.Arguments;
23  import org.apache.jsieve.Block;
24  import org.apache.jsieve.SieveContext;
25  import org.apache.jsieve.exception.SieveException;
26  import org.apache.jsieve.mail.MailAdapter;
27  
28  /**
29   * Class If implements the If Command as defined in RFC 3028, section 3.1.
30   */
31  public class If extends AbstractConditionalCommand {
32      /**
33       * Constructor for If.
34       */
35      public If() {
36          super();
37      }
38  
39      /**
40       * <p>
41       * Conditionally eexecute a Block if an If Condition is allowed and
42       * runnable.
43       * </p>
44       * <p>
45       * Also,
46       * 
47       * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(MailAdapter,
48       *      Arguments, Block, SieveContext)
49       *      </p>
50       */
51      protected Object executeBasic(MailAdapter mail, Arguments arguments,
52              Block block, SieveContext context) throws SieveException {
53          // Check Syntax
54          if (!context.getConditionManager().isIfAllowed())
55              throw context.getCoordinate().commandException(
56                      "Unexpected Command: \"if\".");
57  
58          // Check Runnable
59          if (!context.getConditionManager().isIfRunnable())
60              return Boolean.FALSE;
61  
62          // Run the tests
63          final boolean isTestPassed = arguments.getTestList().allTestsPass(mail,context);
64  
65          // If the tests answered TRUE, execute the Block
66          if (isTestPassed)
67              execute(mail, block, context);
68  
69          // Update the ConditionManager
70          context.getConditionManager().setIfTestResult(isTestPassed);
71  
72          // Return the result
73          return isTestPassed;
74      }
75  
76      /**
77       * @see org.apache.jsieve.commands.AbstractCommand#validateArguments(Arguments,
78       *      SieveContext)
79       */
80      protected void validateArguments(Arguments arguments, SieveContext context)
81              throws SieveException {
82          if (!arguments.hasTests())
83              throw context.getCoordinate().syntaxException("Expecting a Test");
84      }
85  
86  }