001: package com.coldcore.coloradoftp.command.impl;
002:
003: import com.coldcore.coloradoftp.command.Command;
004: import com.coldcore.coloradoftp.command.Reply;
005: import com.coldcore.coloradoftp.connection.ControlConnection;
006: import com.coldcore.coloradoftp.factory.ObjectFactory;
007: import com.coldcore.coloradoftp.factory.ObjectName;
008: import com.coldcore.coloradoftp.session.DataOpenerType;
009: import com.coldcore.coloradoftp.session.LoginState;
010: import com.coldcore.coloradoftp.session.Session;
011: import com.coldcore.coloradoftp.session.SessionAttributeName;
012:
013: import java.util.regex.Matcher;
014: import java.util.regex.Pattern;
015:
016: /**
017: * @see com.coldcore.coloradoftp.command.Command
018: *
019: * Base class with few helper methods.
020: */
021: abstract public class AbstractCommand implements Command {
022:
023: protected String name;
024: protected String parameter;
025: protected ControlConnection controlConnection;
026: private Reply reply; //Via getter only!
027:
028: protected Reply getReply() {
029: if (reply == null) {
030: reply = (Reply) ObjectFactory.getObject(ObjectName.REPLY);
031: reply.setCommand(this );
032: }
033: return reply;
034: }
035:
036: /** Test if user is loggen in.
037: * If user is not logged in this method fills the internal reply with a default error message.
038: * @return TRUE if user is logged in, FALSE if user is not logged in
039: */
040: protected boolean testLogin() {
041: Session session = getConnection().getSession();
042: LoginState loginState = (LoginState) session
043: .getAttribute(SessionAttributeName.LOGIN_STATE);
044: if (loginState != null)
045: return true;
046: Reply reply = getReply();
047: reply.setCode("530");
048: reply.setText("Not logged in.");
049: return false;
050: }
051:
052: /** Prepares everything for a new data connection.
053: * If failed then this method fills the internal reply with a default error message.
054: * It is recommended to call this method last because it is not easy to reverse changes this
055: * method applies.
056: * @return TRUE if ready for a new data connection, FALSE otherwise
057: */
058: protected boolean prepareForDataConnection() {
059: Session session = getConnection().getSession();
060: DataOpenerType dtype = (DataOpenerType) session
061: .getAttribute(SessionAttributeName.DATA_OPENER_TYPE);
062: if (dtype == null) {
063: Reply reply = getReply();
064: reply.setCode("425");
065: reply.setText("Can't open data connection.");
066: return false;
067: }
068:
069: //PASV is active only once as the connection is removed from the listeners set
070: if (dtype == DataOpenerType.PASV) {
071: session
072: .removeAttribute(SessionAttributeName.DATA_OPENER_TYPE);
073: return true;
074: }
075:
076: //PORT must activate data connection initiator in the control connection
077: if (dtype == DataOpenerType.PORT) {
078:
079: //Byte marker before "150" reply for a data connection initiator
080: session.setAttribute(
081: SessionAttributeName.BYTE_MARKER_150_REPLY,
082: controlConnection.getBytesWrote());
083:
084: controlConnection.getDataConnectionInitiator().activate();
085: return true;
086: }
087:
088: throw new RuntimeException(
089: "BUG: Unknown data opener type provided");
090: }
091:
092: /** Check syntax
093: * @param str String to check
094: * @param regexp Regular expression that defines syntax rules
095: */
096: protected boolean checkRegExp(String str, String regexp) {
097: Pattern pattern = Pattern.compile(regexp);
098: Matcher matcher = pattern.matcher(str);
099: return matcher.matches();
100: }
101:
102: public boolean processInInterruptState() {
103: return false;
104: }
105:
106: public boolean canClearInterruptState() {
107: return false;
108: }
109:
110: public String getName() {
111: if (name == null)
112: return null;
113: return name.trim().toUpperCase(); //Return in uppercase
114: }
115:
116: public void setName(String name) {
117: this .name = name;
118: }
119:
120: public String getParameter() {
121: if (parameter == null)
122: return ""; //Do not return NULL
123: return parameter.trim();
124: }
125:
126: public void setParameter(String parameter) {
127: this .parameter = parameter;
128: }
129:
130: public void setConnection(ControlConnection connection) {
131: controlConnection = connection;
132: }
133:
134: public ControlConnection getConnection() {
135: return controlConnection;
136: }
137:
138: public Reply executeOnParent(Command parent) {
139: return null;
140: }
141: }
|