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