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