View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context.task;
21  
22  import org.waarp.common.command.exception.Reply550Exception;
23  import org.waarp.common.file.FileUtils;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.openr66.context.R66Session;
27  
28  import java.io.File;
29  
30  /**
31   * Move and Rename the current file
32   */
33  public class MoveRenameTask extends AbstractTask {
34    /**
35     * Internal Logger
36     */
37    private static final WaarpLogger logger =
38        WaarpLoggerFactory.getLogger(MoveRenameTask.class);
39  
40    /**
41     * @param argRule
42     * @param delay
43     * @param argTransfer
44     * @param session
45     */
46    public MoveRenameTask(final String argRule, final int delay,
47                          final String argTransfer, final R66Session session) {
48      super(TaskType.MOVERENAME, delay, argRule, argTransfer, session);
49    }
50  
51    @Override
52    public final void run() {
53      String finalname = argRule;
54      finalname = getReplacedValue(finalname, BLANK.split(argTransfer)).trim()
55                                                                       .replace(
56                                                                           '\\',
57                                                                           '/');
58      logger.debug("Move and Rename to {} with {}:{} and {}", finalname, argRule,
59                   argTransfer, session);
60      final File from = session.getFile().getTrueFile();
61      final File to = new File(finalname);
62      try {
63        FileUtils.copy(from, to, true, false);
64      } catch (final Reply550Exception e) {
65        logger.error(
66            "Move and Rename to " + finalname + " with " + argRule + ':' +
67            argTransfer + " and " + session + ": {}", e.getMessage());
68        futureCompletion.setFailure(e);
69        return;
70      }
71      session.getRunner().setFileMoved(finalname, true);
72      futureCompletion.setSuccess();
73    }
74  
75  }