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 java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.jsieve.Arguments;
26  import org.apache.jsieve.Block;
27  import org.apache.jsieve.SieveContext;
28  import org.apache.jsieve.StringListArgument;
29  import org.apache.jsieve.exception.FeatureException;
30  import org.apache.jsieve.exception.LookupException;
31  import org.apache.jsieve.exception.SieveException;
32  import org.apache.jsieve.mail.MailAdapter;
33  
34  /**
35   * Class Require implements the Require Command as defined in RFC 3028, section
36   * 3.2.
37   */
38  public class Require extends AbstractPrologCommand {
39  
40      /**
41       * Constructor for Require.
42       */
43      public Require() {
44          super();
45      }
46  
47      /**
48       * <p>
49       * Ensure the required feature is configured.
50       * </p>
51       * <p>
52       * Also,
53       * 
54       * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(MailAdapter,
55       *      Arguments, Block, SieveContext)
56       *      </p>
57       */
58      protected Object executeBasic(MailAdapter mail, Arguments arguments,
59              Block block, SieveContext context) throws SieveException {
60          Iterator stringsIter = ((StringListArgument) arguments
61                  .getArgumentList().get(0)).getList().iterator();
62  
63          while (stringsIter.hasNext()) {
64              validateFeature((String) stringsIter.next(), mail, context);
65          }
66          return null;
67      }
68  
69      /**
70       * Method validateFeature validates the required feature is configured as
71       * either a Command or a Test.
72       * 
73       * @param name
74       * @param mail
75       * @param context
76       *            TODO
77       * @throws FeatureException
78       */
79      protected void validateFeature(String name, MailAdapter mail,
80              SieveContext context) throws FeatureException {
81          // Validate as a Command
82          try {
83              validateCommand(name, context);
84              return;
85          } catch (LookupException e) {
86              // Not a command
87          }
88  
89          // Validate as a Test
90          try {
91              validateTest(name, context);
92          } catch (LookupException e) {
93              throw new FeatureException("Feature \"" + name
94                      + "\" is not supported.");
95          }
96      }
97  
98      /**
99       * Method validateCommand.
100      * 
101      * @param name
102      * @throws LookupException
103      */
104     protected void validateCommand(String name, SieveContext context)
105             throws LookupException {
106         context.getCommand(name);
107     }
108 
109     /**
110      * Method validateTest.
111      * 
112      * @param name
113      * @param context
114      *            TODO
115      * @throws LookupException
116      */
117     protected void validateTest(String name, SieveContext context)
118             throws LookupException {
119         context.getTest(name);
120     }
121 
122     /**
123      * @see org.apache.jsieve.commands.AbstractCommand#validateArguments(Arguments,
124      *      SieveContext)
125      */
126     protected void validateArguments(Arguments arguments, SieveContext context)
127             throws SieveException {
128         List args = arguments.getArgumentList();
129         if (args.size() != 1)
130             throw context.getCoordinate().syntaxException(
131                     "Exactly 1 argument permitted. Found " + args.size());
132 
133         Object argument = args.get(0);
134         if (!(argument instanceof StringListArgument))
135             throw context.getCoordinate().syntaxException(
136                     "Expecting a string-list");
137     }
138 
139 }