FTPSSocketFactory.java

  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.net.ftp;

  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.net.Socket;
  21. import java.net.UnknownHostException;

  22. import javax.net.SocketFactory;
  23. import javax.net.ssl.SSLContext;

  24. /**
  25.  *
  26.  * Socket factory for FTPS connections.
  27.  *
  28.  * @since 2.0
  29.  */
  30. public class FTPSSocketFactory extends SocketFactory {

  31.     private final SSLContext context;

  32.     public FTPSSocketFactory(final SSLContext context) {
  33.         this.context = context;
  34.     }

  35.     /**
  36.      * @param port the port
  37.      * @return the socket
  38.      * @throws IOException on error
  39.      * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int) instead}
  40.      */
  41.     @Deprecated
  42.     public java.net.ServerSocket createServerSocket(final int port) throws IOException {
  43.         return init(this.context.getServerSocketFactory().createServerSocket(port));
  44.     }

  45.     /**
  46.      * @param port    the port
  47.      * @param backlog the backlog
  48.      * @return the socket
  49.      * @throws IOException on error
  50.      * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int) instead}
  51.      */
  52.     @Deprecated
  53.     public java.net.ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
  54.         return init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
  55.     }

  56.     /**
  57.      * @param port      the port
  58.      * @param backlog   the backlog
  59.      * @param ifAddress the interface
  60.      * @return the socket
  61.      * @throws IOException on error
  62.      * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int, InetAddress) instead}
  63.      */
  64.     @Deprecated
  65.     public java.net.ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
  66.         return init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
  67.     }

  68.     // Override the default implementation
  69.     @Override
  70.     public Socket createSocket() throws IOException {
  71.         return this.context.getSocketFactory().createSocket();
  72.     }

  73.     @Override
  74.     public Socket createSocket(final InetAddress address, final int port) throws IOException {
  75.         return this.context.getSocketFactory().createSocket(address, port);
  76.     }

  77.     // DEPRECATED METHODS - for API compatibility only - DO NOT USE

  78.     @Override
  79.     public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
  80.         return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
  81.     }

  82.     @Override
  83.     public Socket createSocket(final String address, final int port) throws UnknownHostException, IOException {
  84.         return this.context.getSocketFactory().createSocket(address, port);
  85.     }

  86.     @Override
  87.     public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort)
  88.             throws UnknownHostException, IOException {
  89.         return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
  90.     }

  91.     /**
  92.      * @param socket the socket
  93.      * @return the socket
  94.      * @throws IOException on error
  95.      * @deprecated (2.2) use {@link FTPSServerSocketFactory#init(java.net.ServerSocket)}
  96.      */
  97.     @Deprecated
  98.     public java.net.ServerSocket init(final java.net.ServerSocket socket) throws IOException {
  99.         ((javax.net.ssl.SSLServerSocket) socket).setUseClientMode(true);
  100.         return socket;
  101.     }

  102. }