KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > command > impl > AbstractCommand


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 JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
15
16 /**
17  * @see com.coldcore.coloradoftp.command.Command
18  *
19  * Base class with few helper methods.
20  */

21 abstract public class AbstractCommand implements Command {
22
23   protected String JavaDoc name;
24   protected String JavaDoc parameter;
25   protected ControlConnection controlConnection;
26   private Reply reply; //Via getter only!
27

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   /** Test if user is loggen in.
39    * If user is not logged in this method fills the internal reply with a default error message.
40    * @return TRUE if user is logged in, FALSE if user is not logged in
41    */

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   /** Prepares everything for a new data connection.
54    * If failed then this method fills the internal reply with a default error message.
55    * It is recommended to call this method last because it is not easy to reverse changes this
56    * method applies.
57    * @return TRUE if ready for a new data connection, FALSE otherwise
58    */

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     //PASV is active only once as the connection is removed from the listeners set
70
if (dtype == DataOpenerType.PASV) {
71       session.removeAttribute(SessionAttributeName.DATA_OPENER_TYPE);
72       return true;
73     }
74
75     //PORT must activate data connection initiator in the control connection
76
if (dtype == DataOpenerType.PORT) {
77
78       //Byte marker before "150" reply for a data connection initiator
79
session.setAttribute(SessionAttributeName.BYTE_MARKER_150_REPLY, controlConnection.getBytesWrote());
80
81       controlConnection.getDataConnectionInitiator().activate();
82       return true;
83     }
84
85     throw new RuntimeException JavaDoc("BUG: Unknown data opener type provided");
86   }
87
88
89   /** Check syntax
90    * @param str String to check
91    * @param regexp Regular expression that defines syntax rules
92    */

93   protected boolean checkRegExp(String JavaDoc str, String JavaDoc regexp) {
94     Pattern JavaDoc pattern = Pattern.compile(regexp);
95     Matcher JavaDoc 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 JavaDoc getName() {
111     if (name == null) return null;
112     return name.trim().toUpperCase(); //Return in uppercase
113
}
114
115
116   public void setName(String JavaDoc name) {
117     this.name = name;
118   }
119
120
121   public String JavaDoc getParameter() {
122     if (parameter == null) return ""; //Do not return NULL
123
return parameter.trim();
124   }
125
126
127   public void setParameter(String JavaDoc 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