1:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: import ;
56: import ;
57:
58:
63: public class FTPURLConnection
64: extends URLConnection
65: {
66:
67:
70: protected FTPConnection connection;
71:
72: protected boolean passive;
73: protected int representationType;
74: protected int fileStructure;
75: protected int transferMode;
76:
77:
81: public FTPURLConnection(URL url)
82: {
83: super(url);
84: passive = true;
85: representationType = FTPConnection.TYPE_BINARY;
86: fileStructure = -1;
87: transferMode = -1;
88: }
89:
90:
93: public void connect()
94: throws IOException
95: {
96: if (connected)
97: {
98: return;
99: }
100: String host = url.getHost();
101: int port = url.getPort();
102: String username = url.getUserInfo();
103: String password = null;
104: if (username != null)
105: {
106: int ci = username.indexOf(':');
107: if (ci != -1)
108: {
109: password = username.substring(ci + 1);
110: username = username.substring(0, ci);
111: }
112: }
113: else
114: {
115: username = "anonymous";
116: PrivilegedAction a = new GetSystemPropertyAction("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());
122: }
123: connection = new FTPConnection(host, port);
124: if (!connection.authenticate(username, password))
125: {
126: throw new SecurityException("Authentication failed");
127: }
128: connection.setPassive(passive);
129: if (representationType != -1)
130: {
131: connection.setRepresentationType(representationType);
132: }
133: if (fileStructure != -1)
134: {
135: connection.setFileStructure(fileStructure);
136: }
137: if (transferMode != -1)
138: {
139: connection.setTransferMode(transferMode);
140: }
141: }
142:
143:
146: public void setDoInput(boolean doinput)
147: {
148: doInput = doinput;
149: }
150:
151:
154: public void setDoOutput(boolean dooutput)
155: {
156: doOutput = dooutput;
157: }
158:
159:
162: public InputStream getInputStream()
163: throws IOException
164: {
165: if (!connected)
166: {
167: connect();
168: }
169: String path = url.getPath();
170: String filename = null;
171: int lsi = path.lastIndexOf('/');
172: if (lsi != -1)
173: {
174: filename = path.substring(lsi + 1);
175: path = path.substring(0, lsi);
176: if (!connection.changeWorkingDirectory(path))
177: {
178: throw new FileNotFoundException(path);
179: }
180: }
181: if (filename != null && filename.length() > 0)
182: {
183: return this.new ClosingInputStream(connection.retrieve(filename));
184: }
185: else
186: {
187: return this.new ClosingInputStream(connection.list(null));
188: }
189: }
190:
191:
194: public OutputStream getOutputStream()
195: throws IOException
196: {
197: if (!connected)
198: {
199: connect();
200: }
201: String dir = url.getPath();
202: String filename = url.getFile();
203: if (!connection.changeWorkingDirectory(dir))
204: {
205: throw new FileNotFoundException(dir);
206: }
207: if (filename != null)
208: {
209: return this.new ClosingOutputStream(connection.store(filename));
210: }
211: else
212: {
213: throw new FileNotFoundException(filename);
214: }
215: }
216:
217: public String getRequestProperty(String key)
218: {
219: if ("passive".equals(key))
220: {
221: return Boolean.toString(passive);
222: }
223: else if ("representationType".equals(key))
224: {
225: switch (representationType)
226: {
227: case FTPConnection.TYPE_ASCII:
228: return "ASCII";
229: case FTPConnection.TYPE_EBCDIC:
230: return "EBCDIC";
231: case FTPConnection.TYPE_BINARY:
232: return "BINARY";
233: }
234: }
235: else if ("fileStructure".equals(key))
236: {
237: switch (fileStructure)
238: {
239: case FTPConnection.STRUCTURE_FILE:
240: return "FILE";
241: case FTPConnection.STRUCTURE_RECORD:
242: return "RECORD";
243: case FTPConnection.STRUCTURE_PAGE:
244: return "PAGE";
245: }
246: }
247: else if ("transferMode".equals(key))
248: {
249: switch (transferMode)
250: {
251: case FTPConnection.MODE_STREAM:
252: return "STREAM";
253: case FTPConnection.MODE_BLOCK:
254: return "BLOCK";
255: case FTPConnection.MODE_COMPRESSED:
256: return "COMPRESSED";
257: }
258: }
259: return null;
260: }
261:
262: public Map getRequestProperties()
263: {
264: Map map = new HashMap();
265: addRequestPropertyValue(map, "passive");
266: addRequestPropertyValue(map, "representationType");
267: addRequestPropertyValue(map, "fileStructure");
268: addRequestPropertyValue(map, "transferMode");
269: return map;
270: }
271:
272: private void addRequestPropertyValue(Map map, String key)
273: {
274: String value = getRequestProperty(key);
275: map.put(key, value);
276: }
277:
278: public void setRequestProperty(String key, String value)
279: {
280: if (connected)
281: {
282: throw new IllegalStateException();
283: }
284: if ("passive".equals(key))
285: {
286: passive = Boolean.valueOf(value).booleanValue();
287: }
288: else if ("representationType".equals(key))
289: {
290: if ("A".equalsIgnoreCase(value) ||
291: "ASCII".equalsIgnoreCase(value))
292: {
293: representationType = FTPConnection.TYPE_ASCII;
294: }
295: else if ("E".equalsIgnoreCase(value) ||
296: "EBCDIC".equalsIgnoreCase(value))
297: {
298: representationType = FTPConnection.TYPE_EBCDIC;
299: }
300: else if ("I".equalsIgnoreCase(value) ||
301: "BINARY".equalsIgnoreCase(value))
302: {
303: representationType = FTPConnection.TYPE_BINARY;
304: }
305: else
306: {
307: throw new IllegalArgumentException(value);
308: }
309: }
310: else if ("fileStructure".equals(key))
311: {
312: if ("F".equalsIgnoreCase(value) ||
313: "FILE".equalsIgnoreCase(value))
314: {
315: fileStructure = FTPConnection.STRUCTURE_FILE;
316: }
317: else if ("R".equalsIgnoreCase(value) ||
318: "RECORD".equalsIgnoreCase(value))
319: {
320: fileStructure = FTPConnection.STRUCTURE_RECORD;
321: }
322: else if ("P".equalsIgnoreCase(value) ||
323: "PAGE".equalsIgnoreCase(value))
324: {
325: fileStructure = FTPConnection.STRUCTURE_PAGE;
326: }
327: else
328: {
329: throw new IllegalArgumentException(value);
330: }
331: }
332: else if ("transferMode".equals(key))
333: {
334: if ("S".equalsIgnoreCase(value) ||
335: "STREAM".equalsIgnoreCase(value))
336: {
337: transferMode = FTPConnection.MODE_STREAM;
338: }
339: else if ("B".equalsIgnoreCase(value) ||
340: "BLOCK".equalsIgnoreCase(value))
341: {
342: transferMode = FTPConnection.MODE_BLOCK;
343: }
344: else if ("C".equalsIgnoreCase(value) ||
345: "COMPRESSED".equalsIgnoreCase(value))
346: {
347: transferMode = FTPConnection.MODE_COMPRESSED;
348: }
349: else
350: {
351: throw new IllegalArgumentException(value);
352: }
353: }
354: }
355:
356: public void addRequestProperty(String key, String value)
357: {
358: setRequestProperty(key, value);
359: }
360:
361: class ClosingInputStream
362: extends FilterInputStream
363: {
364:
365: ClosingInputStream(InputStream in)
366: {
367: super(in);
368: }
369:
370: public void close()
371: throws IOException
372: {
373: super.close();
374: connection.logout();
375: }
376:
377: }
378:
379: class ClosingOutputStream
380: extends FilterOutputStream
381: {
382:
383: ClosingOutputStream(OutputStream out)
384: {
385: super(out);
386: }
387:
388: public void close()
389: throws IOException
390: {
391: super.close();
392: connection.logout();
393: }
394:
395: }
396:
397: }