1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.mindtree.techworks.insight.download.ftpbrowse;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.InetAddress;
30 import java.net.MalformedURLException;
31 import java.net.PasswordAuthentication;
32 import java.net.SocketException;
33 import java.net.URL;
34 import java.net.UnknownHostException;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import javax.swing.filechooser.FileSystemView;
39
40 import org.apache.commons.net.ftp.FTPClient;
41 import org.apache.commons.net.ftp.FTPFile;
42 import org.apache.commons.net.ftp.FTPListParseEngine;
43 import org.apache.commons.net.ftp.FTPReply;
44
45
46
47
48
49
50
51
52
53
54 public class FTPRemoteFileSystemView extends FileSystemView {
55
56
57
58
59
60
61
62
63 protected static final Logger logger = Logger
64 .getLogger(FTPRemoteFileSystemView.class.getName());
65
66
67
68
69 public static final PasswordAuthentication anonPassAuth = new PasswordAuthentication(
70 "anonymous", "insight@mindtree.com".toCharArray());
71
72
73
74
75 protected static final String FILE_SYSTEM_ROOT_NAME = "/";
76
77
78
79
80 public static final String FILE_SEPERATOR = "/";
81
82
83
84
85
86
87
88
89 private FTPClient ftpClient;
90
91
92
93
94 private URL url;
95
96
97
98
99 private PasswordAuthentication passwordAuthentication;
100
101
102
103
104 private String homeDirectory;
105
106
107
108
109
110
111
112
113
114
115
116 public FTPRemoteFileSystemView (URL url,
117 PasswordAuthentication passwordAuthentication) {
118
119 this.url = url;
120 if (null != passwordAuthentication) {
121 this.passwordAuthentication = passwordAuthentication;
122 } else {
123 this.passwordAuthentication = anonPassAuth;
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public FTPRemoteFileSystemView (String host, int port,
143 PasswordAuthentication passwordAuthentication)
144 throws FTPBrowseException {
145
146 try {
147 url = new URL("ftp", host, port, null);
148 if (null != passwordAuthentication) {
149 this.passwordAuthentication = passwordAuthentication;
150 } else {
151 this.passwordAuthentication = anonPassAuth;
152 }
153 } catch (MalformedURLException e) {
154 throw new FTPBrowseException(e.getMessage());
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public FTPRemoteFileSystemView (String host,
173 PasswordAuthentication passwordAuthentication)
174 throws FTPBrowseException {
175
176 try {
177 url = new URL("ftp", host, null);
178 if (null != passwordAuthentication) {
179 this.passwordAuthentication = passwordAuthentication;
180 } else {
181 this.passwordAuthentication = anonPassAuth;
182 }
183 } catch (MalformedURLException e) {
184 throw new FTPBrowseException(e.getMessage());
185 }
186 }
187
188
189
190
191
192
193
194
195 public PasswordAuthentication getPasswordAuthentication () {
196
197 return passwordAuthentication;
198 }
199
200
201
202
203 public URL getUrl () {
204
205 return url;
206 }
207
208
209
210
211
212
213
214
215 public void disconnect () {
216
217 if (null != ftpClient && ftpClient.isConnected()) {
218 try {
219 ftpClient.disconnect();
220 } catch (IOException e) {
221 logger.log(Level.WARNING, "IOEx while disconnecting", e);
222 }
223 }
224 }
225
226
227
228
229
230
231
232
233
234
235 private synchronized void createFTPConnection () throws FTPBrowseException {
236
237 ftpClient = new FTPClient();
238 try {
239 InetAddress inetAddress = InetAddress.getByName(url.getHost());
240
241
242
243 if (url.getPort() == -1) {
244 ftpClient.connect(inetAddress);
245 } else {
246 ftpClient.connect(inetAddress, url.getPort());
247 }
248
249 if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
250 throw new FTPBrowseException(ftpClient.getReplyString());
251 }
252 if (null != passwordAuthentication) {
253 ftpClient.login(passwordAuthentication.getUserName(),
254 new StringBuffer().append(
255 passwordAuthentication.getPassword())
256 .toString());
257 }
258 if (url.getPath().length() > 0) {
259 ftpClient.changeWorkingDirectory(url.getPath());
260 }
261 homeDirectory = ftpClient.printWorkingDirectory();
262 } catch (UnknownHostException e) {
263 throw new FTPBrowseException(e.getMessage());
264 } catch (SocketException e) {
265 throw new FTPBrowseException(e.getMessage());
266 } catch (FTPBrowseException e) {
267 throw e;
268 } catch (IOException e) {
269 throw new FTPBrowseException(e.getMessage());
270 }
271 }
272
273
274
275
276
277
278
279
280 private void checkConnection () throws FTPBrowseException {
281
282 if (null == ftpClient || !ftpClient.isConnected()) {
283 createFTPConnection();
284 }
285 }
286
287
288
289
290
291
292
293
294 public File createNewFolder (File containingDir) throws IOException {
295
296
297 throw new FTPBrowseException(
298 "This file system view supports READ ONLY support ONLY!");
299 }
300
301
302
303
304
305
306
307 public File getDefaultDirectory () {
308
309 return getHomeDirectory();
310 }
311
312
313
314
315 public File getHomeDirectory () {
316
317 try {
318 checkConnection();
319
320 if (homeDirectory.equals(FILE_SYSTEM_ROOT_NAME)) {
321 return getRoots()[0];
322 }
323
324
325 FTPFileFile ftpFileFile = null;
326 try {
327 String parent = homeDirectory.substring(0, homeDirectory
328 .lastIndexOf(FILE_SEPERATOR));
329 ftpClient.changeWorkingDirectory(parent);
330 FTPListParseEngine ftpListParseEngine = ftpClient
331 .initiateListParsing();
332 FTPFile [] returnedFiles = ftpListParseEngine.getFiles();
333 String dirName = homeDirectory.substring(homeDirectory
334 .lastIndexOf(FILE_SEPERATOR) + 1);
335 for (int i = 0; i < returnedFiles.length; i++ ) {
336 if (returnedFiles[i].getName().equals(dirName)) {
337 returnedFiles[i].setName(parent + FILE_SEPERATOR
338 + returnedFiles[i].getName());
339 ftpFileFile = new FTPFileFile(returnedFiles[i], this);
340 }
341 }
342 } catch (FTPBrowseException e) {
343 logger.log(Level.WARNING, "Problem browsing file system", e);
344 } catch (IOException e) {
345 logger.log(Level.WARNING, "Problem browsing file system", e);
346 }
347
348 return ftpFileFile;
349 } catch (FTPBrowseException e) {
350 logger.log(Level.WARNING, "FTBEx", e);
351 }
352
353 return null;
354 }
355
356
357
358
359 public File [] getRoots () {
360
361 FTPFileFile [] ftpFiles = null;
362 try {
363 checkConnection();
364
365 FTPFile ftpFile = new FTPFile();
366 ftpFile.setName(FILE_SYSTEM_ROOT_NAME);
367 ftpFile.setType(FTPFile.DIRECTORY_TYPE);
368 FTPFileFile ftpFileFile = new FTPFileFile(ftpFile, this);
369 ftpFiles = new FTPFileFile [1];
370 ftpFiles[0] = ftpFileFile;
371 } catch (FTPBrowseException e) {
372 logger.log(Level.WARNING, "Could not get root file", e);
373 }
374 return ftpFiles;
375 }
376
377
378
379
380
381 public File createFileObject (File dir, String filename) {
382
383
384
385 logger.fine("Calling Super with: " + dir.toString() + " " + filename);
386 return super.createFileObject(dir, filename);
387 }
388
389
390
391
392 public File createFileObject (String path) {
393
394
395
396 logger.fine("Calling Super with: " + path);
397 return super.createFileObject(path);
398 }
399
400
401
402
403
404 public File getChild (File parent, String fileName) {
405
406 if (parent instanceof FTPFileFile) {
407 FTPFile parentDir = ((FTPFileFile) parent).getFtpFile();
408 FTPFileFile returnedFile = null;
409 try {
410 checkConnection();
411 ftpClient.changeWorkingDirectory(parentDir.getName());
412 FTPListParseEngine ftpListParseEngine = ftpClient
413 .initiateListParsing();
414 FTPFile [] returnedFiles = ftpListParseEngine.getFiles();
415
416 if (fileName.indexOf(FILE_SEPERATOR) > -1) {
417 fileName = fileName.substring(fileName
418 .lastIndexOf(FILE_SEPERATOR) + 1);
419 }
420 for (int i = 0; i < returnedFiles.length; i++ ) {
421 if (returnedFiles[i].getName().equals(fileName)) {
422 returnedFiles[i].setName(parentDir.getName()
423 + FILE_SEPERATOR + returnedFiles[i].getName());
424 returnedFile = new FTPFileFile(returnedFiles[i], this);
425 }
426 }
427 } catch (FTPBrowseException e) {
428 logger.log(Level.WARNING, "Problem browsing file system", e);
429 } catch (IOException e) {
430 logger.log(Level.WARNING, "Problem browsing file system", e);
431 }
432 return returnedFile;
433 } else {
434
435 logger.fine("Calling Super with: " + parent.toString() + " " + fileName);
436 return super.getChild(parent, fileName);
437 }
438 }
439
440
441
442
443
444 public synchronized File [] getFiles (File dir, boolean useFileHiding) {
445
446 if (dir instanceof FTPFileFile && dir.isDirectory()) {
447 FTPFile ftpFile = ((FTPFileFile) dir).getFtpFile();
448 String name = ftpFile.getName();
449 try {
450 checkConnection();
451 String pwd = ftpClient.printWorkingDirectory();
452 if (null == pwd || !pwd.equals(name)) {
453 ftpClient.changeWorkingDirectory(name);
454 }
455 pwd = ftpClient.printWorkingDirectory();
456 FTPListParseEngine ftpListParseEngine = ftpClient
457 .initiateListParsing();
458 FTPFile [] files = ftpListParseEngine.getFiles();
459 FTPFileFile [] ftpFiles = new FTPFileFile [files.length];
460 for (int i = 0; i < files.length; i++ ) {
461 files[i].setName(pwd + FILE_SEPERATOR + files[i].getName());
462 ftpFiles[i] = new FTPFileFile(files[i], this);
463 }
464 return ftpFiles;
465 } catch (FTPBrowseException e) {
466 logger.log(Level.WARNING, "Could not connect to host", e);
467 return new FTPFileFile [0];
468 } catch (IOException e) {
469 logger.log(Level.WARNING, "Could not operate on host", e);
470 return new FTPFileFile [0];
471 }
472 }
473
474 logger.fine("Calling Super with: " + dir.toString() + " " + String.valueOf(useFileHiding));
475 return super.getFiles(dir, useFileHiding);
476 }
477
478
479
480
481 public File getParentDirectory (File dir) {
482
483 if (dir instanceof FTPFileFile) {
484
485 FTPFile ftpFile = ((FTPFileFile) dir).getFtpFile();
486 String name = ftpFile.getName();
487 if (name.equals(FILE_SYSTEM_ROOT_NAME)) {
488 return null;
489 }
490
491 String parent = name.substring(0, name.lastIndexOf(FILE_SEPERATOR));
492
493
494 if (parent.equals(FILE_SYSTEM_ROOT_NAME)) {
495 return getRoots()[0];
496 }
497
498
499 String pparent = parent.substring(0, parent
500 .lastIndexOf(FILE_SEPERATOR));
501 if (pparent.length() == 0) {
502 pparent = FILE_SYSTEM_ROOT_NAME;
503 }
504
505 FTPFileFile parentFile = null;
506
507 try {
508 checkConnection();
509 ftpClient.changeWorkingDirectory(pparent);
510 FTPListParseEngine ftpListParseEngine = ftpClient
511 .initiateListParsing();
512 FTPFile [] returnedFiles = ftpListParseEngine.getFiles();
513 String parentName = parent.substring(parent
514 .lastIndexOf(FILE_SEPERATOR) + 1);
515 for (int i = 0; i < returnedFiles.length; i++ ) {
516 if (returnedFiles[i].getName().equals(parentName)) {
517 returnedFiles[i].setName(pparent + FILE_SEPERATOR
518 + returnedFiles[i].getName());
519 parentFile = new FTPFileFile(returnedFiles[i], this);
520 }
521 }
522 } catch (FTPBrowseException e) {
523 logger.log(Level.WARNING, "Problem browsing file system", e);
524 } catch (IOException e) {
525 logger.log(Level.WARNING, "Problem browsing file system", e);
526 }
527
528 if (null == parentFile) {
529 parentFile = (FTPFileFile) getRoots()[0];
530 }
531
532 return parentFile;
533 }
534
535 logger.fine("Calling Super with: " + dir.toString());
536 return super.getParentDirectory(dir);
537 }
538
539
540
541
542 public String getSystemDisplayName (File f) {
543
544 if (f instanceof FTPFileFile) {
545 FTPFile ftpFile = ((FTPFileFile) f).getFtpFile();
546 String name = ftpFile.getName();
547 if (FILE_SYSTEM_ROOT_NAME.equals(name)) {
548 return url.getHost();
549 } else {
550 return f.getName();
551 }
552 } else {
553 logger.fine("Calling Super with: " + f.getPath());
554 return super.getSystemDisplayName(f);
555 }
556 }
557
558
559
560
561
562
563
564 public String getSystemTypeDescription (File f) {
565
566 return null;
567 }
568
569
570
571
572 public boolean isComputerNode (File dir) {
573
574 if (dir instanceof FTPFileFile) {
575 FTPFile ftpFile = ((FTPFileFile) dir).getFtpFile();
576 String name = ftpFile.getName();
577 if (FILE_SYSTEM_ROOT_NAME.equals(name)) {
578 return true;
579 } else {
580 return false;
581 }
582
583 } else {
584 return super.isComputerNode(dir);
585 }
586 }
587
588
589
590
591
592
593 public boolean isDrive (File dir) {
594
595 return false;
596 }
597
598
599
600
601
602
603
604
605 public boolean isFileSystem (File f) {
606
607 if (f instanceof FTPFileFile) {
608 FTPFile ftpFile = ((FTPFileFile) f).getFtpFile();
609 return !ftpFile.isSymbolicLink();
610 }
611 logger.fine("Calling Super for: " + f.toString());
612 return super.isFileSystem(f);
613 }
614
615
616
617
618 public boolean isFileSystemRoot (File dir) {
619
620 if (dir instanceof FTPFileFile) {
621 FTPFile ftpFile = ((FTPFileFile) dir).getFtpFile();
622 String name = ftpFile.getName();
623 if (FILE_SYSTEM_ROOT_NAME.equals(name)) {
624 return true;
625 } else {
626 return false;
627 }
628 }
629 logger.fine("Calling Super for: " + dir.toString());
630 return super.isFileSystemRoot(dir);
631 }
632
633
634
635
636
637
638 public boolean isFloppyDrive (File dir) {
639
640 return false;
641 }
642
643
644
645
646
647
648 public boolean isHiddenFile (File f) {
649
650 return false;
651 }
652
653
654
655
656
657 public boolean isParent (File folder, File file) {
658
659 if (folder instanceof FTPFileFile && file instanceof FTPFileFile) {
660
661 FTPFileFile calculatedParent = (FTPFileFile) getParentDirectory(file);
662 String parentPath = ((FTPFileFile) folder).getFtpFile().getName();
663 if (parentPath.equals(calculatedParent.getFtpFile().getName())) {
664 return true;
665 } else {
666 return false;
667 }
668 }
669 logger.fine("Calling Super for: " + folder.toString() + " " + file.toString());
670 return super.isParent(folder, file);
671 }
672
673
674
675
676 public boolean isRoot (File f) {
677
678 if (f instanceof FTPFileFile) {
679 FTPFile ftpFile = ((FTPFileFile) f).getFtpFile();
680 String name = ftpFile.getName();
681 if (FILE_SYSTEM_ROOT_NAME.equals(name)) {
682 return true;
683 } else {
684 return false;
685 }
686 }
687 logger.fine("Calling super for: " + f.toString());
688 return super.isRoot(f);
689 }
690
691
692
693
694 public Boolean isTraversable (File f) {
695
696 if (f instanceof FTPFileFile) {
697 FTPFile ftpFile = ((FTPFileFile) f).getFtpFile();
698 return new Boolean(ftpFile.isDirectory());
699 }
700 logger.fine("Calling super for: " + f.toString());
701 return super.isTraversable(f);
702 }
703 }