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