This repository was archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWaarpFtp4jClient.java
More file actions
638 lines (612 loc) · 21.5 KB
/
Copy pathWaarpFtp4jClient.java
File metadata and controls
638 lines (612 loc) · 21.5 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* This file is part of Waarp Project.
*
* Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
* COPYRIGHT.txt in the distribution for a full listing of individual contributors.
*
* All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Waarp . If not, see
* <http://www.gnu.org/licenses/>.
*/
package org.waarp.ftp.client;
import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPCommunicationListener;
import it.sauronsoftware.ftp4j.FTPConnector;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;
import it.sauronsoftware.ftp4j.FTPReply;
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.waarp.common.logging.WaarpLogger;
import org.waarp.common.logging.WaarpLoggerFactory;
/**
* FTP client using FTP4J model (working in all modes)
*
* @author "Frederic Bregier"
*
*/
public class WaarpFtp4jClient {
/**
* Internal Logger
*/
private static final WaarpLogger logger = WaarpLoggerFactory.getLogger(WaarpFtp4jClient.class);
String server = null;
int port = 21;
String user = null;
String pwd = null;
String acct = null;
int timeout;
int keepalive;
boolean isPassive = false;
int ssl = 0; // -1 native, 1 auth
protected FTPClient ftpClient = null;
protected String result = null;
private boolean binaryTransfer = true;
/**
* @param server
* @param port
* @param user
* @param pwd
* @param acct
* @param isPassive
* @param ssl
* @param timeout
*/
public WaarpFtp4jClient(String server, int port,
String user, String pwd, String acct, boolean isPassive, int ssl, int keepalive,
int timeout) {
this.server = server;
this.port = port;
this.user = user;
this.pwd = pwd;
this.acct = acct;
this.isPassive = isPassive;
this.ssl = ssl;
this.keepalive = keepalive;
this.timeout = timeout;
this.ftpClient = new FTPClient();
if (this.ssl != 0) {
// implicit or explicit
TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManager, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Bad algorithm", e);
} catch (KeyManagementException e) {
throw new IllegalArgumentException("Bad KeyManagement", e);
}
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
this.ftpClient.setSSLSocketFactory(sslSocketFactory);
if (this.ssl < 0) {
this.ftpClient.setSecurity(FTPClient.SECURITY_FTPS);
} else {
this.ftpClient.setSecurity(FTPClient.SECURITY_FTPES);
}
} else {
this.ftpClient = new FTPClient();
}
if (timeout > 0) {
System.setProperty("ftp4j.activeDataTransfer.acceptTimeout", "" + timeout);
}
System.setProperty("ftp4j.activeDataTransfer.hostAddress", "127.0.0.1");
this.ftpClient.addCommunicationListener(new FTPCommunicationListener() {
public void sent(String arg0) {
logger.debug("Command: " + arg0);
}
public void received(String arg0) {
logger.debug("Answer: " + arg0);
}
});
FTPConnector connector = this.ftpClient.getConnector();
connector.setCloseTimeout(timeout);
connector.setReadTimeout(timeout);
connector.setUseSuggestedAddressForDataConnections(true);
}
/**
* @return the result associated with the last command
*/
public String getResult() {
return result;
}
/**
* Try to connect to the server and goes with the authentication
*
* @return True if connected and authenticated, else False
*/
public boolean connect() {
result = null;
boolean isActive = false;
try {
try {
this.ftpClient.connect(this.server, this.port);
} catch (SocketException e) {
result = "Connection in error";
logger.error(result, e);
return false;
} catch (IOException e) {
result = "Connection in error";
logger.error(result, e);
return false;
} catch (IllegalStateException e) {
result = "Connection in error";
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
result = "Connection in error";
logger.error(result, e);
return false;
} catch (FTPException e) {
result = "Connection in error";
logger.error(result, e);
return false;
}
try {
if (this.acct == null) {
// no account
this.ftpClient.login(this.user, this.pwd);
} else {
this.ftpClient.login(this.user, this.pwd, this.acct);
}
} catch (IOException e) {
result = "Login in error";
logger.error(result, e);
return false;
} catch (IllegalStateException e) {
this.logout();
result = "Login in error";
logger.error(result);
return false;
} catch (FTPIllegalReplyException e) {
this.logout();
result = "Login in error";
logger.error(result);
return false;
} catch (FTPException e) {
this.logout();
result = "Login in error";
logger.error(result);
return false;
}
try {
this.ftpClient.setType(FTPClient.TYPE_BINARY);
} catch (IllegalArgumentException e1) {
result = "Set BINARY in error";
logger.error(result, e1);
return false;
}
changeMode(isPassive);
if (keepalive > 0) {
this.ftpClient.setAutoNoopTimeout(keepalive);
}
isActive = true;
return true;
} finally {
if ((!isActive) && !this.ftpClient.isPassive()) {
this.disconnect();
}
}
}
/**
* QUIT the control connection
*/
public void logout() {
this.ftpClient.setAutoNoopTimeout(0);
logger.debug("QUIT");
if (this.executeCommand("QUIT") == null) {
try {
this.ftpClient.logout();
} catch (IOException e) {
// do nothing
} catch (IllegalStateException e) {
// do nothing
} catch (FTPIllegalReplyException e) {
// do nothing
} catch (FTPException e) {
// do nothing
} finally {
if (!this.ftpClient.isPassive()) {
disconnect();
}
}
}
}
/**
* Disconnect the Ftp Client
*/
public void disconnect() {
this.ftpClient.setAutoNoopTimeout(0);
try {
this.ftpClient.disconnect(false);
} catch (IOException e) {
logger.debug("Disconnection in error", e);
} catch (IllegalStateException e) {
logger.debug("Disconnection in error", e);
} catch (FTPIllegalReplyException e) {
logger.debug("Disconnection in error", e);
} catch (FTPException e) {
logger.debug("Disconnection in error", e);
}
}
/**
* Create a new directory
*
* @param newDir
* @return True if created
*/
public boolean makeDir(String newDir) {
result = null;
try {
this.ftpClient.createDirectory(newDir);
return true;
} catch (IOException e) {
result = "MKDIR in error";
logger.info(result, e);
return false;
} catch (IllegalStateException e) {
result = "MKDIR in error";
logger.info(result, e);
return false;
} catch (FTPIllegalReplyException e) {
result = "MKDIR in error";
logger.info(result, e);
return false;
} catch (FTPException e) {
result = "MKDIR in error";
logger.info(result, e);
return false;
}
}
/**
* Change remote directory
*
* @param newDir
* @return True if the change is OK
*/
public boolean changeDir(String newDir) {
result = null;
try {
this.ftpClient.changeDirectory(newDir);
return true;
} catch (IOException e) {
result = "CHDIR in error";
logger.info(result, e);
return false;
} catch (IllegalStateException e) {
result = "CHDIR in error";
logger.info(result, e);
return false;
} catch (FTPIllegalReplyException e) {
result = "CHDIR in error";
logger.info(result, e);
return false;
} catch (FTPException e) {
result = "CHDIR in error";
logger.info(result, e);
return false;
}
}
/**
* Change the FileType of Transfer (Binary true, ASCII false)
*
* @param binaryTransfer1
* @return True if the change is OK
*/
public boolean changeFileType(boolean binaryTransfer1) {
result = null;
this.binaryTransfer = binaryTransfer1;
try {
if (this.binaryTransfer) {
this.ftpClient.setType(FTPClient.TYPE_BINARY);
} else {
this.ftpClient.setType(FTPClient.TYPE_TEXTUAL);
}
return true;
} catch (IllegalArgumentException e) {
result = "FileType in error";
logger.warn(result, e);
return false;
}
}
/**
* Change to passive (true) or active (false) mode
*
* @param passive
*/
public void changeMode(boolean passive) {
this.isPassive = passive;
this.ftpClient.setPassive(passive);
}
/**
* Ask to transfer a file
*
* @param local
* local filepath (full path)
* @param remote
* filename (basename)
* @param getStoreOrAppend
* -1 = get, 1 = store, 2 = append
* @return True if the file is correctly transfered
*/
public boolean transferFile(String local, String remote, int getStoreOrAppend) {
result = null;
try {
if (getStoreOrAppend > 0) {
File from = new File(local);
result = "Cannot finalize store like operation";
logger.debug("Will STOR: " + from);
try {
if (getStoreOrAppend == 1) {
this.ftpClient.upload(from, new DataTimeOutListener(ftpClient, timeout,
"STOR", local));
} else {
// append
this.ftpClient.append(from, new DataTimeOutListener(ftpClient, timeout,
"APPE", local));
}
result = null;
} catch (IllegalStateException e) {
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
logger.error(result, e);
return false;
} catch (FTPException e) {
logger.error(result, e);
return false;
} catch (FTPDataTransferException e) {
logger.error(result, e);
return false;
} catch (FTPAbortedException e) {
logger.error(result, e);
return false;
}
return true;
} else {
result = "Cannot finalize retrieve like operation";
if (local == null) {
// test
NullOutputStream nullOutputStream = new NullOutputStream();
logger.debug("Will DLD nullStream: " + remote);
try {
this.ftpClient.download(remote, nullOutputStream, 0,
new DataTimeOutListener(ftpClient, timeout, "RETR", remote));
result = null;
} catch (IllegalStateException e) {
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
logger.error(result, e);
return false;
} catch (FTPException e) {
logger.error(result, e);
return false;
} catch (FTPDataTransferException e) {
logger.error(result, e);
return false;
} catch (FTPAbortedException e) {
logger.error(result, e);
return false;
}
} else {
logger.debug("Will DLD to local: " + remote + " into " + local);
File to = new File(local);
try {
this.ftpClient.download(remote, to, new DataTimeOutListener(ftpClient,
timeout, "RETR", local));
result = null;
} catch (IllegalStateException e) {
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
logger.error(result, e);
return false;
} catch (FTPException e) {
logger.error(result, e);
return false;
} catch (FTPDataTransferException e) {
logger.error(result, e);
return false;
} catch (FTPAbortedException e) {
logger.error(result, e);
return false;
}
}
return true;
}
} catch (IOException e) {
result = "Cannot finalize operation";
logger.error(result, e);
return false;
}
}
/**
*
* @return the list of Files as given by FTP
*/
public String[] listFiles() {
try {
FTPFile[] list = this.ftpClient.list();
String[] results = new String[list.length];
int i = 0;
for (FTPFile file : list) {
results[i] = file.toString();
i++;
}
return results;
} catch (IOException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (IllegalStateException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (FTPIllegalReplyException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (FTPException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (FTPDataTransferException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (FTPAbortedException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
} catch (FTPListParseException e) {
result = "Cannot finalize transfer operation";
logger.error(result, e);
return null;
}
}
/**
*
* @param feature
* @return True if the given feature is listed
*/
public boolean featureEnabled(String feature) {
try {
FTPReply reply = this.ftpClient.sendCustomCommand("FEAT");
String[] msg = reply.getMessages();
for (String string : msg) {
if (string.contains(feature.toUpperCase())) {
return true;
}
}
return false;
} catch (IOException e) {
result = "Cannot execute operation Feature";
logger.error(result, e);
return false;
} catch (IllegalStateException e) {
result = "Cannot execute operation Feature";
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
result = "Cannot execute operation Feature";
logger.error(result, e);
return false;
}
}
/**
* @param remote
*
* @return True if deleted
*/
public boolean deleteFile(String remote) {
try {
logger.debug("DELE {}", remote);
ftpClient.deleteFile(remote);
return true;
} catch (IOException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return false;
} catch (IllegalStateException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return false;
} catch (FTPIllegalReplyException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return false;
} catch (FTPException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return false;
}
}
/**
*
* @param params
* @return the string lines result for the command params
*/
public String[] executeCommand(String params) {
result = null;
try {
logger.debug(params);
FTPReply reply = this.ftpClient.sendCustomCommand(params);
if (!reply.isSuccessCode()) {
result = reply.toString();
return null;
}
return reply.getMessages();
} catch (IOException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
} catch (IllegalStateException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
} catch (FTPIllegalReplyException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
}
}
/**
*
* @param params
* command without SITE in front
* @return the string lines result for the SITE command params
*/
public String[] executeSiteCommand(String params) {
result = null;
try {
logger.debug("SITE " + params);
FTPReply reply = this.ftpClient.sendSiteCommand(params);
if (!reply.isSuccessCode()) {
result = reply.toString();
return null;
}
return reply.getMessages();
} catch (IOException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
} catch (IllegalStateException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
} catch (FTPIllegalReplyException e) {
result = "Cannot execute operation Site";
logger.error(result, e);
return null;
}
}
}