View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Create FTPSClient instances.
32   *
33   * @since 2.0
34   */
35  public final class FtpsClientFactory {
36  
37      private FtpsClientFactory() {
38          // empty
39      }
40  
41      /**
42       * Creates a new connection to the server.
43       *
44       * @param hostname The host name.
45       * @param port The port.
46       * @param username The user name for authentication.
47       * @param password The user's password.
48       * @param workingDirectory The directory to use.
49       * @param fileSystemOptions The FileSystemOptions.
50       * @return The FTPSClient.
51       * @throws FileSystemException if an error occurs.
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      /** Connection Factory for FTPS case. */
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                  // '0' means streaming, that's what we do!
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  }