1 /*
2 * $HeadURL: $
3 * $Date: $
4 * $Revision: $
5 * $Author: $
6 *
7 * Copyright (c) 2005 MindTree Consulting Ltd.
8 *
9 * This file is part of Insight.
10 *
11 * Insight is free software: you can redistribute it and/or modify it under the
12 * terms of the GNU General Public License as published by the Free Software
13 * Foundation, either version 3 of the License, or (at your option) any later
14 * version.
15 *
16 * Insight is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * Insight. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 package com.mindtree.techworks.insight.download.ftpbrowse;
26
27 import java.io.File;
28 import java.io.FileFilter;
29 import java.io.FilenameFilter;
30 import java.io.IOException;
31 import java.net.MalformedURLException;
32 import java.net.PasswordAuthentication;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.net.URL;
36
37 import javax.swing.filechooser.FileSystemView;
38
39 import org.apache.commons.net.ftp.FTPFile;
40
41
42 /**
43 * This class is a wrapper for <code>FTPFile</code>. This class is to be used
44 * by {@link javax.swing.JFileChooser JFileChooser}which requires an instance
45 * of <code>File</code> to it.
46 * <p>
47 * Most operations on the file wrapped by this class are <b>not allowed </b>.
48 * <code>FTPFile</code> is meant to contain information about files on a
49 * remote server, and <code>FTPFileFile</code> is also meant for the same. It
50 * merely provides a <code>File</code> like interface to <code>FTPFile</code>.
51 * </p>
52 * <p>
53 * For any operations on the file, use the information about the file from an
54 * instance of this class and use
55 * {@link org.apache.commons.net.ftp.FTPClient FTPClient}or any wrapper around
56 * it. You might use any other FTPClient also.
57 * </p>
58 * <p>
59 * The {@link FTPFileFile#getHostURL() getHostURL}and
60 * {@link FTPFileFile#getPasswordAuthentication() getPasswordAuthentication}
61 * methods return the host and authentication information to connect to the
62 * host.
63 * </p>
64 *
65 * @see org.apache.commons.net.ftp.FTPFile FTPFile
66 * @see java.io.File File
67 * @author Bindul Bhowmik
68 * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
69 */
70 public class FTPFileFile extends File {
71
72 /**
73 * Used for object serialization
74 */
75 private static final long serialVersionUID = 4465086823354456311L;
76
77 //
78 // Instance variables
79 //
80
81 /**
82 * The ftpFile instance backing the wrapper
83 */
84 private FTPFile ftpFile;
85
86 /**
87 * The file system view which created this.
88 */
89 private FileSystemView fileSystemView;
90
91 /**
92 * The URL associated with this file.
93 */
94 private URL hostURL;
95
96 /**
97 * User credentials for the remote host.
98 */
99 private PasswordAuthentication passwordAuthentication;
100
101 //
102 // Constructors
103 //
104
105 /**
106 * Constructs a new FTPFileFile object.
107 *
108 * @param ftpFile The FTPFile instance to wrap
109 * @param fileSystemView The file system view which created this instance.
110 */
111 public FTPFileFile (FTPFile ftpFile, FileSystemView fileSystemView) {
112
113 super(ftpFile.getName());
114 this.ftpFile = ftpFile;
115 this.fileSystemView = fileSystemView;
116 if (null != fileSystemView
117 && fileSystemView instanceof FTPRemoteFileSystemView) {
118 FTPRemoteFileSystemView ftpFileSystemView = (FTPRemoteFileSystemView) fileSystemView;
119 this.hostURL = ftpFileSystemView.getUrl();
120 this.passwordAuthentication = ftpFileSystemView
121 .getPasswordAuthentication();
122 }
123 }
124
125 //
126 // Accessors
127 //
128
129 /**
130 * @return Returns the passwordAuthentication.
131 */
132 public PasswordAuthentication getPasswordAuthentication () {
133
134 return passwordAuthentication;
135 }
136
137 /**
138 * @return Returns the hostURL.
139 */
140 public URL getHostURL () {
141
142 return hostURL;
143 }
144
145 //
146 // Overridden methods from java.io.File
147 //
148
149 /**
150 * @return Returns the ftpFile.
151 */
152 public FTPFile getFtpFile () {
153
154 return ftpFile;
155 }
156
157 /**
158 * @see java.io.File#isAbsolute()
159 */
160 public boolean isAbsolute () {
161
162 return !ftpFile.isSymbolicLink();
163 }
164
165 /**
166 * @see java.io.File#isDirectory()
167 */
168 public boolean isDirectory () {
169
170 return ftpFile.isDirectory();
171 }
172
173 /**
174 * @see java.io.File#isFile()
175 */
176 public boolean isFile () {
177
178 return ftpFile.isFile();
179 }
180
181 /**
182 * @see java.io.File#lastModified()
183 */
184 public long lastModified () {
185
186 return ftpFile.getTimestamp().getTimeInMillis();
187 }
188
189
190 /**
191 * Always returns true. Read access is always allowed.
192 *
193 * @see java.io.File#canRead()
194 */
195 public boolean canRead () {
196
197 return true;
198 }
199
200 /**
201 * Always returns false. For more information, see note
202 * {@link FTPFileFile above}.
203 *
204 * @see java.io.File#canWrite()
205 */
206 public boolean canWrite () {
207
208 return false;
209 }
210
211 /**
212 * @see java.io.File#compareTo(java.io.File)
213 */
214 public int compareTo (File pathname) {
215
216 return this.getPath().compareToIgnoreCase(pathname.getPath());
217 }
218
219 /**
220 * @see java.lang.Comparable#compareTo(java.lang.Object)
221 */
222 public int compareTo (Object o) {
223
224 return compareTo((File) o);
225 }
226
227 /**
228 * Operation not supported.
229 *
230 * @throws IOException Since this operation is not supported.
231 * @see java.io.File#createNewFile()
232 */
233 public boolean createNewFile () throws IOException {
234
235 throw new IOException("READ-ONLY view");
236 }
237
238 /**
239 * Operation not supported, always returns false
240 *
241 * @throws UnsupportedOperationException Since this operation is not
242 * supported.
243 * @see java.io.File#delete()
244 */
245 public boolean delete () {
246
247 return false;
248 }
249
250 /**
251 * Operation not supported. Nothing is returned.
252 *
253 * @see java.io.File#deleteOnExit()
254 */
255 public void deleteOnExit () {
256
257 // No Operation
258 }
259
260 /**
261 * @see java.lang.Object#equals(java.lang.Object)
262 */
263 public boolean equals (Object obj) {
264
265 if ((obj != null) && (obj instanceof File)) {
266 return compareTo((File) obj) == 0;
267 }
268 return false;
269 }
270
271 /**
272 * Returns <code>true</code> always. Any file for which an FTPFile
273 * instance can be got is assumed to exist.
274 *
275 * @see java.io.File#exists()
276 */
277 public boolean exists () {
278
279 return true;
280 }
281
282 /**
283 * Returns the same instance!
284 *
285 * @see java.io.File#getAbsoluteFile()
286 */
287 public File getAbsoluteFile () {
288
289 return this;
290 }
291
292 /**
293 * Returns the path!
294 *
295 * @see java.io.File#getAbsolutePath()
296 */
297 public String getAbsolutePath () {
298
299 return this.getPath();
300 }
301
302 /**
303 * Returns the instance as the canonical file
304 *
305 * @see java.io.File#getCanonicalFile()
306 */
307 public File getCanonicalFile () throws IOException {
308
309 return this;
310 }
311
312 /**
313 * Always returns the unix style path from the root.
314 *
315 * @see java.io.File#getCanonicalPath()
316 */
317 public String getCanonicalPath () throws IOException {
318
319 return this.getPath();
320 }
321
322 /**
323 * Returns the name of the file
324 *
325 * @see java.io.File#getName()
326 */
327 public String getName () {
328
329 String path = this.getPath();
330 int index = path.lastIndexOf('/');
331 return path.substring(index + 1);
332 }
333
334 /**
335 * Returns the path of the parent, if a file system view is present, or
336 * <code>null</code>.
337 *
338 * @see java.io.File#getParent()
339 */
340 public String getParent () {
341
342 if (null != fileSystemView) {
343 return fileSystemView.getParentDirectory(this).getPath();
344 } else {
345 return null;
346 }
347 }
348
349 /**
350 * Returns the parent directory, if a file system view is present, or
351 * <code>null</code>.
352 *
353 * @see java.io.File#getParentFile()
354 */
355 public File getParentFile () {
356
357 if (null != fileSystemView) {
358 return fileSystemView.getParentDirectory(this);
359 } else {
360 return null;
361 }
362 }
363
364 /**
365 * Returns the path in the unix style.
366 *
367 * @see java.io.File#getPath()
368 */
369 public String getPath () {
370
371 // We always construct the FTPFile name with the complete path of the
372 // FTPFile, so the name will be the path. If this assumption is changed,
373 // need to change this implementation.
374 return this.ftpFile.getName();
375 }
376
377 /**
378 * Returns false. No files are hidden!
379 *
380 * @see java.io.File#isHidden()
381 */
382 public boolean isHidden () {
383
384 return false;
385 }
386
387 /**
388 * Returns the size of the FTP file instance
389 *
390 * @see java.io.File#length()
391 */
392 public long length () {
393
394 return ftpFile.getSize();
395 }
396
397 /**
398 * Operation is not supported. Use the
399 * {@link FTPRemoteFileSystemView FTPRemoteFileSystemView}for this.
400 *
401 * @see java.io.File#list()
402 * @return An empty String array
403 */
404 public String [] list () {
405
406 return new String [0];
407 }
408
409 /**
410 * Operation is not supported. Use the
411 * {@link FTPRemoteFileSystemView FTPRemoteFileSystemView}for this.
412 *
413 * @see java.io.File#list(java.io.FilenameFilter)
414 * @return An empty String array
415 */
416 public String [] list (FilenameFilter filter) {
417
418 return new String [0];
419 }
420
421 /**
422 * Operation is not supported. Use the
423 * {@link FTPRemoteFileSystemView FTPRemoteFileSystemView}for this.
424 *
425 * @see java.io.File#listFiles()
426 * @return An empty File array
427 */
428 public File [] listFiles () {
429
430 return new FTPFileFile [0];
431 }
432
433 /**
434 * Operation is not supported. Use the
435 * {@link FTPRemoteFileSystemView FTPRemoteFileSystemView}for this.
436 *
437 * @see java.io.File#listFiles(java.io.FileFilter)
438 * @return An empty File array
439 */
440 public File [] listFiles (FileFilter filter) {
441
442 return new FTPFileFile [0];
443 }
444
445 /**
446 * Operation is not supported. Use the
447 * {@link FTPRemoteFileSystemView FTPRemoteFileSystemView}for this.
448 *
449 * @see java.io.File#listFiles(java.io.FilenameFilter)
450 * @return An empty File array
451 */
452 public File [] listFiles (FilenameFilter filter) {
453
454 return new FTPFileFile [0];
455 }
456
457 /**
458 * The operation is not supported, always returns <code>false</code>
459 *
460 * @see java.io.File#mkdir()
461 */
462 public boolean mkdir () {
463
464 return false;
465 }
466
467 /**
468 * The operation is not supported, always returns <code>false</code>
469 *
470 * @see java.io.File#mkdirs()
471 */
472 public boolean mkdirs () {
473
474 return false;
475 }
476
477 /**
478 * The operation is not supported, always returns <code>false</code>
479 *
480 * @see java.io.File#renameTo(java.io.File)
481 */
482 public boolean renameTo (File dest) {
483
484 return false;
485 }
486
487 /**
488 * The operation is not supported, always returns <code>false</code>
489 *
490 * @see java.io.File#setLastModified(long)
491 */
492 public boolean setLastModified (long time) {
493
494 return false;
495 }
496
497 /**
498 * The operation is not supported, always returns <code>false</code>
499 *
500 * @see java.io.File#setReadOnly()
501 */
502 public boolean setReadOnly () {
503
504 return false;
505 }
506
507 /**
508 * @see java.io.File#toURI()
509 */
510 public URI toURI () {
511
512 try {
513 String p = slashify(this.getPath(), this.isDirectory());
514 if (p.startsWith("//"))
515 p = "//" + p;
516 return new URI("file", null, p, null);
517 } catch (URISyntaxException x) {
518 throw new Error(x); // Can't happen
519 }
520 }
521
522 /**
523 * @see java.io.File#toURL()
524 */
525 public URL toURL () throws MalformedURLException {
526
527 return new URL("file", "", slashify(this.getPath(), this.isDirectory()));
528 }
529
530 /**
531 * Creates a slash representation of the file
532 *
533 * @param path The path to slashify
534 * @param isDirectory Is it a directory
535 * @return The slashified path
536 */
537 private String slashify (String path, boolean isDirectory) {
538
539 String p = path;
540 if (!p.startsWith("/"))
541 p = "/" + p;
542 if (!p.endsWith("/") && isDirectory)
543 p = p + "/";
544 return p;
545 }
546 }