Source code: jsd/ftp/server/ftp/FtpStatus.java
1 /*
2 * ----------------------------------------------------------------------------
3 * JStrangeDownloader and all accompanying source code files are
4 * Copyright (C) 2002 Dusty Davidson (dustyd@iastate.edu)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * ----------------------------------------------------------------------------
20 *
21 * Please see gpl.txt for the full text of the GNU General Public
22 * License.
23 */
24
25 package jsd.ftp.server.ftp;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.util.Properties;
32
33 /**
34 * Ftp status line parser class. This class loads <code>FtpStatus.properties</code>
35 * file from the classpath. It generates the descriptive ftp status for
36 * astatus code. The actual response depends on the status code, the ftp
37 * command and the passed argument list.
38 *
39 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
40 */
41 public
42 class FtpStatus extends Properties {
43
44 private final static String RESOURCE = "FtpStatus.properties";
45 // private final static String RESOURCE = "ftp/server/ftp/FtpStatus.properties";
46 private final static String PREFIX = "FtpServer.status.";
47 private final static String EMPTY = "";
48 private final static String CRLF = "\r\n";
49
50 private final static String CMD = "CMD";
51 private final static String ARG = "ARG";
52
53 /**
54 * Load status propeties file from the classpath.
55 */
56 public FtpStatus() throws IOException {
57 InputStream pis = getClass().getClassLoader().getResourceAsStream(RESOURCE);
58 load(pis);
59 pis.close();
60 }
61
62
63 /**
64 * Process ftp response new line character.
65 */
66 public String processNewLine(String msg, int status) {
67
68 // no newline
69 if(msg.indexOf('\n') == -1) {
70 return status + " " + msg + CRLF;
71 }
72
73 StringBuffer sw = new StringBuffer(256);
74
75 try {
76 BufferedReader sr = new BufferedReader(new StringReader(msg));
77
78 sw.append(String.valueOf(status));
79 sw.append('-');
80
81 String line = sr.readLine();
82 for(;;) {
83 String nextLine = sr.readLine();
84
85 if(nextLine != null) {
86 sw.append(line);
87 sw.append(CRLF);
88 }
89 else {
90 sw.append(String.valueOf(status));
91 sw.append(' ');
92 sw.append(line);
93 sw.append(CRLF);
94 break;
95 }
96 line = nextLine;
97 }
98 sr.close();
99 }
100 catch(IOException ex) {
101 }
102
103 return sw.toString();
104 }
105
106
107 /**
108 * Get ftp message from the properties file and replace the variables.
109 */
110 private String getMessage(int status, FtpRequest cmdLine, String[] args) {
111
112 // make the key from the passed parameters
113 String key = PREFIX + status;
114 String keyc = key;
115 if(cmdLine != null) {
116 keyc = keyc + '.' + cmdLine.getCommand();
117 }
118
119 // get status property
120 String str = getProperty(keyc);
121 if(str == null) {
122 str = getProperty(key);
123 }
124 if(str == null) {
125 str = EMPTY;
126 }
127
128 // replace variables
129 int startIndex = 0;
130 int openIndex = str.indexOf('{', startIndex);
131 int closeIndex = str.indexOf('}', startIndex);
132
133 if( (openIndex == -1) || (closeIndex == -1) || (openIndex > closeIndex) ) {
134 return str;
135 }
136
137 StringBuffer sb = new StringBuffer();
138 sb.append(str.substring(startIndex, openIndex));
139 while(true) {
140 String intStr = str.substring(openIndex+1, closeIndex);
141 sb.append(getParam(cmdLine, args, intStr));
142
143 startIndex = closeIndex + 1;
144 openIndex = str.indexOf('{', startIndex);
145 closeIndex = str.indexOf('}', startIndex);
146
147 if( (openIndex == -1) || (closeIndex == -1) || (openIndex > closeIndex) ) {
148 sb.append(str.substring(startIndex));
149 break;
150 }
151 sb.append(str.substring(startIndex, openIndex));
152 }
153 return sb.toString();
154 }
155
156
157 /**
158 * Get variable value.
159 */
160 private String getParam(FtpRequest cmdLine, String[] elms, String intStr) {
161
162 // command line param
163 if(cmdLine != null) {
164 if(intStr.equals(CMD)) {
165 return cmdLine.getCommand();
166 }
167 if(intStr.equals(ARG)) {
168 return cmdLine.getArgument();
169 }
170 }
171
172 // list param
173 if(elms == null) {
174 return EMPTY;
175 }
176
177 int index = 0;
178 try {
179 index = Integer.parseInt(intStr);
180 }
181 catch(NumberFormatException ex) {
182 return EMPTY;
183 }
184 if( (index < 0) || (index >= elms.length) ) {
185 return EMPTY;
186 }
187 return elms[index];
188 }
189
190
191 /**
192 * Get ftp response.
193 * @param status ftp status code.
194 * @param cmd ftp request object (may be null).
195 * @param ars variable arguent list (may be null).
196 */
197 public String getResponse(int status, FtpRequest cmd, FtpUser user, String[] args) {
198 String strRes = getMessage(status, cmd, args);
199 return processNewLine(strRes, status);
200 }
201
202 }