    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.ftp;
    019
    020import java.io.IOException;
    021import java.net.InetAddress;
    022import java.net.ServerSocket;
    023
    024import javax.net.ServerSocketFactory;
    025import javax.net.ssl.SSLContext;
    026import javax.net.ssl.SSLServerSocket;
    027import javax.net.ssl.SSLServerSocketFactory;
    028
    029/**
    030 * Server socket factory for FTPS connections.
    031 *
    032 * @since 2.2
    033 */
    034public class FTPSServerSocketFactory extends ServerSocketFactory {
    035
    036    /** Factory for secure socket factories */
    037    private final SSLContext sslContext;
    038
    039    /**
    040     * Constructs a new instance for the given SSL context.
    041     *
    042     * @param sslContext The SSL context.
    043     */
    044    public FTPSServerSocketFactory(final SSLContext sslContext) {
    045        this.sslContext = sslContext;
    046    }
    047
    048    @SuppressWarnings("resource") // Factory method.
    049    @Override
    050    public ServerSocket createServerSocket() throws IOException {
    051        return init(getServerSocketFactory().createServerSocket());
    052    }
    053
    054    @SuppressWarnings("resource") // Factory method.
    055    @Override
    056    public ServerSocket createServerSocket(final int port) throws IOException {
    057        return init(getServerSocketFactory().createServerSocket(port));
    058    }
    059
    060    @SuppressWarnings("resource") // Factory method.
    061    @Override
    062    public ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
    063        return init(getServerSocketFactory().createServerSocket(port, backlog));
    064    }
    065
    066    @SuppressWarnings("resource") // Factory method.
    067    @Override
    068    public ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress)
    069        throws IOException {
    070        return init(getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
    071    }
    072
    073    private SSLServerSocketFactory getServerSocketFactory() {
    074        return this.sslContext.getServerSocketFactory();
    075    }
    076
    077    /**
    078     * Sets the socket so newly accepted connections will use SSL client mode.
    079     *
    080     * @param socket the SSLServerSocket to initialize
    081     * @return the socket
    082     * @throws ClassCastException if socket is not an instance of SSLServerSocket
    083     */
    084    public ServerSocket init(final ServerSocket socket) {
    085        ((SSLServerSocket) socket).setUseClientMode(true);
    086        return socket;
    087    }
    088}