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.info;
19  
20  import java.util.List;
21  
22  import org.waarp.common.command.ReplyCode;
23  import org.waarp.common.command.exception.CommandAbstractException;
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  import org.waarp.ftp.core.utils.FtpChannelUtils;
29  
30  /**
31   * STAT command
32   * 
33   * @author Frederic Bregier
34   * 
35   */
36  public class STAT extends AbstractCommand {
37      @Override
38      public void exec() throws CommandAbstractException {
39          String path = null;
40          String message = null;
41          message = "STATUS information\nNo FtpFile currently in transfer\n";
42          FtpFile file = null;
43          try {
44              file = getSession().getDataConn().getFtpTransferControl()
45                      .getExecutingFtpTransfer().getFtpFile();
46          } catch (FtpNoFileException e) {
47          } catch (FtpNoTransferException e) {
48          }
49          if (file != null) {
50              if (file.isInReading()) {
51                  message = "STATUS information\nFile currently in Retrieve transfer\n";
52              } else if (file.isInWriting()) {
53                  message = "STATUS information\nFile currently in Store transfer\n";
54              }
55          }
56          if (!hasArg()) {
57              // Current status of connection
58              message += getSession().getDataConn().getStatus();
59              message += "\nControl: " +
60                      FtpChannelUtils.nbCommandChannels(getConfiguration()) +
61                      " Data: " +
62                      FtpChannelUtils.nbDataChannels(getConfiguration()) +
63                      " Binded: " +
64                      getConfiguration().getFtpInternalConfiguration()
65                              .getNbBindedPassive();
66              message += "\nEnd of Status";
67              getSession().setReplyCode(ReplyCode.REPLY_211_SYSTEM_STATUS_REPLY,
68                      message);
69          } else {
70              // List of files from path
71              path = getArg();
72              List<String> filesInfo = getSession().getDir().listFull(path, true);
73              StringBuilder builder = new StringBuilder().append("List of files from ").append(path).append('\n');
74              for (String newfileInfo : filesInfo) {
75                  builder.append(newfileInfo).append('\n');
76              }
77              builder.append("End of Status");
78              message += builder.toString();
79              getSession().setReplyCode(ReplyCode.REPLY_212_DIRECTORY_STATUS,
80                      message);
81          }
82      }
83  
84  }