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.List;
23
24 import org.apache.jsieve.Arguments;
25 import org.apache.jsieve.SieveContext;
26 import org.apache.jsieve.StringListArgument;
27 import org.apache.jsieve.exception.CommandException;
28 import org.apache.jsieve.exception.SieveException;
29
30 /**
31 * Abstract class AbstractActionCommand defines the common state validation and
32 * state update behavior for Action Commands as per RFC 3028, section 8.
33 */
34 public abstract class AbstractActionCommand extends AbstractBodyCommand {
35
36 /**
37 * Constructor for AbstractActionCommand.
38 */
39 public AbstractActionCommand() {
40 super();
41 }
42
43 /**
44 * <p>
45 * Method updateState() updates the CommandStateManager to indicate an
46 * Action Command has been processed and to cancel implicit keep.
47 * </p>
48 *
49 * <p>
50 * And also
51 * </p>
52 *
53 * @see org.apache.jsieve.commands.AbstractCommand#updateState(SieveContext)
54 */
55 protected void updateState(SieveContext context) {
56 super.updateState(context);
57 context.getCommandStateManager().setHasActions(true);
58 context.getCommandStateManager().setImplicitKeep(false);
59 }
60
61 /**
62 * <p>
63 * Method validateState() validates via the CommandStateManager that an
64 * Action Command is legal at this time.
65 * </p>
66 *
67 * <p>
68 * Also,
69 * </p>
70 *
71 * @see org.apache.jsieve.commands.AbstractCommand#validateState(SieveContext)
72 */
73 protected void validateState(SieveContext context) throws CommandException {
74 if (context.getCommandStateManager().isRejected())
75 throw context.getCoordinate().commandException(
76 "Cannot perform Actions on a rejected message.");
77 }
78
79 /**
80 * This is an utility method for subclasses
81 *
82 * @see org.apache.jsieve.commands.Redirect
83 * @see org.apache.jsieve.commands.optional.FileInto
84 * @see org.apache.jsieve.commands.optional.Reject
85 */
86 protected void validateSingleStringArguments(Arguments arguments,
87 SieveContext context) throws SieveException {
88 List args = arguments.getArgumentList();
89 if (args.size() != 1)
90 throw context.getCoordinate().syntaxException(
91 "Exactly 1 argument permitted. Found " + args.size());
92
93 Object argument = args.get(0);
94 if (!(argument instanceof StringListArgument))
95 throw context.getCoordinate().syntaxException(
96 "Expecting a string-list");
97
98 if (1 != ((StringListArgument) argument).getList().size())
99 throw context.getCoordinate().syntaxException(
100 "Expecting exactly one argument");
101 }
102
103 }