KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > commands > MLST


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.commands;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import net.sf.drftpd.ObjectNotFoundException;
30 import net.sf.drftpd.master.BaseFtpConnection;
31 import net.sf.drftpd.master.FtpReply;
32 import net.sf.drftpd.master.command.CommandManager;
33 import net.sf.drftpd.master.command.CommandManagerFactory;
34 import net.sf.drftpd.master.command.plugins.DataConnectionHandler;
35 import net.sf.drftpd.remotefile.LinkedRemoteFile;
36 import net.sf.drftpd.remotefile.MLSTSerialize;
37 import net.sf.drftpd.remotefile.RemoteFileInterface;
38 import net.sf.drftpd.util.ListUtils;
39
40 import org.apache.log4j.Logger;
41
42 /**
43  * @author mog
44  * @version $Id: MLST.java,v 1.1 2004/06/01 15:40:34 mog Exp $
45  */

46 public class MLST implements CommandHandlerFactory, CommandHandler {
47
48     private static final Logger logger = Logger.getLogger(MLST.class);
49     public FtpReply execute(BaseFtpConnection conn)
50         throws UnhandledCommandException {
51         String JavaDoc command = conn.getRequest().getCommand();
52
53         LinkedRemoteFile dir = conn.getCurrentDirectory();
54         if (conn.getRequest().hasArgument()) {
55             try {
56                 dir = dir.lookupFile(conn.getRequest().getArgument());
57             } catch (FileNotFoundException JavaDoc e) {
58                 return FtpReply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
59             }
60         }
61         if (!conn.getConfig().checkPrivPath(conn.getUserNull(), dir)) {
62             return FtpReply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
63         }
64         PrintWriter JavaDoc out = conn.getControlWriter();
65         if ("MLST".equals(command)) {
66             out.print("250- Begin\r\n");
67             out.print(toMLST(dir) + "\r\n");
68             out.print("250 End.\r\n");
69             return null;
70         } else if ("MLSD".equals(command)) {
71             DataConnectionHandler dataConnHnd;
72             try {
73                 dataConnHnd =
74                     (DataConnectionHandler) conn
75                         .getCommandManager()
76                         .getCommandHandler(
77                         DataConnectionHandler.class);
78             } catch (ObjectNotFoundException e) {
79                 return new FtpReply(500, e.getMessage());
80             }
81
82             out.print(FtpReply.RESPONSE_150_OK);
83             out.flush();
84             try {
85                 Socket JavaDoc sock =
86                     dataConnHnd.getDataSocket(conn.getSocketFactory());
87                 List JavaDoc files = ListUtils.list(dir, conn);
88                 Writer JavaDoc os = new OutputStreamWriter JavaDoc(sock.getOutputStream());
89                 for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
90                     RemoteFileInterface file =
91                         (RemoteFileInterface) iter.next();
92                     os.write(toMLST(file) + "\r\n");
93                 }
94                 os.close();
95             } catch (IOException JavaDoc e1) {
96                 logger.warn("", e1);
97                 //425 Can't open data connection
98
return new FtpReply(425, e1.getMessage());
99             }
100
101             return FtpReply.RESPONSE_226_CLOSING_DATA_CONNECTION;
102         }
103         return FtpReply.RESPONSE_500_SYNTAX_ERROR;
104     }
105
106     public String JavaDoc[] getFeatReplies() {
107         return new String JavaDoc[] { "MLST type*,x.crc32*,size*,modify*,unix.owner*,unix.group*,x.slaves*,x.xfertime*" };
108     }
109
110     public CommandHandler initialize(
111         BaseFtpConnection conn,
112         CommandManager initializer) {
113         return this;
114     }
115
116     public void load(CommandManagerFactory initializer) {
117     }
118
119     private String JavaDoc toMLST(RemoteFileInterface file) {
120         String JavaDoc ret = MLSTSerialize.toMLST(file);
121         //add perm=
122
//add
123
return ret;
124     }
125
126     public void unload() {
127     }
128 }
129
Popular Tags