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
38 private static final class FtpsConnectionFactory
39 extends FtpClientFactory.ConnectionFactory<FTPSClient, FtpsFileSystemConfigBuilder> {
40
41 private FtpsConnectionFactory(final FtpsFileSystemConfigBuilder builder) {
42 super(builder);
43 }
44
45 @Override
46 protected FTPSClient createClient(final FileSystemOptions fileSystemOptions) throws FileSystemException {
47 final FTPSClient client = new FTPSClient(builder.getFtpsMode(fileSystemOptions) == FtpsMode.IMPLICIT);
48
49 final TrustManager trustManager = builder.getTrustManager(fileSystemOptions);
50 if (trustManager != null) {
51 client.setTrustManager(trustManager);
52 }
53
54 final KeyManager keyManager = builder.getKeyManager(fileSystemOptions);
55 if (keyManager != null) {
56 client.setKeyManager(keyManager);
57 }
58 return client;
59 }
60
61 @Override
62 protected void setupOpenConnection(final FTPSClient client, final FileSystemOptions fileSystemOptions)
63 throws IOException {
64 final FtpsDataChannelProtectionLevel level = builder.getDataChannelProtectionLevel(fileSystemOptions);
65 if (level != null) {
66
67 try {
68 client.execPBSZ(0);
69 client.execPROT(level.name());
70 } catch (final SSLException e) {
71 throw new FileSystemException("vfs.provider.ftps/data-channel.level", e, level.toString());
72 }
73 }
74 }
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public static FTPSClient createConnection(final String hostname, final int port, final char[] username,
90 final char[] password, final String workingDirectory, final FileSystemOptions fileSystemOptions)
91 throws FileSystemException {
92 final FtpsConnectionFactory factory = new FtpsConnectionFactory(FtpsFileSystemConfigBuilder.getInstance());
93 return factory.createConnection(hostname, port, username, password, workingDirectory, fileSystemOptions);
94 }
95
96 private FtpsClientFactory() {
97
98 }
99 }