1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.ftps;
18
19 import java.io.IOException;
20
21 import javax.net.ssl.KeyManager;
22 import javax.net.ssl.SSLException;
23 import javax.net.ssl.TrustManager;
24
25 import org.apache.commons.net.ftp.FTPSClient;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.provider.ftp.FtpClientFactory;
29
30
31
32
33
34
35 public final class FtpsClientFactory {
36
37 private FtpsClientFactory() {
38
39 }
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static FTPSClient createConnection(final String hostname, final int port, final char[] username,
54 final char[] password, final String workingDirectory, final FileSystemOptions fileSystemOptions)
55 throws FileSystemException {
56 final FtpsConnectionFactory factory = new FtpsConnectionFactory(FtpsFileSystemConfigBuilder.getInstance());
57 return factory.createConnection(hostname, port, username, password, workingDirectory, fileSystemOptions);
58 }
59
60
61 private static final class FtpsConnectionFactory
62 extends FtpClientFactory.ConnectionFactory<FTPSClient, FtpsFileSystemConfigBuilder> {
63
64 private FtpsConnectionFactory(final FtpsFileSystemConfigBuilder builder) {
65 super(builder);
66 }
67
68 @Override
69 protected FTPSClient createClient(final FileSystemOptions fileSystemOptions) throws FileSystemException {
70 final FTPSClient client = new FTPSClient(builder.getFtpsMode(fileSystemOptions) == FtpsMode.IMPLICIT);
71
72 final TrustManager trustManager = builder.getTrustManager(fileSystemOptions);
73 if (trustManager != null) {
74 client.setTrustManager(trustManager);
75 }
76
77 final KeyManager keyManager = builder.getKeyManager(fileSystemOptions);
78 if (keyManager != null) {
79 client.setKeyManager(keyManager);
80 }
81 return client;
82 }
83
84 @Override
85 protected void setupOpenConnection(final FTPSClient client, final FileSystemOptions fileSystemOptions)
86 throws IOException {
87 final FtpsDataChannelProtectionLevel level = builder.getDataChannelProtectionLevel(fileSystemOptions);
88 if (level != null) {
89
90 try {
91 client.execPBSZ(0);
92 client.execPROT(level.name());
93 } catch (final SSLException e) {
94 throw new FileSystemException("vfs.provider.ftps/data-channel.level", e, level.toString());
95 }
96 }
97 }
98 }
99 }