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    
018    package org.apache.commons.net.ftp;
019    
020    import java.io.IOException;
021    import java.net.InetAddress;
022    import java.net.ServerSocket;
023    
024    import javax.net.ServerSocketFactory;
025    import javax.net.ssl.SSLContext;
026    import javax.net.ssl.SSLServerSocket;
027    
028    /**
029     * Server socket factory for FTPS connections.
030     * @since 2.2
031     */
032    public class FTPSServerSocketFactory extends ServerSocketFactory {
033    
034        /** Factory for secure socket factories */
035        private final SSLContext context;
036    
037        public FTPSServerSocketFactory(SSLContext context) {
038            this.context = context;
039        }
040    
041        // Override the default superclass method
042        @Override
043        public ServerSocket createServerSocket() throws IOException {
044            return init(this.context.getServerSocketFactory().createServerSocket());
045        }
046    
047        @Override
048        public ServerSocket createServerSocket(int port) throws IOException {
049            return init(this.context.getServerSocketFactory().createServerSocket(port));
050        }
051    
052        @Override
053        public ServerSocket createServerSocket(int port, int backlog) throws IOException {
054            return init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
055        }
056    
057        @Override
058        public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
059            return init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
060        }
061    
062        /**
063         * Sets the socket so newly accepted connections will use SSL client mode.
064         *
065         * @param socket the SSLServerSocket to initialise
066         * @return the socket
067         * @throws ClassCastException if socket is not an instance of SSLServerSocket
068         */
069        public ServerSocket init(ServerSocket socket) {
070            ((SSLServerSocket) socket).setUseClientMode(true);
071            return socket;
072        }
073    }
074