1 package com.coldcore.coloradoftp.command.impl.ftp; 2 3 import com.coldcore.coloradoftp.command.CommandFactory; 4 import com.coldcore.coloradoftp.command.Reply; 5 import com.coldcore.coloradoftp.command.Command; 6 import com.coldcore.coloradoftp.command.impl.AbstractCommand; 7 import com.coldcore.coloradoftp.factory.ObjectFactory; 8 import com.coldcore.coloradoftp.factory.ObjectName; 9 import org.apache.log4j.Logger; 10 11 import java.util.ArrayList ; 12 import java.util.Collections ; 13 import java.util.Comparator ; 14 import java.util.List ; 15 16 22 public class HelpCommand extends AbstractCommand { 23 24 private static Logger log = Logger.getLogger(HelpCommand.class); 25 26 27 public Reply execute() { 28 Reply reply = getReply(); 29 30 CommandFactory commandFactory = (CommandFactory) ObjectFactory.getObject(ObjectName.COMMAND_FACTORY); 32 List <String > commandNames = new ArrayList <String >(commandFactory.listNames()); 33 34 String pcmd = getParameter().toUpperCase(); 35 if (pcmd.length() > 0) { 36 if (commandNames.contains(pcmd)) { 37 log.debug("Command "+pcmd+" is supported"); 39 replyOnExistingCommand(pcmd); 40 return reply; 41 } else { 42 log.debug("Command "+pcmd+" is not supported"); 44 replyOnUnexistingCommand(pcmd); 45 return reply; 46 } 47 } 48 49 replyOnNoCommand(); 51 52 return reply; 53 } 54 55 56 59 protected void replyOnExistingCommand(String command) { 60 CommandFactory commandFactory = (CommandFactory) ObjectFactory.getObject(ObjectName.COMMAND_FACTORY); 62 Command cmd = commandFactory.create(command+"\r\n"); 63 cmd.setConnection(getConnection()); 64 65 Reply reply = getReply(); 66 67 Reply rpl = cmd.executeOnParent(this); 69 if (rpl != null) { 70 reply.setCode(rpl.getCode()); 72 reply.setText(rpl.getText()); 73 } else { 74 StringBuffer sb = new StringBuffer ("Supported command:\r\n"); 76 sb.append(command).append("\r\nend"); reply.setCode("211"); 78 reply.setText(sb.toString()); 79 } 80 } 81 82 83 86 protected void replyOnUnexistingCommand(String command) { 87 Reply reply = getReply(); 88 reply.setCode("504"); 89 reply.setText("Command not implemented for that parameter."); 90 } 91 92 93 94 protected void replyOnNoCommand() { 95 CommandFactory commandFactory = (CommandFactory) ObjectFactory.getObject(ObjectName.COMMAND_FACTORY); 97 List <String > commandNames = new ArrayList <String >(commandFactory.listNames()); 98 99 Collections.sort(commandNames, new Comparator <String >() { 101 public int compare(String o1, String o2) { 102 return o1.compareToIgnoreCase(o2); 103 } 104 }); 105 106 StringBuffer sb = new StringBuffer ("Supported commands:\r\n"); 108 int i = 0; 109 for (String commandName : commandNames) { 110 sb.append(commandName); 111 if (++i % 6 != 0) sb.append("\t"); 112 else sb.append("\r\n"); 113 } 114 115 Reply reply = getReply(); 116 reply.setCode("214"); 117 reply.setText(sb.toString().trim()+"\r\nOther commands unimplemented."); 118 } 119 120 } 121 | Popular Tags |