1 package com.coldcore.coloradoftp.command.impl; 2 3 import com.coldcore.coloradoftp.command.Command; 4 import com.coldcore.coloradoftp.command.CommandProcessor; 5 import com.coldcore.coloradoftp.command.Reply; 6 import com.coldcore.coloradoftp.factory.ObjectFactory; 7 import com.coldcore.coloradoftp.factory.ObjectName; 8 import com.coldcore.coloradoftp.filesystem.FailedActionException; 9 import org.apache.log4j.Logger; 10 11 14 public class GenericCommandProcessor implements CommandProcessor { 15 16 private static Logger log = Logger.getLogger(GenericCommandProcessor.class); 17 18 19 public void execute(Command command) { 20 Reply reply; 22 try { 23 reply = command.execute(); 24 } catch (FailedActionException e) { 25 reply = getFileSystemReply(e); 27 } catch (Throwable e) { 28 log.error("Command failed (ignoring)", e); 30 reply = ((Command) ObjectFactory.getObject(ObjectName.COMMAND_LOCAL_ERROR)).execute(); 31 } 32 33 String debug = "[Command] "+command.getName()+" "+(command.getParameter()==null?"":command.getParameter())+"\n"; 34 debug += reply==null?"[NO REPLY]":"[Reply] "+reply.getCode()+" "+(reply.getText()==null?"":reply.getText()); 35 log.debug("Execution result:\n"+debug); 36 37 try { 39 if (reply != null) command.getConnection().reply(reply); 40 } catch (Throwable e) { 41 log.error("Command's connection reply failed (ignoring)", e); 42 } 43 } 44 45 46 50 protected Reply getFileSystemReply(FailedActionException e) { 51 Reply reply = (Reply) ObjectFactory.getObject(ObjectName.REPLY); 52 switch (e.getReason()) { 53 case INVALID_INPUT: 54 reply.setCode("553"); 55 reply.setText(e.getText() == null ? "Cannot parse input." : e.getText()); 56 return reply; 57 case NO_PERMISSIONS: 58 reply.setCode("550"); 59 reply.setText(e.getText() == null ? "No permission." : e.getText()); 60 return reply; 61 case NOT_IMPLEMENTED: 62 reply.setCode("504"); 63 reply.setText(e.getText() == null ? "Not implemented." : e.getText()); 64 return reply; 65 case PATH_ERROR: 66 reply.setCode("450"); 67 reply.setText(e.getText() == null ? "Requested path error." : e.getText()); 68 return reply; 69 case OTHER: 70 reply.setCode("450"); 71 reply.setText(e.getText() == null ? "Unknown reason." : e.getText()); 72 return reply; 73 default: 74 log.error("Filesystem failed (ignoring)", e); 75 return ((Command) ObjectFactory.getObject(ObjectName.COMMAND_LOCAL_ERROR)).execute(); 76 } 77 } 78 } 79 | Popular Tags |