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  
19  package org.apache.commons.net.ftp;
20  
21  import java.io.IOException;
22  import java.net.InetAddress;
23  import java.net.ServerSocket;
24  import java.net.Socket;
25  import java.net.UnknownHostException;
26  
27  import javax.net.SocketFactory;
28  import javax.net.ssl.SSLContext;
29  import javax.net.ssl.SSLServerSocket;
30  
31  
32  /**
33   * 
34   * Implementation of org.apache.commons.net.SocketFactory
35   *
36   * @since 2.0
37   */
38  public class FTPSSocketFactory extends SocketFactory {
39  
40      private SSLContext context;
41      
42      public FTPSSocketFactory(SSLContext context) {
43          this.context = context;
44      }
45      
46      @Override
47      public Socket createSocket(String address, int port) throws UnknownHostException, IOException {
48          return this.context.getSocketFactory().createSocket(address, port);
49      }
50  
51      @Override
52      public Socket createSocket(InetAddress address, int port) throws IOException {
53          return this.context.getSocketFactory().createSocket(address, port);
54      }
55  
56      @Override
57      public Socket createSocket(String address, int port, InetAddress localAddress, int localPort) throws UnknownHostException, IOException {
58          return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
59      }
60  
61      @Override
62      public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
63          return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
64      }
65      
66      public ServerSocket createServerSocket(int port) throws IOException {
67          return this.init(this.context.getServerSocketFactory().createServerSocket(port));
68      }
69  
70      public ServerSocket createServerSocket(int port, int backlog) throws IOException {
71          return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
72      }
73  
74      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
75          return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
76      }
77          
78      public ServerSocket init(ServerSocket socket) throws IOException {
79          ((SSLServerSocket) socket).setUseClientMode(true);
80          return socket;
81      }
82  }