This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / gnu / java / net / protocol / ftp / FTPURLConnection.java
blob368c035c33902065c5e115c37d537c7d4ace8010
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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;
51 import java.net.URL;
52 import java.net.URLConnection;
53 import java.security.AccessController;
54 import java.security.PrivilegedAction;
55 import java.util.HashMap;
56 import java.util.Map;
58 /**
59 * An FTP URL connection.
61 * @author Chris Burdess (dog@gnu.org)
63 public class FTPURLConnection
64 extends URLConnection
67 /**
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;
77 /**
78 * Constructs an FTP connection to the specified URL.
79 * @param url the URL
81 public FTPURLConnection(URL url)
83 super(url);
84 passive = true;
85 representationType = FTPConnection.TYPE_BINARY;
86 fileStructure = -1;
87 transferMode = -1;
90 /**
91 * Establishes the connection.
93 public void connect()
94 throws IOException
96 if (connected)
98 return;
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(':');
107 if (ci != -1)
109 password = username.substring(ci + 1);
110 username = username.substring(0, ci);
113 else
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)
148 doInput = doinput;
152 * This connection supports doOutput.
154 public void setDoOutput(boolean dooutput)
156 doOutput = dooutput;
160 * Returns an input stream that reads from this open connection.
162 public InputStream getInputStream()
163 throws IOException
165 if (!connected)
167 connect();
169 String path = url.getPath();
170 String filename = null;
171 int lsi = path.lastIndexOf('/');
172 if (lsi != -1)
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));
185 else
187 return this.new ClosingInputStream(connection.list(null));
192 * Returns an output stream that writes to this connection.
194 public OutputStream getOutputStream()
195 throws IOException
197 if (!connected)
199 connect();
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));
211 else
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:
228 return "ASCII";
229 case FTPConnection.TYPE_EBCDIC:
230 return "EBCDIC";
231 case FTPConnection.TYPE_BINARY:
232 return "BINARY";
235 else if ("fileStructure".equals(key))
237 switch (fileStructure)
239 case FTPConnection.STRUCTURE_FILE:
240 return "FILE";
241 case FTPConnection.STRUCTURE_RECORD:
242 return "RECORD";
243 case FTPConnection.STRUCTURE_PAGE:
244 return "PAGE";
247 else if ("transferMode".equals(key))
249 switch (transferMode)
251 case FTPConnection.MODE_STREAM:
252 return "STREAM";
253 case FTPConnection.MODE_BLOCK:
254 return "BLOCK";
255 case FTPConnection.MODE_COMPRESSED:
256 return "COMPRESSED";
259 return null;
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");
269 return map;
272 private void addRequestPropertyValue(Map map, String key)
274 String value = getRequestProperty(key);
275 map.put(key, value);
278 public void setRequestProperty(String key, String value)
280 if (connected)
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;
305 else
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;
327 else
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;
349 else
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)
367 super(in);
370 public void close()
371 throws IOException
373 super.close();
374 connection.logout();
379 class ClosingOutputStream
380 extends FilterOutputStream
383 ClosingOutputStream(OutputStream out)
385 super(out);
388 public void close()
389 throws IOException
391 super.close();
392 connection.logout();