1 package com.coldcore.coloradoftp.command.impl; 2 3 import com.coldcore.coloradoftp.command.Command; 4 import com.coldcore.coloradoftp.command.Reply; 5 import com.coldcore.coloradoftp.connection.ControlConnection; 6 import com.coldcore.coloradoftp.factory.ObjectFactory; 7 import com.coldcore.coloradoftp.factory.ObjectName; 8 import com.coldcore.coloradoftp.session.DataOpenerType; 9 import com.coldcore.coloradoftp.session.LoginState; 10 import com.coldcore.coloradoftp.session.Session; 11 import com.coldcore.coloradoftp.session.SessionAttributeName; 12 13 import java.util.regex.Matcher ; 14 import java.util.regex.Pattern ; 15 16 21 abstract public class AbstractCommand implements Command { 22 23 protected String name; 24 protected String parameter; 25 protected ControlConnection controlConnection; 26 private Reply reply; 28 29 protected Reply getReply() { 30 if (reply == null) { 31 reply = (Reply) ObjectFactory.getObject(ObjectName.REPLY); 32 reply.setCommand(this); 33 } 34 return reply; 35 } 36 37 38 42 protected boolean testLogin() { 43 Session session = getConnection().getSession(); 44 LoginState loginState = (LoginState) session.getAttribute(SessionAttributeName.LOGIN_STATE); 45 if (loginState != null) return true; 46 Reply reply = getReply(); 47 reply.setCode("530"); 48 reply.setText("Not logged in."); 49 return false; 50 } 51 52 53 59 protected boolean prepareForDataConnection() { 60 Session session = getConnection().getSession(); 61 DataOpenerType dtype = (DataOpenerType) session.getAttribute(SessionAttributeName.DATA_OPENER_TYPE); 62 if (dtype == null) { 63 Reply reply = getReply(); 64 reply.setCode("425"); 65 reply.setText("Can't open data connection."); 66 return false; 67 } 68 69 if (dtype == DataOpenerType.PASV) { 71 session.removeAttribute(SessionAttributeName.DATA_OPENER_TYPE); 72 return true; 73 } 74 75 if (dtype == DataOpenerType.PORT) { 77 78 session.setAttribute(SessionAttributeName.BYTE_MARKER_150_REPLY, controlConnection.getBytesWrote()); 80 81 controlConnection.getDataConnectionInitiator().activate(); 82 return true; 83 } 84 85 throw new RuntimeException ("BUG: Unknown data opener type provided"); 86 } 87 88 89 93 protected boolean checkRegExp(String str, String regexp) { 94 Pattern pattern = Pattern.compile(regexp); 95 Matcher matcher = pattern.matcher(str); 96 return matcher.matches(); 97 } 98 99 100 public boolean processInInterruptState() { 101 return false; 102 } 103 104 105 public boolean canClearInterruptState() { 106 return false; 107 } 108 109 110 public String getName() { 111 if (name == null) return null; 112 return name.trim().toUpperCase(); } 114 115 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 121 public String getParameter() { 122 if (parameter == null) return ""; return parameter.trim(); 124 } 125 126 127 public void setParameter(String parameter) { 128 this.parameter = parameter; 129 } 130 131 132 public void setConnection(ControlConnection connection) { 133 controlConnection = connection; 134 } 135 136 137 public ControlConnection getConnection() { 138 return controlConnection; 139 } 140 141 142 public Reply executeOnParent(Command parent) { 143 return null; 144 } 145 } 146 | Popular Tags |