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