View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.core.command.service;
19  
20  import org.waarp.common.command.ReplyCode;
21  import org.waarp.common.command.exception.CommandAbstractException;
22  import org.waarp.common.command.exception.Reply501Exception;
23  import org.waarp.common.command.exception.Reply553Exception;
24  import org.waarp.ftp.core.command.AbstractCommand;
25  import org.waarp.ftp.core.exception.FtpNoFileException;
26  import org.waarp.ftp.core.exception.FtpNoTransferException;
27  import org.waarp.ftp.core.file.FtpFile;
28  
29  /**
30   * RNTO command
31   * 
32   * @author Frederic Bregier
33   * 
34   */
35  public class RNTO extends AbstractCommand {
36      @Override
37      public void exec() throws CommandAbstractException {
38          if (!hasArg()) {
39              invalidCurrentCommand();
40              throw new Reply501Exception("Need a pathname as argument");
41          }
42          String filename = getArg();
43          FtpFile file = null;
44          try {
45              file = getSession().getDataConn().getFtpTransferControl()
46                      .getExecutingFtpTransfer().getFtpFile();
47          } catch (FtpNoFileException e) {
48          } catch (FtpNoTransferException e) {
49          }
50          if (file != null) {
51              String previousName = file.getFile();
52              if (file.renameTo(filename)) {
53                  getSession().setReplyCode(
54                          ReplyCode.REPLY_250_REQUESTED_FILE_ACTION_OKAY,
55                          "\"" + filename + "\" as new file name for \"" +
56                                  previousName + "\"");
57                  return;
58              }
59          }
60          // FtpFile name not allowed or not found
61          throw new Reply553Exception("Filename not allowed");
62      }
63  
64  }