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.optional;
21
22 import org.apache.jsieve.Arguments;
23 import org.apache.jsieve.Block;
24 import org.apache.jsieve.SieveContext;
25 import org.apache.jsieve.StringListArgument;
26 import org.apache.jsieve.commands.AbstractActionCommand;
27 import org.apache.jsieve.exception.SieveException;
28 import org.apache.jsieve.mail.Action;
29 import org.apache.jsieve.mail.ActionFileInto;
30 import org.apache.jsieve.mail.MailAdapter;
31
32 /**
33 * Class FileInto implements the FileInto Command as defined in RFC 3028,
34 * section 4.2.
35 */
36 public class FileInto extends AbstractActionCommand {
37
38 /**
39 * Constructor for Require.
40 */
41 public FileInto() {
42 super();
43 }
44
45 /**
46 * <p>
47 * Add an ActionFileInto to the List of Actions to be performed passing the
48 * sole StringList argument as the destination. RFC 3028 mandates that there
49 * should be only one FileInto per destination. If this is a duplicate, this
50 * Command is silently ignored.
51 * </p>
52 * <p>
53 * Also,
54 *
55 * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(MailAdapter,
56 * Arguments, Block, SieveContext)
57 * </p>
58 */
59 protected Object executeBasic(MailAdapter mail, Arguments arguments,
60 Block block, SieveContext context) throws SieveException {
61 final String destination = ((StringListArgument) arguments
62 .getArgumentList().get(0)).getList().get(0);
63
64 // Only one fileinto per destination allowed, others should be
65 // discarded
66 boolean isDuplicate = false;
67 for (final Action action: mail.getActions()) {
68 isDuplicate = (action instanceof ActionFileInto)
69 && (((ActionFileInto) action).getDestination()
70 .equals(destination));
71 if (isDuplicate) {
72 break;
73 }
74 }
75
76 if (!isDuplicate)
77 mail.addAction(new ActionFileInto(destination));
78
79 return null;
80 }
81
82 /**
83 * @see org.apache.jsieve.commands.AbstractCommand#validateArguments(Arguments,
84 * SieveContext)
85 */
86 protected void validateArguments(Arguments arguments, SieveContext context)
87 throws SieveException {
88 validateSingleStringArguments(arguments, context);
89 }
90
91 }