1 /* FTPURLConnection.java --
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.net
.protocol
.ftp
;
41 import gnu
.java
.net
.GetLocalHostAction
;
42 import gnu
.java
.security
.action
.GetPropertyAction
;
44 import java
.io
.FileNotFoundException
;
45 import java
.io
.FilterInputStream
;
46 import java
.io
.FilterOutputStream
;
47 import java
.io
.InputStream
;
48 import java
.io
.IOException
;
49 import java
.io
.OutputStream
;
50 import java
.net
.InetAddress
;
52 import java
.net
.URLConnection
;
53 import java
.security
.AccessController
;
54 import java
.security
.PrivilegedAction
;
55 import java
.util
.HashMap
;
59 * An FTP URL connection.
61 * @author Chris Burdess (dog@gnu.org)
63 public class FTPURLConnection
68 * The connection managing the protocol exchange.
70 protected FTPConnection connection
;
72 protected boolean passive
;
73 protected int representationType
;
74 protected int fileStructure
;
75 protected int transferMode
;
78 * Constructs an FTP connection to the specified URL.
81 public FTPURLConnection(URL url
)
85 representationType
= FTPConnection
.TYPE_BINARY
;
91 * Establishes the connection.
100 String host
= url
.getHost();
101 int port
= url
.getPort();
102 String username
= url
.getUserInfo();
103 String password
= null;
104 if (username
!= null)
106 int ci
= username
.indexOf(':');
109 password
= username
.substring(ci
+ 1);
110 username
= username
.substring(0, ci
);
115 username
= "anonymous";
116 PrivilegedAction a
= new GetPropertyAction("user.name");
117 String systemUsername
=(String
) AccessController
.doPrivileged(a
);
118 a
= new GetLocalHostAction();
119 InetAddress localhost
=(InetAddress
) AccessController
.doPrivileged(a
);
120 password
= systemUsername
+ "@" +
121 ((localhost
== null) ?
"localhost" : localhost
.getHostName());
123 connection
= new FTPConnection(host
, port
);
124 if (!connection
.authenticate(username
, password
))
126 throw new SecurityException("Authentication failed");
128 connection
.setPassive(passive
);
129 if (representationType
!= -1)
131 connection
.setRepresentationType(representationType
);
133 if (fileStructure
!= -1)
135 connection
.setFileStructure(fileStructure
);
137 if (transferMode
!= -1)
139 connection
.setTransferMode(transferMode
);
144 * This connection supports doInput.
146 public void setDoInput(boolean doinput
)
152 * This connection supports doOutput.
154 public void setDoOutput(boolean dooutput
)
160 * Returns an input stream that reads from this open connection.
162 public InputStream
getInputStream()
169 String path
= url
.getPath();
170 String filename
= null;
171 int lsi
= path
.lastIndexOf('/');
174 filename
= path
.substring(lsi
+ 1);
175 path
= path
.substring(0, lsi
);
176 if (!connection
.changeWorkingDirectory(path
))
178 throw new FileNotFoundException(path
);
181 if (filename
!= null && filename
.length() > 0)
183 return this.new ClosingInputStream(connection
.retrieve(filename
));
187 return this.new ClosingInputStream(connection
.list(null));
192 * Returns an output stream that writes to this connection.
194 public OutputStream
getOutputStream()
201 String dir
= url
.getPath();
202 String filename
= url
.getFile();
203 if (!connection
.changeWorkingDirectory(dir
))
205 throw new FileNotFoundException(dir
);
207 if (filename
!= null)
209 return this.new ClosingOutputStream(connection
.store(filename
));
213 throw new FileNotFoundException(filename
);
217 public String
getRequestProperty(String key
)
219 if ("passive".equals(key
))
221 return Boolean
.toString(passive
);
223 else if ("representationType".equals(key
))
225 switch (representationType
)
227 case FTPConnection
.TYPE_ASCII
:
229 case FTPConnection
.TYPE_EBCDIC
:
231 case FTPConnection
.TYPE_BINARY
:
235 else if ("fileStructure".equals(key
))
237 switch (fileStructure
)
239 case FTPConnection
.STRUCTURE_FILE
:
241 case FTPConnection
.STRUCTURE_RECORD
:
243 case FTPConnection
.STRUCTURE_PAGE
:
247 else if ("transferMode".equals(key
))
249 switch (transferMode
)
251 case FTPConnection
.MODE_STREAM
:
253 case FTPConnection
.MODE_BLOCK
:
255 case FTPConnection
.MODE_COMPRESSED
:
262 public Map
getRequestProperties()
264 Map map
= new HashMap();
265 addRequestPropertyValue(map
, "passive");
266 addRequestPropertyValue(map
, "representationType");
267 addRequestPropertyValue(map
, "fileStructure");
268 addRequestPropertyValue(map
, "transferMode");
272 private void addRequestPropertyValue(Map map
, String key
)
274 String value
= getRequestProperty(key
);
278 public void setRequestProperty(String key
, String value
)
282 throw new IllegalStateException();
284 if ("passive".equals(key
))
286 passive
= Boolean
.valueOf(value
).booleanValue();
288 else if ("representationType".equals(key
))
290 if ("A".equalsIgnoreCase(value
) ||
291 "ASCII".equalsIgnoreCase(value
))
293 representationType
= FTPConnection
.TYPE_ASCII
;
295 else if ("E".equalsIgnoreCase(value
) ||
296 "EBCDIC".equalsIgnoreCase(value
))
298 representationType
= FTPConnection
.TYPE_EBCDIC
;
300 else if ("I".equalsIgnoreCase(value
) ||
301 "BINARY".equalsIgnoreCase(value
))
303 representationType
= FTPConnection
.TYPE_BINARY
;
307 throw new IllegalArgumentException(value
);
310 else if ("fileStructure".equals(key
))
312 if ("F".equalsIgnoreCase(value
) ||
313 "FILE".equalsIgnoreCase(value
))
315 fileStructure
= FTPConnection
.STRUCTURE_FILE
;
317 else if ("R".equalsIgnoreCase(value
) ||
318 "RECORD".equalsIgnoreCase(value
))
320 fileStructure
= FTPConnection
.STRUCTURE_RECORD
;
322 else if ("P".equalsIgnoreCase(value
) ||
323 "PAGE".equalsIgnoreCase(value
))
325 fileStructure
= FTPConnection
.STRUCTURE_PAGE
;
329 throw new IllegalArgumentException(value
);
332 else if ("transferMode".equals(key
))
334 if ("S".equalsIgnoreCase(value
) ||
335 "STREAM".equalsIgnoreCase(value
))
337 transferMode
= FTPConnection
.MODE_STREAM
;
339 else if ("B".equalsIgnoreCase(value
) ||
340 "BLOCK".equalsIgnoreCase(value
))
342 transferMode
= FTPConnection
.MODE_BLOCK
;
344 else if ("C".equalsIgnoreCase(value
) ||
345 "COMPRESSED".equalsIgnoreCase(value
))
347 transferMode
= FTPConnection
.MODE_COMPRESSED
;
351 throw new IllegalArgumentException(value
);
356 public void addRequestProperty(String key
, String value
)
358 setRequestProperty(key
, value
);
361 class ClosingInputStream
362 extends FilterInputStream
365 ClosingInputStream(InputStream in
)
379 class ClosingOutputStream
380 extends FilterOutputStream
383 ClosingOutputStream(OutputStream out
)