1 18 package org.drftpd.commands; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.OutputStreamWriter ; 23 import java.io.PrintWriter ; 24 import java.io.Writer ; 25 import java.net.Socket ; 26 import java.util.Iterator ; 27 import java.util.List ; 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 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 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 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 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 sock = 86 dataConnHnd.getDataSocket(conn.getSocketFactory()); 87 List files = ListUtils.list(dir, conn); 88 Writer os = new OutputStreamWriter (sock.getOutputStream()); 89 for (Iterator 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 e1) { 96 logger.warn("", e1); 97 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 [] getFeatReplies() { 107 return new String [] { "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 toMLST(RemoteFileInterface file) { 120 String ret = MLSTSerialize.toMLST(file); 121 return ret; 124 } 125 126 public void unload() { 127 } 128 } 129 | Popular Tags |