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      /** Connection Factory for FTPS case. */
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                  // '0' means streaming, that's what we do!
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       * Creates a new connection to the server.
79       *
80       * @param hostname The host name.
81       * @param port The port.
82       * @param username The user name for authentication.
83       * @param password The user's password.
84       * @param workingDirectory The directory to use.
85       * @param fileSystemOptions The FileSystemOptions.
86       * @return The FTPSClient.
87       * @throws FileSystemException if an error occurs while establishing a connection.
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          // empty
98      }
99  }