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