View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.cmd.impl;
26  
27  import java.io.File;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import net.sf.hermesftp.cmd.AbstractFtpCmd;
32  import net.sf.hermesftp.exception.FtpCmdException;
33  import net.sf.hermesftp.utils.IOUtils;
34  
35  /***
36   * <b>STATUS (STAT)</b>
37   * <p>
38   * This command shall cause a status response to be sent over the control connection in the form of
39   * a reply. The command may be sent during a file transfer (along with the Telnet IP and Synch
40   * signals--see the Section on FTP Commands) in which case the server will respond with the status
41   * of the operation in progress, or it may be sent between file transfers. In the latter case, the
42   * command may have an argument field. If the argument is a pathname, the command is analogous to
43   * the "list" command except that data shall be transferred over the control connection. If a
44   * partial pathname is given, the server may respond with a list of file names or attributes
45   * associated with that specification. If no argument is given, the server should return general
46   * status information about the server FTP process. This should include current values of all
47   * transfer parameters and the status of connections.
48   * <p>
49   * <i>[Excerpt from RFC-959, Postel and Reynolds]</i>
50   * </p>
51   * 
52   * @author Lars Behnke
53   */
54  public class FtpCmdStat extends AbstractFtpCmd {
55  
56      /***
57       * {@inheritDoc}
58       */
59      public void execute() throws FtpCmdException {
60          String clientHost = getCtx().getClientSocket().getInetAddress().getHostAddress();
61          String msg = getCtx().getUser() + "(" + clientHost + ")";
62          msgOut(MSG211_STAT, new Object[] {msg});
63          Map<String, Long> map = getCtx().getUserManager().getUserStatistics(getCtx().getUser());
64          String arg = getArguments();
65          if (arg.length() == 0) {
66              printUserStatistics(map);
67          } else {
68              File dir = new File(getPathArg());
69              if (!dir.exists()) {
70                  msgOut(MSG550);
71                  return;
72              }
73              if (dir.isDirectory()) {
74                  File[] files = dir.listFiles();
75                  for (int i = 0; i < files.length; i++) {
76                      doPrintFileInfo(files[i]);
77                  }
78              } else {
79                  doPrintFileInfo(dir);
80              }
81          }
82          out("211 ");
83      }
84  
85      private void doPrintFileInfo(File file) {
86          int permission = getCtx().getPermission(file.getAbsolutePath());
87          boolean read = (permission & PRIV_READ) > 0;
88          boolean write = (permission & PRIV_WRITE) > 0;
89          out("211-" + IOUtils.formatUnixFtpFileInfo(file, read, write));
90  
91      }
92  
93      private void printUserStatistics(Map<String, Long> map) {
94          Set<Map.Entry<String, Long>> entrySet = map.entrySet();
95          for (Map.Entry<String, Long> entry : entrySet) {
96              String statName = (String) entry.getKey();
97              Long value = (Long) entry.getValue();
98              printOutStats(statName, value);
99  
100         }
101 
102     }
103 
104     private void printOutStats(String statName, Long value) {
105         long statValue = value == null ? 0 : value.longValue();
106         out("211-" + statName + ": " + statValue);
107     }
108 
109     /***
110      * {@inheritDoc}
111      */
112     public String getHelp() {
113         return "Returns short client session based statistic";
114     }
115 
116     /***
117      * {@inheritDoc}
118      */
119     public boolean isAuthenticationRequired() {
120         return true;
121     }
122 
123 }