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
21 package org.apache.jsieve.commands;
22
23 import org.apache.jsieve.Arguments;
24 import org.apache.jsieve.Block;
25 import org.apache.jsieve.CommandException;
26 import org.apache.jsieve.SieveContext;
27 import org.apache.jsieve.SieveException;
28 import org.apache.jsieve.mail.MailAdapter;
29
30 /***
31 * Abstract class AbstractCommand defines a framework of common behavior for
32 * Sieve Commands.
33 */
34 public abstract class AbstractCommand implements ExecutableCommand
35 {
36
37 /***
38 * Constructor for AbstractCommand.
39 */
40 public AbstractCommand()
41 {
42 super();
43 }
44
45 /***
46 * Framework method validateState is invoked before a Sieve Command is
47 * executed to validate its state. Subclass methods are expected to override
48 * or extend this method to perform their own validation as appropriate.
49 * @param context <code>SieveContext</code> giving contextual information,
50 * not null
51 * @throws CommandException
52 */
53 protected void validateState(SieveContext context)
54 throws CommandException
55 {
56 }
57
58 /***
59 * Framework method updateState is invoked after a Sieve Command has executed to
60 * update the Sieve state. Subclass methods are expected to override or extend
61 * this method to update state as appropriate.
62 */
63 protected void updateState()
64 {
65 }
66
67 /***
68 * Framework method validateArguments is invoked before a Sieve Command is
69 * executed to validate its arguments. Subclass methods are expected to override
70 * or extend this method to perform their own validation as appropriate.
71 *
72 * @param arguments
73 * @param context <code>SieveContext</code> giving contextual information,
74 * not null
75 * @throws SieveException
76 */
77 protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException
78 {
79 if (!arguments.getArgumentList().isEmpty())
80 throw context.getCoordinate().syntaxException("Found unexpected arguments");
81 }
82
83 /***
84 * Framework method validateBlock is invoked before a Sieve Command is
85 * executed to validate its Block. Subclass methods are expected to override
86 * or extend this method to perform their own validation as appropriate.
87 *
88 * @param block
89 * @param context <code>ScriptCoordinate</code> giving positional information,
90 * not null
91 * @throws SieveException
92 */
93 protected void validateBlock(Block block, SieveContext context)
94 throws SieveException
95 {
96 if (null != block)
97 throw context.getCoordinate().syntaxException("Found unexpected Block. Missing ';'?");
98 }
99
100 /***
101 * <p>Method execute executes a basic Sieve Command after first invoking framework
102 * methods to validate that Sieve is in a legal state to invoke the Command and
103 * that the Command arguments are legal. After invocation, a framework method is
104 * invoked to update the state.</p>
105 *
106 * <p>Also, @see org.apache.jsieve.Executable#execute()</p>
107 */
108 public Object execute(MailAdapter mail, Arguments arguments, Block block, SieveContext context)
109 throws SieveException
110 {
111 validateState(context);
112 validateArguments(arguments, context);
113 validateBlock(block, context);
114 Object result = executeBasic( mail, arguments, block, context);
115 updateState();
116 return result;
117 }
118
119 /***
120 * Abstract method executeBasic invokes a Sieve Command.
121 * @param mail
122 * @param arguments
123 * @param block
124 * @param context <code>SieveContext</code> giving contextual information,
125 * not null
126 * @return Object
127 * @throws SieveException
128 */
129 abstract protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context)
130 throws SieveException;
131
132 }