FTPSServerSocketFactory.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.ServerSocket;

  21. import javax.net.ServerSocketFactory;
  22. import javax.net.ssl.SSLContext;
  23. import javax.net.ssl.SSLServerSocket;
  24. import javax.net.ssl.SSLServerSocketFactory;

  25. /**
  26.  * Server socket factory for FTPS connections.
  27.  *
  28.  * @since 2.2
  29.  */
  30. public class FTPSServerSocketFactory extends ServerSocketFactory {

  31.     /** Factory for secure socket factories */
  32.     private final SSLContext sslContext;

  33.     /**
  34.      * Constructs a new instance for the given SSL context.
  35.      *
  36.      * @param sslContext The SSL context.
  37.      */
  38.     public FTPSServerSocketFactory(final SSLContext sslContext) {
  39.         this.sslContext = sslContext;
  40.     }

  41.     @SuppressWarnings("resource") // Factory method.
  42.     @Override
  43.     public ServerSocket createServerSocket() throws IOException {
  44.         return init(getServerSocketFactory().createServerSocket());
  45.     }

  46.     @SuppressWarnings("resource") // Factory method.
  47.     @Override
  48.     public ServerSocket createServerSocket(final int port) throws IOException {
  49.         return init(getServerSocketFactory().createServerSocket(port));
  50.     }

  51.     @SuppressWarnings("resource") // Factory method.
  52.     @Override
  53.     public ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
  54.         return init(getServerSocketFactory().createServerSocket(port, backlog));
  55.     }

  56.     @SuppressWarnings("resource") // Factory method.
  57.     @Override
  58.     public ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
  59.         return init(getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
  60.     }

  61.     private SSLServerSocketFactory getServerSocketFactory() {
  62.         return this.sslContext.getServerSocketFactory();
  63.     }

  64.     /**
  65.      * Sets the socket so newly accepted connections will use SSL client mode.
  66.      *
  67.      * @param socket the SSLServerSocket to initialize
  68.      * @return the socket
  69.      * @throws ClassCastException if socket is not an instance of SSLServerSocket
  70.      */
  71.     public ServerSocket init(final ServerSocket socket) {
  72.         ((SSLServerSocket) socket).setUseClientMode(true);
  73.         return socket;
  74.     }
  75. }