1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ftp.server;
12
13 import org.mule.util.IOUtils;
14
15 import java.io.File;
16 import java.io.IOException;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.commons.net.ftp.FTPClient;
21 import org.apache.commons.net.ftp.FTPFile;
22
23
24
25
26 public class FTPTestClient
27 {
28 protected final transient Log logger = LogFactory.getLog(this.getClass());
29 FTPClient ftpClient = null;
30 String server = null;
31 int port;
32 String user = null;
33 String password = null;
34 public static final int TIMEOUT = 5000;
35
36 public FTPTestClient(String server, int port, String user, String password)
37 {
38 super();
39 this.server = server;
40 this.port = port;
41 this.user = user;
42 this.password = password;
43 ftpClient = new FTPClient();
44 }
45
46 public boolean testConnection() throws IOException
47 {
48 connect();
49 return verifyStatusCode(ftpClient.noop());
50 }
51
52
53
54
55
56
57 public String[] getFileList(String path) throws IOException
58 {
59 connect();
60 return ftpClient.listNames(path);
61 }
62
63
64
65
66
67
68
69 public boolean makeDir(String dir) throws IOException
70 {
71 connect();
72 return verifyStatusCode(ftpClient.mkd(dir));
73 }
74
75
76
77
78
79
80
81 public boolean deleteDir(String dir) throws IOException
82 {
83 connect();
84 return verifyStatusCode(ftpClient.rmd(dir));
85 }
86
87
88
89
90
91
92 private boolean verifyStatusCode(int status)
93 {
94 if(status >= 200 && status < 300)
95 {
96 return true;
97 }
98 return false;
99 }
100
101
102
103
104
105
106
107 public boolean putFile(String fileName, String targetDir) throws IOException
108 {
109 connect();
110 File file = new File(IOUtils.getResourceAsUrl(fileName, getClass()).getFile());
111 return ftpClient.storeFile(targetDir + "/" + file.getName(), IOUtils.getResourceAsStream(fileName, getClass()));
112 }
113
114
115
116
117
118
119
120 public boolean dirExists(String path) throws IOException
121 {
122 connect();
123 String cwd = ftpClient.printWorkingDirectory();
124 boolean dirExists = ftpClient.changeWorkingDirectory(path);
125 ftpClient.changeWorkingDirectory(cwd);
126 return dirExists;
127 }
128
129
130
131
132
133
134 public void recursiveDelete(String path) throws IOException
135 {
136 connect();
137 String cwd = ftpClient.printWorkingDirectory();
138 System.out.println("CWD: " + cwd);
139 ftpClient.changeWorkingDirectory(path);
140 System.out.println("Changed CWD: " + path);
141
142 FTPFile[] fileObjs = ftpClient.listFiles();
143 for(int i = 0; i < fileObjs.length; i++)
144 {
145 if(fileObjs[i].isFile())
146 {
147 ftpClient.deleteFile(fileObjs[i].getName());
148 }
149 else if(fileObjs[i].isDirectory() && (getFileList(ftpClient.printWorkingDirectory() + "/" + fileObjs[i].getName()).length > 0))
150 {
151 recursiveDelete(ftpClient.printWorkingDirectory() + "/" + fileObjs[i].getName());
152 deleteDir(ftpClient.printWorkingDirectory() + "/" + fileObjs[i].getName());
153 }
154 else if(fileObjs[i].isDirectory())
155 {
156 deleteDir(ftpClient.printWorkingDirectory() + "/" + fileObjs[i].getName());
157 }
158
159 }
160 ftpClient.changeWorkingDirectory(cwd);
161 }
162
163
164
165
166
167 protected void connect() throws IOException
168 {
169 if(!ftpClient.isConnected())
170 {
171 ftpClient = new FTPClient();
172 ftpClient.setDefaultTimeout(TIMEOUT);
173 ftpClient.connect(server, port);
174 ftpClient.login(user, password);
175 }
176 }
177
178
179
180
181
182 public boolean isConnected()
183 {
184 return ftpClient.isConnected();
185 }
186
187
188
189
190
191 public void disconnect() throws IOException
192 {
193 ftpClient.disconnect();
194 }
195
196
197
198
199
200
201
202 public boolean fileExists(String file) throws IOException
203 {
204 return (ftpClient.listFiles(file).length > 0);
205 }
206
207
208
209
210
211
212
213 public boolean deleteFile(String name) throws IOException
214 {
215 return ftpClient.deleteFile(name);
216 }
217
218
219
220
221
222
223
224 public boolean expectFileCount(String directory, int count, long timeout) throws InterruptedException, IOException
225 {
226 long endTime = System.currentTimeMillis() + timeout;
227 int iteration = 1;
228 while(System.currentTimeMillis() < endTime)
229 {
230 logger.debug("checking file list, iteration :" + iteration);
231 if (getFileList(directory).length == count)
232 {
233 logger.debug("found expected file count : " + count);
234 return true;
235 }
236 Thread.sleep(1000);
237 ++iteration;
238 }
239 return false;
240 }
241 }