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