001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.commands;
019:
020: import org.apache.log4j.Level;
021: import org.apache.log4j.Logger;
022:
023: import java.io.BufferedReader;
024: import java.io.IOException;
025:
026: import java.util.Iterator;
027: import java.util.Vector;
028:
029: /**
030: * @author mog
031: * @version $Id: Reply.java 1513 2006-10-13 22:41:08Z tdsoul $
032: */
033: public class Reply implements Cloneable {
034: private static final Logger logger = Logger.getLogger(Reply.class
035: .getName());
036:
037: /** 150 File status okay; about to open data connection. */
038: public static final String RESPONSE_150_OK = "150 File status okay; about to open data connection.\r\n";
039:
040: /** 200 Command okay */
041: public static final Reply RESPONSE_200_COMMAND_OK = new Reply(200,
042: "Command okay");
043:
044: /** 202 Command not implemented, superfluous at this site. */
045: public static final Reply RESPONSE_202_COMMAND_NOT_IMPLEMENTED = new Reply(
046: 202, "Command not implemented, superfluous at this site.");
047:
048: /** 215 NAME system type. */
049: public static final Reply RESPONSE_215_SYSTEM_TYPE = new Reply(215,
050: "UNIX system type.");
051:
052: /** 221 Service closing control connection. */
053: public static final Reply RESPONSE_221_SERVICE_CLOSING = new Reply(
054: 221, "Service closing control connection.");
055:
056: /** 226 Closing data connection */
057: public static final Reply RESPONSE_226_CLOSING_DATA_CONNECTION = new Reply(
058: 226, "Closing data connection");
059:
060: /** 230 User logged in, proceed. */
061: public static final Reply RESPONSE_230_USER_LOGGED_IN = new Reply(
062: 230, "User logged in, proceed.");
063:
064: /** 250 Requested file action okay, completed. */
065: public static final Reply RESPONSE_250_ACTION_OKAY = new Reply(250,
066: "Requested file action okay, completed.");
067:
068: /** 331 User name okay, need password. */
069: public static final Reply RESPONSE_331_USERNAME_OK_NEED_PASS = new Reply(
070: 331, "User name okay, need password.");
071:
072: /** 350 Requested file action pending further information. */
073: public static final Reply RESPONSE_350_PENDING_FURTHER_INFORMATION = new Reply(
074: 350, "Requested file action pending further information.");
075:
076: /** 425 Can't open data connection. */
077: public static final String RESPONSE_425_CANT_OPEN_DATA_CONNECTION = "425 Can't open data connection.\r\n";
078:
079: /** 426 Connection closed; transfer aborted. */
080: public static final Reply RESPONSE_426_CONNECTION_CLOSED_TRANSFER_ABORTED = new Reply(
081: 426, "Connection closed; transfer aborted.");
082:
083: /** 450 Requested file action not taken. */
084: public static final Reply RESPONSE_450_REQUESTED_ACTION_NOT_TAKEN = new Reply(
085: 450, "Requested file action not taken.");
086:
087: /** 450 No transfer-slave(s) available
088: * author <a href="mailto:drftpd@mog.se">Morgan Christiansson</a>
089: */
090: public static final Reply RESPONSE_450_SLAVE_UNAVAILABLE = new Reply(
091: 450, "No transfer-slave(s) available");
092:
093: /** 500 Syntax error, command unrecognized. */
094: public static final Reply RESPONSE_500_SYNTAX_ERROR = new Reply(
095: 500, "Syntax error, command unrecognized.");
096:
097: /** 501 Syntax error in parameters or arguments */
098: public static final Reply RESPONSE_501_SYNTAX_ERROR = new Reply(
099: 501, "Syntax error in parameters or arguments");
100:
101: /** 502 Command not implemented. */
102: public static final Reply RESPONSE_502_COMMAND_NOT_IMPLEMENTED = new Reply(
103: 502, "Command not implemented.");
104:
105: /** 503 Bad sequence of commands. */
106: public static final Reply RESPONSE_503_BAD_SEQUENCE_OF_COMMANDS = new Reply(
107: 503, "Bad sequence of commands.");
108:
109: /** 504 Command not implemented for that parameter. */
110: public static final Reply RESPONSE_504_COMMAND_NOT_IMPLEMENTED_FOR_PARM = new Reply(
111: 504, "Command not implemented for that parameter.");
112:
113: /** 530 Access denied */
114: public static final Reply RESPONSE_530_ACCESS_DENIED = new Reply(
115: 530, "Access denied");
116:
117: /** 530 Not logged in. */
118: public static final Reply RESPONSE_530_NOT_LOGGED_IN = new Reply(
119: 530, "Not logged in.");
120: public static final Reply RESPONSE_530_SLAVE_UNAVAILABLE = new Reply(
121: 530, "No transfer-slave(s) available");
122:
123: /**
124: * 550 Requested action not taken. File unavailable.
125: * File unavailable (e.g., file not found, no access).
126: */
127: public static final Reply RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN = new Reply(
128: 550, "Requested action not taken. File unavailable.");
129:
130: /** 553 Requested action not taken.
131: * File name not allowed.
132: */
133: public static final Reply RESPONSE_553_REQUESTED_ACTION_NOT_TAKEN = new Reply(
134: 553, "Requested action not taken. File name not allowed");
135:
136: /** 550 Requested action not taken.
137: * File exists.
138: */
139: public static final Reply RESPONSE_553_REQUESTED_ACTION_NOT_TAKEN_FILE_EXISTS = new Reply(
140: 550, "Requested action not taken. File exists.");
141:
142: protected int _code;
143: protected Vector<String> _lines = new Vector<String>();
144: protected String _message;
145:
146: public Reply() {
147: }
148:
149: public Reply(int code) {
150: setCode(code);
151: }
152:
153: public Reply(int code, String response) {
154: setCode(code);
155: setMessage(response);
156: }
157:
158: public Reply addComment(BufferedReader in) throws IOException {
159: String line;
160:
161: while ((line = in.readLine()) != null) { //throws IOException
162: this .addComment(line);
163: }
164:
165: return this ;
166: }
167:
168: public Reply addComment(Object response) {
169: String resp = String.valueOf(response);
170:
171: if (resp.indexOf('\n') != -1) {
172: String[] lines = resp.split("\n");
173:
174: for (int i = 0; i < lines.length; i++) {
175: _lines.add(lines[i]);
176: }
177: } else {
178: _lines.add(resp);
179: }
180: return this ;
181: }
182:
183: public Object clone() {
184: try {
185: Reply r = (Reply) super .clone();
186: r._lines = (Vector) _lines.clone();
187:
188: return r;
189: } catch (CloneNotSupportedException ex) {
190: throw new RuntimeException(ex);
191: }
192: }
193:
194: public int getCode() {
195: return _code;
196: }
197:
198: public void setCode(int code) {
199: _code = code;
200: }
201:
202: public void setMessage(String response) {
203: if (response == null)
204: response = "No text";
205: int pos = response.indexOf('\n');
206:
207: if (pos != -1) {
208: addComment(response.substring(pos + 1));
209: response = response.substring(0, pos);
210: logger.log(Level.DEBUG,
211: "Truncated response message with multiple lines: "
212: + response);
213: }
214:
215: _message = response;
216: }
217:
218: public int size() {
219: return _lines.size();
220: }
221:
222: public String toString() {
223: StringBuffer sb = new StringBuffer();
224:
225: //sb.append(code + "-");
226: if ((_lines.size() == 0) && (_message == null)) {
227: setMessage("No text specified");
228: }
229:
230: for (Iterator iter = _lines.iterator(); iter.hasNext();) {
231: String comment = (String) iter.next();
232:
233: if (!iter.hasNext() && (_message == null)) {
234: sb.append(_code + " " + comment + "\r\n");
235: } else {
236: sb.append(_code + "- " + comment + "\r\n");
237: }
238: }
239:
240: if (_message != null) {
241: sb.append(_code + " " + _message + "\r\n");
242: }
243:
244: return sb.toString();
245: }
246:
247: public String getMessage() {
248: return _message;
249: }
250: }
|