|
8 | 8 | import java.io.File; |
9 | 9 | import java.io.IOException; |
10 | 10 | import java.util.ArrayList; |
| 11 | +import java.util.Dictionary; |
| 12 | +import java.util.Enumeration; |
| 13 | +import java.util.Hashtable; |
11 | 14 | import java.util.List; |
12 | 15 |
|
13 | 16 | import it.sauronsoftware.ftp4j.FTPAbortedException; |
|
24 | 27 | * @author Paul-DS |
25 | 28 | */ |
26 | 29 | public class FtpRepository { |
| 30 | + /** |
| 31 | + * File containing open FTP connections. |
| 32 | + */ |
| 33 | + private Hashtable<Integer, FTPClient> openConnections = new Hashtable<Integer, FTPClient>(); |
27 | 34 |
|
28 | 35 | /** |
29 | 36 | * Lists the files present at the specified path. |
30 | 37 | * @param path The specified path. |
31 | 38 | * @return The list of files. |
32 | 39 | */ |
33 | 40 | public List<FileEntity> listFiles(FtpServer server, String path) throws FTPException, IOException, FTPIllegalReplyException, FTPAbortedException, FTPDataTransferException, FTPListParseException { |
34 | | - FTPClient client = new FTPClient(); |
35 | | - client.connect(server.getHost(), server.getPort()); |
36 | | - client.login(server.isAnonymous() ? "anonymous" : server.getLogin(), server.getPassword()); |
| 41 | + FTPClient client = this.getConnection(server); |
37 | 42 |
|
38 | 43 | if(path != null && !path.equals("/")) { |
39 | 44 | client.changeDirectory(path); |
@@ -68,6 +73,49 @@ public void createFolder(String path, String name) { |
68 | 73 | } |
69 | 74 | } |
70 | 75 |
|
| 76 | + /** |
| 77 | + * Delete a list of files and directories. |
| 78 | + * @param server The FTP server. |
| 79 | + * @param elements The specified list. |
| 80 | + */ |
| 81 | + public void deleteFiles(FtpServer server, List<FileEntity> elements) throws FTPException, IOException, FTPIllegalReplyException { |
| 82 | + FTPClient client = this.getConnection(server); |
| 83 | + |
| 84 | + for (FileEntity element: elements) { |
| 85 | + if(element.isDirectory()) { |
| 86 | + client.deleteDirectory(element.getPath()); |
| 87 | + } |
| 88 | + else { |
| 89 | + client.deleteFile(element.getPath()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets a FTP connection for a given server. |
| 96 | + * @param server The server to connect. |
| 97 | + * @return The FTP connection. |
| 98 | + */ |
| 99 | + private FTPClient getConnection(FtpServer server) throws FTPException, IOException, FTPIllegalReplyException { |
| 100 | + FTPClient client = openConnections.get(server.getId()); |
| 101 | + |
| 102 | + if(client == null) |
| 103 | + { |
| 104 | + client = new FTPClient(); |
| 105 | + openConnections.put(server.getId(), client); |
| 106 | + } |
| 107 | + |
| 108 | + if(!client.isConnected()) { |
| 109 | + client.connect(server.getHost(), server.getPort()); |
| 110 | + } |
| 111 | + |
| 112 | + if(!client.isAuthenticated()) { |
| 113 | + client.login(server.isAnonymous() ? "anonymous" : server.getLogin(), server.getPassword()); |
| 114 | + } |
| 115 | + |
| 116 | + return client; |
| 117 | + } |
| 118 | + |
71 | 119 | /** |
72 | 120 | * Convert a FTP file to a file entity. |
73 | 121 | * @param file The FTP file to convert. |
|
0 commit comments