1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.sftp;
18
19 import java.io.File;
20 import java.io.Serializable;
21 import java.time.Duration;
22 import java.util.Objects;
23 import java.util.stream.Stream;
24
25 import org.apache.commons.vfs2.FileSystem;
26 import org.apache.commons.vfs2.FileSystemConfigBuilder;
27 import org.apache.commons.vfs2.FileSystemException;
28 import org.apache.commons.vfs2.FileSystemOptions;
29
30 import com.jcraft.jsch.ConfigRepository;
31 import com.jcraft.jsch.UserInfo;
32
33
34
35
36 public final class SftpFileSystemConfigBuilder extends FileSystemConfigBuilder {
37
38
39
40
41 public static final class ProxyType implements Serializable, Comparable<ProxyType> {
42
43
44
45 private static final long serialVersionUID = 20101208L;
46
47 private final String proxyType;
48
49 private ProxyType(final String proxyType) {
50 this.proxyType = proxyType;
51 }
52
53 @Override
54 public int compareTo(final ProxyType pType) {
55 return this.proxyType.compareTo(pType.proxyType);
56 }
57
58 @Override
59 public boolean equals(final Object obj) {
60 if (this == obj) {
61 return true;
62 }
63 if (obj == null || this.getClass() != obj.getClass()) {
64 return false;
65 }
66 return Objects.equals(this.proxyType, ((ProxyType) obj).proxyType);
67 }
68
69
70
71
72
73 @Override
74 public int hashCode() {
75 return this.proxyType.hashCode();
76 }
77 }
78 private static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ZERO;
79
80 private static final Duration DEFAULT_SESSION_TIMEOUT = Duration.ZERO;
81
82 private static final String _PREFIX = SftpFileSystemConfigBuilder.class.getName();
83 private static final SftpFileSystemConfigBuilderleSystemConfigBuilder.html#SftpFileSystemConfigBuilder">SftpFileSystemConfigBuilder BUILDER = new SftpFileSystemConfigBuilder();
84 private static final String COMPRESSION = _PREFIX + "COMPRESSION";
85 private static final String CONNECT_TIMEOUT = _PREFIX + ".CONNECT_TIMEOUT";
86 private static final String ENCODING = _PREFIX + ".ENCODING";
87 private static final String HOST_KEY_CHECK_ASK = "ask";
88 private static final String HOST_KEY_CHECK_NO = "no";
89 private static final String HOST_KEY_CHECK_YES = "yes";
90 private static final String IDENTITIES = _PREFIX + ".IDENTITIES";
91 private static final String IDENTITY_REPOSITORY_FACTORY = _PREFIX + "IDENTITY_REPOSITORY_FACTORY";
92 private static final String CONFIG_REPOSITORY = _PREFIX + "CONFIG_REPOSITORY";
93 private static final String KEY_EXCHANGE_ALGORITHM = _PREFIX + ".KEY_EXCHANGE_ALGORITHM";
94 private static final String LOAD_OPENSSH_CONFIG = _PREFIX + "LOAD_OPENSSH_CONFIG";
95 private static final String KNOWN_HOSTS = _PREFIX + ".KNOWN_HOSTS";
96 private static final String PREFERRED_AUTHENTICATIONS = _PREFIX + ".PREFERRED_AUTHENTICATIONS";
97 private static final String PROXY_COMMAND = _PREFIX + ".PROXY_COMMAND";
98 private static final String PROXY_HOST = _PREFIX + ".PROXY_HOST";
99 private static final String PROXY_OPTIONS = _PREFIX + ".PROXY_OPTIONS";
100 private static final String PROXY_PASSWORD = _PREFIX + ".PROXY_PASSWORD";
101 private static final String PROXY_PORT = _PREFIX + ".PROXY_PORT";
102 private static final String DISABLE_DETECT_EXEC_CHANNEL = _PREFIX + ".DISABLE_DETECT_EXEC_CHANNEL";
103
104
105 public static final ProxyType PROXY_HTTP = new ProxyType("http");
106
107
108 public static final ProxyType PROXY_SOCKS5 = new ProxyType("socks");
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public static final ProxyType PROXY_STREAM = new ProxyType("stream");
124
125 private static final String PROXY_TYPE = _PREFIX + ".PROXY_TYPE";
126 private static final String PROXY_USER = _PREFIX + ".PROXY_USER";
127 private static final String SESSION_TIMEOUT = _PREFIX + ".TIMEOUT";
128 private static final String STRICT_HOST_KEY_CHECKING = _PREFIX + ".STRICT_HOST_KEY_CHECKING";
129 private static final String USER_DIR_IS_ROOT = _PREFIX + ".USER_DIR_IS_ROOT";
130
131
132
133
134
135
136 public static SftpFileSystemConfigBuilder getInstance() {
137 return BUILDER;
138 }
139
140 private SftpFileSystemConfigBuilder() {
141 super("sftp.");
142 }
143
144
145
146
147
148
149 public String getCompression(final FileSystemOptions options) {
150 return this.getString(options, COMPRESSION);
151 }
152
153 @Override
154 protected Class<? extends FileSystem> getConfigClass() {
155 return SftpFileSystem.class;
156 }
157
158
159
160
161
162
163
164 public ConfigRepository getConfigRepository(final FileSystemOptions options) {
165 return getParam(options, CONFIG_REPOSITORY);
166 }
167
168
169
170
171
172
173
174
175
176 public Duration getConnectTimeout(final FileSystemOptions options) {
177 return this.getDuration(options, CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
178 }
179
180
181
182
183
184
185
186
187
188
189 @Deprecated
190 public Integer getConnectTimeoutMillis(final FileSystemOptions options) {
191 return this.getDurationInteger(options, CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
192 }
193
194
195
196
197
198
199
200 public String getFileNameEncoding(final FileSystemOptions options) {
201 return this.getString(options, ENCODING);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 @Deprecated
216 public File[] getIdentities(final FileSystemOptions options) {
217 final IdentityInfo[] info = getIdentityInfo(options);
218 if (info != null) {
219 return Stream.of(info).map(IdentityInfo::getPrivateKey).toArray(File[]::new);
220 }
221 return null;
222 }
223
224
225
226
227
228
229
230
231 public IdentityInfo[] getIdentityInfo(final FileSystemOptions options) {
232 final IdentityProvider[] infos = getIdentityProvider(options);
233 if (infos != null) {
234 return Stream.of(infos).filter(info -> info instanceof IdentityInfo)
235 .map(info -> (IdentityInfo/../../org/apache/commons/vfs2/provider/sftp/IdentityInfo.html#IdentityInfo">IdentityInfo) info).toArray(IdentityInfo[]::new);
236 }
237 return null;
238 }
239
240
241
242
243
244
245
246
247
248 public IdentityProvider[] getIdentityProvider(final FileSystemOptions options) {
249 return getParam(options, IDENTITIES);
250 }
251
252
253
254
255
256
257
258 public IdentityRepositoryFactory getIdentityRepositoryFactory(final FileSystemOptions options) {
259 return getParam(options, IDENTITY_REPOSITORY_FACTORY);
260 }
261
262
263
264
265
266
267
268 public String getKeyExchangeAlgorithm(final FileSystemOptions options) {
269 return this.getString(options, KEY_EXCHANGE_ALGORITHM);
270 }
271
272
273
274
275
276
277
278 public File getKnownHosts(final FileSystemOptions options) {
279 return getParam(options, KNOWN_HOSTS);
280 }
281
282
283
284
285
286
287
288
289 public String getPreferredAuthentications(final FileSystemOptions options) {
290 return getString(options, PREFERRED_AUTHENTICATIONS);
291 }
292
293
294
295
296
297
298
299
300
301
302
303 public String getProxyCommand(final FileSystemOptions options) {
304 return this.getString(options, PROXY_COMMAND, SftpStreamProxy.NETCAT_COMMAND);
305 }
306
307
308
309
310
311
312
313
314
315 public String getProxyHost(final FileSystemOptions options) {
316 return this.getString(options, PROXY_HOST);
317 }
318
319
320
321
322
323
324
325
326
327
328 public FileSystemOptionshtml#FileSystemOptions">FileSystemOptions getProxyOptions(final FileSystemOptions options) {
329 return getParam(options, PROXY_OPTIONS);
330 }
331
332
333
334
335
336
337
338
339
340
341 public String getProxyPassword(final FileSystemOptions options) {
342 return this.getString(options, PROXY_PASSWORD);
343 }
344
345
346
347
348
349
350
351
352
353 public int getProxyPort(final FileSystemOptions options) {
354 return this.getInteger(options, PROXY_PORT, 0);
355 }
356
357
358
359
360
361
362
363 public ProxyType getProxyType(final FileSystemOptions options) {
364 return getParam(options, PROXY_TYPE);
365 }
366
367
368
369
370
371
372
373
374
375 public String getProxyUser(final FileSystemOptions options) {
376 return this.getString(options, PROXY_USER);
377 }
378
379
380
381
382
383
384
385 public Duration getSessionTimeout(final FileSystemOptions options) {
386 return this.getDuration(options, SESSION_TIMEOUT, DEFAULT_SESSION_TIMEOUT);
387 }
388
389
390
391
392
393
394
395
396 @Deprecated
397 public Integer getSessionTimeoutMillis(final FileSystemOptions options) {
398 return this.getDurationInteger(options, SESSION_TIMEOUT, DEFAULT_SESSION_TIMEOUT);
399 }
400
401
402
403
404
405
406 public String getStrictHostKeyChecking(final FileSystemOptions options) {
407 return this.getString(options, STRICT_HOST_KEY_CHECKING, HOST_KEY_CHECK_NO);
408 }
409
410
411
412
413
414
415
416 @Deprecated
417 public Integer getTimeout(final FileSystemOptions options) {
418 return this.getInteger(options, SESSION_TIMEOUT);
419 }
420
421
422
423
424
425
426
427
428
429
430 public Boolean getUserDirIsRoot(final FileSystemOptions options) {
431 return this.getBoolean(options, USER_DIR_IS_ROOT, Boolean.TRUE);
432 }
433
434
435
436
437
438
439 public UserInfo getUserInfo(final FileSystemOptions options) {
440 return getParam(options, UserInfo.class.getName());
441 }
442
443
444
445
446
447
448
449
450
451
452
453
454 public boolean isDisableDetectExecChannel(final FileSystemOptions options) {
455 return this.getBoolean(options, DISABLE_DETECT_EXEC_CHANNEL, Boolean.FALSE);
456 }
457
458
459
460
461
462
463
464
465
466 public boolean isLoadOpenSSHConfig(final FileSystemOptions options) {
467 return this.getBoolean(options, LOAD_OPENSSH_CONFIG, Boolean.FALSE);
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482 public void setCompression(final FileSystemOptions options, final String compression) {
483 this.setParam(options, COMPRESSION, compression);
484 }
485
486
487
488
489
490
491
492
493
494
495
496 public void setConfigRepository(final FileSystemOptions options, final ConfigRepository configRepository) {
497 this.setParam(options, CONFIG_REPOSITORY, configRepository);
498 }
499
500
501
502
503
504
505
506
507 public void setConnectTimeout(final FileSystemOptions options, final Duration timeout) {
508 this.setParam(options, CONNECT_TIMEOUT, timeout);
509 }
510
511
512
513
514
515
516
517
518
519 @Deprecated
520 public void setConnectTimeoutMillis(final FileSystemOptions options, final Integer timeout) {
521 this.setConnectTimeout(options, Duration.ofMillis(timeout));
522 }
523
524
525
526
527
528
529
530
531
532 public void setDisableDetectExecChannel(final FileSystemOptions options, final boolean disableDetectExecChannel) {
533 this.setParam(options, DISABLE_DETECT_EXEC_CHANNEL, toBooleanObject(disableDetectExecChannel));
534 }
535
536
537
538
539
540
541
542 public void setFileNameEncoding(final FileSystemOptions options, final String fileNameEncoding) {
543 this.setParam(options, ENCODING, fileNameEncoding);
544 }
545
546
547
548
549
550
551
552
553
554
555
556 @Deprecated
557 public void setIdentities(final FileSystemOptions options, final File... identityFiles) {
558 IdentityProvider[] info = null;
559 if (identityFiles != null) {
560 info = Stream.of(identityFiles).map(IdentityInfo::new).toArray(IdentityProvider[]::new);
561 }
562 this.setParam(options, IDENTITIES, info);
563 }
564
565
566
567
568
569
570
571
572
573 @Deprecated
574 public void setIdentityInfo(final FileSystemOptions options, final IdentityInfo... identites) {
575 this.setParam(options, IDENTITIES, identites);
576 }
577
578
579
580
581
582
583
584
585 public void setIdentityProvider(final FileSystemOptions options, final IdentityProvider... identites) {
586 this.setParam(options, IDENTITIES, identites);
587 }
588
589
590
591
592
593
594
595
596
597
598
599 public void setIdentityRepositoryFactory(final FileSystemOptions options, final IdentityRepositoryFactory factory) {
600 this.setParam(options, IDENTITY_REPOSITORY_FACTORY, factory);
601 }
602
603
604
605
606
607
608
609
610
611 public void setKeyExchangeAlgorithm(final FileSystemOptions options, final String keyExchangeAlgoritm) {
612 setParam(options, KEY_EXCHANGE_ALGORITHM, keyExchangeAlgoritm);
613 }
614
615
616
617
618
619
620
621
622
623
624 public void setKnownHosts(final FileSystemOptions options, final File knownHosts) {
625 this.setParam(options, KNOWN_HOSTS, knownHosts);
626 }
627
628
629
630
631
632
633
634 public void setLoadOpenSSHConfig(final FileSystemOptions options, final boolean loadOpenSSHConfig) {
635 this.setParam(options, LOAD_OPENSSH_CONFIG, toBooleanObject(loadOpenSSHConfig));
636 }
637
638
639
640
641
642
643
644
645 public void setPreferredAuthentications(final FileSystemOptions options, final String preferredAuthentications) {
646 this.setParam(options, PREFERRED_AUTHENTICATIONS, preferredAuthentications);
647 }
648
649
650
651
652
653
654
655
656
657 public void setProxyCommand(final FileSystemOptions options, final String proxyCommand) {
658 this.setParam(options, PROXY_COMMAND, proxyCommand);
659 }
660
661
662
663
664
665
666
667
668
669
670 public void setProxyHost(final FileSystemOptions options, final String proxyHost) {
671 this.setParam(options, PROXY_HOST, proxyHost);
672 }
673
674
675
676
677
678
679
680
681
682 public void setProxyOptions(final FileSystemOptionsptions.html#FileSystemOptions">FileSystemOptions options, final FileSystemOptions proxyOptions) {
683 this.setParam(options, PROXY_OPTIONS, proxyOptions);
684 }
685
686
687
688
689
690
691
692
693
694 public void setProxyPassword(final FileSystemOptions options, final String proxyPassword) {
695 this.setParam(options, PROXY_PASSWORD, proxyPassword);
696 }
697
698
699
700
701
702
703
704
705
706
707
708 public void setProxyPort(final FileSystemOptions options, final int proxyPort) {
709 this.setParam(options, PROXY_PORT, Integer.valueOf(proxyPort));
710 }
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726 public void setProxyType(final FileSystemOptions options, final ProxyType proxyType) {
727 this.setParam(options, PROXY_TYPE, proxyType);
728 }
729
730
731
732
733
734
735
736
737
738 public void setProxyUser(final FileSystemOptions options, final String proxyUser) {
739 this.setParam(options, PROXY_USER, proxyUser);
740 }
741
742
743
744
745
746
747
748
749 public void setSessionTimeout(final FileSystemOptions options, final Duration timeout) {
750 this.setParam(options, SESSION_TIMEOUT, timeout);
751 }
752
753
754
755
756
757
758
759
760
761 @Deprecated
762 public void setSessionTimeoutMillis(final FileSystemOptions options, final Integer timeout) {
763 this.setSessionTimeout(options, Duration.ofMillis(timeout));
764 }
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779 public void setStrictHostKeyChecking(final FileSystemOptions options, final String hostKeyChecking)
780 throws FileSystemException {
781 if (hostKeyChecking == null || (!hostKeyChecking.equals(HOST_KEY_CHECK_ASK)
782 && !hostKeyChecking.equals(HOST_KEY_CHECK_NO) && !hostKeyChecking.equals(HOST_KEY_CHECK_YES))) {
783 throw new FileSystemException("vfs.provider.sftp/StrictHostKeyChecking-arg.error", hostKeyChecking);
784 }
785
786 this.setParam(options, STRICT_HOST_KEY_CHECKING, hostKeyChecking);
787 }
788
789
790
791
792
793
794
795
796 @Deprecated
797 public void setTimeout(final FileSystemOptions options, final Integer timeout) {
798 this.setParam(options, SESSION_TIMEOUT, timeout);
799 }
800
801
802
803
804
805
806
807 public void setUserDirIsRoot(final FileSystemOptions options, final boolean userDirIsRoot) {
808 this.setParam(options, USER_DIR_IS_ROOT, toBooleanObject(userDirIsRoot));
809 }
810
811
812
813
814
815
816
817 public void setUserInfo(final FileSystemOptions options, final UserInfo info) {
818 this.setParam(options, UserInfo.class.getName(), info);
819 }
820
821 }