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  
18  package org.apache.commons.net.ftp;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.Socket;
23  import java.net.UnknownHostException;
24  
25  import javax.net.SocketFactory;
26  import javax.net.ssl.SSLContext;
27  
28  /**
29   *
30   * Socket factory for FTPS connections.
31   *
32   * @since 2.0
33   */
34  public class FTPSSocketFactory extends SocketFactory {
35  
36      private final SSLContext context;
37  
38      public FTPSSocketFactory(final SSLContext context) {
39          this.context = context;
40      }
41  
42      /**
43       * @param port the port
44       * @return the socket
45       * @throws IOException on error
46       * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int) instead}
47       */
48      @Deprecated
49      public java.net.ServerSocket createServerSocket(final int port) throws IOException {
50          return this.init(this.context.getServerSocketFactory().createServerSocket(port));
51      }
52  
53      /**
54       * @param port    the port
55       * @param backlog the backlog
56       * @return the socket
57       * @throws IOException on error
58       * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int) instead}
59       */
60      @Deprecated
61      public java.net.ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
62          return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
63      }
64  
65      /**
66       * @param port      the port
67       * @param backlog   the backlog
68       * @param ifAddress the interface
69       * @return the socket
70       * @throws IOException on error
71       * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int, InetAddress) instead}
72       */
73      @Deprecated
74      public java.net.ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
75          return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
76      }
77  
78      // Override the default implementation
79      @Override
80      public Socket createSocket() throws IOException {
81          return this.context.getSocketFactory().createSocket();
82      }
83  
84      @Override
85      public Socket createSocket(final InetAddress address, final int port) throws IOException {
86          return this.context.getSocketFactory().createSocket(address, port);
87      }
88  
89      // DEPRECATED METHODS - for API compatibility only - DO NOT USE
90  
91      @Override
92      public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
93          return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
94      }
95  
96      @Override
97      public Socket createSocket(final String address, final int port) throws UnknownHostException, IOException {
98          return this.context.getSocketFactory().createSocket(address, port);
99      }
100 
101     @Override
102     public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort)
103             throws UnknownHostException, IOException {
104         return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
105     }
106 
107     /**
108      * @param socket the socket
109      * @return the socket
110      * @throws IOException on error
111      * @deprecated (2.2) use {@link FTPSServerSocketFactory#init(java.net.ServerSocket)}
112      */
113     @Deprecated
114     public java.net.ServerSocket init(final java.net.ServerSocket socket) throws IOException {
115         ((javax.net.ssl.SSLServerSocket) socket).setUseClientMode(true);
116         return socket;
117     }
118 
119 }