1 |
/* |
/* |
2 |
* $Id$ |
* FTPURLConnection.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
59 |
* An FTP URL connection. |
* An FTP URL connection. |
60 |
* |
* |
61 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
62 |
*/ |
*/ |
63 |
public class FTPURLConnection extends URLConnection |
public class FTPURLConnection |
64 |
|
extends URLConnection |
65 |
{ |
{ |
66 |
|
|
67 |
/** |
/** |
78 |
* Constructs an FTP connection to the specified URL. |
* Constructs an FTP connection to the specified URL. |
79 |
* @param url the URL |
* @param url the URL |
80 |
*/ |
*/ |
81 |
public FTPURLConnection (URL url) |
public FTPURLConnection(URL url) |
82 |
{ |
{ |
83 |
super (url); |
super(url); |
84 |
passive = true; |
passive = true; |
85 |
representationType = FTPConnection.TYPE_BINARY; |
representationType = FTPConnection.TYPE_BINARY; |
86 |
fileStructure = -1; |
fileStructure = -1; |
90 |
/** |
/** |
91 |
* Establishes the connection. |
* Establishes the connection. |
92 |
*/ |
*/ |
93 |
public void connect () throws IOException |
public void connect() |
94 |
|
throws IOException |
95 |
{ |
{ |
96 |
if (connected) |
if (connected) |
97 |
{ |
{ |
98 |
return; |
return; |
99 |
} |
} |
100 |
String host = url.getHost (); |
String host = url.getHost(); |
101 |
int port = url.getPort (); |
int port = url.getPort(); |
102 |
String username = url.getUserInfo (); |
String username = url.getUserInfo(); |
103 |
String password = null; |
String password = null; |
104 |
if (username != null) |
if (username != null) |
105 |
{ |
{ |
106 |
int ci = username.indexOf (':'); |
int ci = username.indexOf(':'); |
107 |
if (ci != -1) |
if (ci != -1) |
108 |
{ |
{ |
109 |
password = username.substring (ci + 1); |
password = username.substring(ci + 1); |
110 |
username = username.substring (0, ci); |
username = username.substring(0, ci); |
111 |
} |
} |
112 |
} |
} |
113 |
else |
else |
114 |
{ |
{ |
115 |
username = "anonymous"; |
username = "anonymous"; |
116 |
PrivilegedAction a = new GetSystemPropertyAction ("user.name"); |
PrivilegedAction a = new GetSystemPropertyAction("user.name"); |
117 |
String systemUsername = (String) AccessController.doPrivileged (a); |
String systemUsername =(String) AccessController.doPrivileged(a); |
118 |
a = new GetLocalHostAction (); |
a = new GetLocalHostAction(); |
119 |
InetAddress localhost = (InetAddress) AccessController.doPrivileged (a); |
InetAddress localhost =(InetAddress) AccessController.doPrivileged(a); |
120 |
password = systemUsername + "@" + |
password = systemUsername + "@" + |
121 |
((localhost == null) ? "localhost" : localhost.getHostName ()); |
((localhost == null) ? "localhost" : localhost.getHostName()); |
122 |
} |
} |
123 |
connection = new FTPConnection (host, port); |
connection = new FTPConnection(host, port); |
124 |
if (!connection.authenticate (username, password)) |
if (!connection.authenticate(username, password)) |
125 |
{ |
{ |
126 |
throw new SecurityException ("Authentication failed"); |
throw new SecurityException("Authentication failed"); |
127 |
} |
} |
128 |
connection.setPassive (passive); |
connection.setPassive(passive); |
129 |
if (representationType != -1) |
if (representationType != -1) |
130 |
{ |
{ |
131 |
connection.setRepresentationType (representationType); |
connection.setRepresentationType(representationType); |
132 |
} |
} |
133 |
if (fileStructure != -1) |
if (fileStructure != -1) |
134 |
{ |
{ |
135 |
connection.setFileStructure (fileStructure); |
connection.setFileStructure(fileStructure); |
136 |
} |
} |
137 |
if (transferMode != -1) |
if (transferMode != -1) |
138 |
{ |
{ |
139 |
connection.setTransferMode (transferMode); |
connection.setTransferMode(transferMode); |
140 |
} |
} |
141 |
} |
} |
142 |
|
|
143 |
/** |
/** |
144 |
* This connection supports doInput. |
* This connection supports doInput. |
145 |
*/ |
*/ |
146 |
public void setDoInput (boolean doinput) |
public void setDoInput(boolean doinput) |
147 |
{ |
{ |
148 |
doInput = doinput; |
doInput = doinput; |
149 |
} |
} |
151 |
/** |
/** |
152 |
* This connection supports doOutput. |
* This connection supports doOutput. |
153 |
*/ |
*/ |
154 |
public void setDoOutput (boolean dooutput) |
public void setDoOutput(boolean dooutput) |
155 |
{ |
{ |
156 |
doOutput = dooutput; |
doOutput = dooutput; |
157 |
} |
} |
159 |
/** |
/** |
160 |
* Returns an input stream that reads from this open connection. |
* Returns an input stream that reads from this open connection. |
161 |
*/ |
*/ |
162 |
public InputStream getInputStream () throws IOException |
public InputStream getInputStream() |
163 |
|
throws IOException |
164 |
{ |
{ |
165 |
if (!connected) |
if (!connected) |
166 |
{ |
{ |
167 |
connect (); |
connect(); |
168 |
} |
} |
169 |
String path = url.getPath (); |
String path = url.getPath(); |
170 |
String filename = null; |
String filename = null; |
171 |
int lsi = path.lastIndexOf ('/'); |
int lsi = path.lastIndexOf('/'); |
172 |
if (lsi != -1) |
if (lsi != -1) |
173 |
{ |
{ |
174 |
filename = path.substring (lsi + 1); |
filename = path.substring(lsi + 1); |
175 |
path = path.substring (0, lsi); |
path = path.substring(0, lsi); |
176 |
if (!connection.changeWorkingDirectory (path)) |
if (!connection.changeWorkingDirectory(path)) |
177 |
{ |
{ |
178 |
throw new FileNotFoundException (path); |
throw new FileNotFoundException(path); |
179 |
} |
} |
180 |
} |
} |
181 |
if (filename != null && filename.length () > 0) |
if (filename != null && filename.length() > 0) |
182 |
{ |
{ |
183 |
return this.new ClosingInputStream (connection.retrieve (filename)); |
return this.new ClosingInputStream(connection.retrieve(filename)); |
184 |
} |
} |
185 |
else |
else |
186 |
{ |
{ |
187 |
return this.new ClosingInputStream (connection.list (null)); |
return this.new ClosingInputStream(connection.list(null)); |
188 |
} |
} |
189 |
} |
} |
190 |
|
|
191 |
/** |
/** |
192 |
* Returns an output stream that writes to this connection. |
* Returns an output stream that writes to this connection. |
193 |
*/ |
*/ |
194 |
public OutputStream getOutputStream () throws IOException |
public OutputStream getOutputStream() |
195 |
|
throws IOException |
196 |
{ |
{ |
197 |
if (!connected) |
if (!connected) |
198 |
{ |
{ |
199 |
connect (); |
connect(); |
200 |
} |
} |
201 |
String dir = url.getPath (); |
String dir = url.getPath(); |
202 |
String filename = url.getFile (); |
String filename = url.getFile(); |
203 |
if (!connection.changeWorkingDirectory (dir)) |
if (!connection.changeWorkingDirectory(dir)) |
204 |
{ |
{ |
205 |
throw new FileNotFoundException (dir); |
throw new FileNotFoundException(dir); |
206 |
} |
} |
207 |
if (filename != null) |
if (filename != null) |
208 |
{ |
{ |
209 |
return this.new ClosingOutputStream (connection.store (filename)); |
return this.new ClosingOutputStream(connection.store(filename)); |
210 |
} |
} |
211 |
else |
else |
212 |
{ |
{ |
213 |
throw new FileNotFoundException (filename); |
throw new FileNotFoundException(filename); |
214 |
} |
} |
215 |
} |
} |
216 |
|
|
217 |
public String getRequestProperty (String key) |
public String getRequestProperty(String key) |
218 |
{ |
{ |
219 |
if ("passive".equals (key)) |
if ("passive".equals(key)) |
220 |
{ |
{ |
221 |
return Boolean.toString (passive); |
return Boolean.toString(passive); |
222 |
} |
} |
223 |
else if ("representationType".equals (key)) |
else if ("representationType".equals(key)) |
224 |
{ |
{ |
225 |
switch (representationType) |
switch (representationType) |
226 |
{ |
{ |
232 |
return "BINARY"; |
return "BINARY"; |
233 |
} |
} |
234 |
} |
} |
235 |
else if ("fileStructure".equals (key)) |
else if ("fileStructure".equals(key)) |
236 |
{ |
{ |
237 |
switch (fileStructure) |
switch (fileStructure) |
238 |
{ |
{ |
244 |
return "PAGE"; |
return "PAGE"; |
245 |
} |
} |
246 |
} |
} |
247 |
else if ("transferMode".equals (key)) |
else if ("transferMode".equals(key)) |
248 |
{ |
{ |
249 |
switch (transferMode) |
switch (transferMode) |
250 |
{ |
{ |
259 |
return null; |
return null; |
260 |
} |
} |
261 |
|
|
262 |
public Map getRequestProperties () |
public Map getRequestProperties() |
263 |
{ |
{ |
264 |
Map map = new HashMap (); |
Map map = new HashMap(); |
265 |
addRequestPropertyValue (map, "passive"); |
addRequestPropertyValue(map, "passive"); |
266 |
addRequestPropertyValue (map, "representationType"); |
addRequestPropertyValue(map, "representationType"); |
267 |
addRequestPropertyValue (map, "fileStructure"); |
addRequestPropertyValue(map, "fileStructure"); |
268 |
addRequestPropertyValue (map, "transferMode"); |
addRequestPropertyValue(map, "transferMode"); |
269 |
return map; |
return map; |
270 |
} |
} |
271 |
|
|
272 |
private void addRequestPropertyValue (Map map, String key) |
private void addRequestPropertyValue(Map map, String key) |
273 |
{ |
{ |
274 |
String value = getRequestProperty (key); |
String value = getRequestProperty(key); |
275 |
map.put (key, value); |
map.put(key, value); |
276 |
} |
} |
277 |
|
|
278 |
public void setRequestProperty (String key, String value) |
public void setRequestProperty(String key, String value) |
279 |
{ |
{ |
280 |
if (connected) |
if (connected) |
281 |
{ |
{ |
282 |
throw new IllegalStateException (); |
throw new IllegalStateException(); |
283 |
} |
} |
284 |
if ("passive".equals (key)) |
if ("passive".equals(key)) |
285 |
{ |
{ |
286 |
passive = Boolean.valueOf (value).booleanValue (); |
passive = Boolean.valueOf(value).booleanValue(); |
287 |
} |
} |
288 |
else if ("representationType".equals (key)) |
else if ("representationType".equals(key)) |
289 |
{ |
{ |
290 |
if ("A".equalsIgnoreCase (value) || |
if ("A".equalsIgnoreCase(value) || |
291 |
"ASCII".equalsIgnoreCase (value)) |
"ASCII".equalsIgnoreCase(value)) |
292 |
{ |
{ |
293 |
representationType = FTPConnection.TYPE_ASCII; |
representationType = FTPConnection.TYPE_ASCII; |
294 |
} |
} |
295 |
else if ("E".equalsIgnoreCase (value) || |
else if ("E".equalsIgnoreCase(value) || |
296 |
"EBCDIC".equalsIgnoreCase (value)) |
"EBCDIC".equalsIgnoreCase(value)) |
297 |
{ |
{ |
298 |
representationType = FTPConnection.TYPE_EBCDIC; |
representationType = FTPConnection.TYPE_EBCDIC; |
299 |
} |
} |
300 |
else if ("I".equalsIgnoreCase (value) || |
else if ("I".equalsIgnoreCase(value) || |
301 |
"BINARY".equalsIgnoreCase (value)) |
"BINARY".equalsIgnoreCase(value)) |
302 |
{ |
{ |
303 |
representationType = FTPConnection.TYPE_BINARY; |
representationType = FTPConnection.TYPE_BINARY; |
304 |
} |
} |
305 |
else |
else |
306 |
{ |
{ |
307 |
throw new IllegalArgumentException (value); |
throw new IllegalArgumentException(value); |
308 |
} |
} |
309 |
} |
} |
310 |
else if ("fileStructure".equals (key)) |
else if ("fileStructure".equals(key)) |
311 |
{ |
{ |
312 |
if ("F".equalsIgnoreCase (value) || |
if ("F".equalsIgnoreCase(value) || |
313 |
"FILE".equalsIgnoreCase (value)) |
"FILE".equalsIgnoreCase(value)) |
314 |
{ |
{ |
315 |
fileStructure = FTPConnection.STRUCTURE_FILE; |
fileStructure = FTPConnection.STRUCTURE_FILE; |
316 |
} |
} |
317 |
else if ("R".equalsIgnoreCase (value) || |
else if ("R".equalsIgnoreCase(value) || |
318 |
"RECORD".equalsIgnoreCase (value)) |
"RECORD".equalsIgnoreCase(value)) |
319 |
{ |
{ |
320 |
fileStructure = FTPConnection.STRUCTURE_RECORD; |
fileStructure = FTPConnection.STRUCTURE_RECORD; |
321 |
} |
} |
322 |
else if ("P".equalsIgnoreCase (value) || |
else if ("P".equalsIgnoreCase(value) || |
323 |
"PAGE".equalsIgnoreCase (value)) |
"PAGE".equalsIgnoreCase(value)) |
324 |
{ |
{ |
325 |
fileStructure = FTPConnection.STRUCTURE_PAGE; |
fileStructure = FTPConnection.STRUCTURE_PAGE; |
326 |
} |
} |
327 |
else |
else |
328 |
{ |
{ |
329 |
throw new IllegalArgumentException (value); |
throw new IllegalArgumentException(value); |
330 |
} |
} |
331 |
} |
} |
332 |
else if ("transferMode".equals (key)) |
else if ("transferMode".equals(key)) |
333 |
{ |
{ |
334 |
if ("S".equalsIgnoreCase (value) || |
if ("S".equalsIgnoreCase(value) || |
335 |
"STREAM".equalsIgnoreCase (value)) |
"STREAM".equalsIgnoreCase(value)) |
336 |
{ |
{ |
337 |
transferMode = FTPConnection.MODE_STREAM; |
transferMode = FTPConnection.MODE_STREAM; |
338 |
} |
} |
339 |
else if ("B".equalsIgnoreCase (value) || |
else if ("B".equalsIgnoreCase(value) || |
340 |
"BLOCK".equalsIgnoreCase (value)) |
"BLOCK".equalsIgnoreCase(value)) |
341 |
{ |
{ |
342 |
transferMode = FTPConnection.MODE_BLOCK; |
transferMode = FTPConnection.MODE_BLOCK; |
343 |
} |
} |
344 |
else if ("C".equalsIgnoreCase (value) || |
else if ("C".equalsIgnoreCase(value) || |
345 |
"COMPRESSED".equalsIgnoreCase (value)) |
"COMPRESSED".equalsIgnoreCase(value)) |
346 |
{ |
{ |
347 |
transferMode = FTPConnection.MODE_COMPRESSED; |
transferMode = FTPConnection.MODE_COMPRESSED; |
348 |
} |
} |
349 |
else |
else |
350 |
{ |
{ |
351 |
throw new IllegalArgumentException (value); |
throw new IllegalArgumentException(value); |
352 |
} |
} |
353 |
} |
} |
354 |
} |
} |
355 |
|
|
356 |
public void addRequestProperty (String key, String value) |
public void addRequestProperty(String key, String value) |
357 |
{ |
{ |
358 |
setRequestProperty (key, value); |
setRequestProperty(key, value); |
359 |
} |
} |
360 |
|
|
361 |
class ClosingInputStream |
class ClosingInputStream |
362 |
extends FilterInputStream |
extends FilterInputStream |
363 |
{ |
{ |
364 |
|
|
365 |
ClosingInputStream (InputStream in) |
ClosingInputStream(InputStream in) |
366 |
{ |
{ |
367 |
super (in); |
super(in); |
368 |
} |
} |
369 |
|
|
370 |
public void close () |
public void close() |
371 |
throws IOException |
throws IOException |
372 |
{ |
{ |
373 |
super.close (); |
super.close(); |
374 |
connection.logout (); |
connection.logout(); |
375 |
} |
} |
376 |
|
|
377 |
} |
} |
380 |
extends FilterOutputStream |
extends FilterOutputStream |
381 |
{ |
{ |
382 |
|
|
383 |
ClosingOutputStream (OutputStream out) |
ClosingOutputStream(OutputStream out) |
384 |
{ |
{ |
385 |
super (out); |
super(out); |
386 |
} |
} |
387 |
|
|
388 |
public void close () |
public void close() |
389 |
throws IOException |
throws IOException |
390 |
{ |
{ |
391 |
super.close (); |
super.close(); |
392 |
connection.logout (); |
connection.logout(); |
393 |
} |
} |
394 |
|
|
395 |
} |
} |
396 |
|
|
397 |
} |
} |
398 |
|
|