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
042        public ServerSocket createServerSocket(int port) throws IOException {
043            return init(this.context.getServerSocketFactory().createServerSocket(port));
044        }
045    
046        @Override
047        public ServerSocket createServerSocket(int port, int backlog) throws IOException {
048            return init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
049        }
050    
051        @Override
052        public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
053            return init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
054        }
055    
056        /**
057         * Sets the socket so newly accepted connections will use SSL client mode.
058         * 
059         * @param socket the SSLServerSocket to initialise
060         * @return the socket
061         * @throws ClassCastException if socket is not an instance of SSLServerSocket
062         */
063        public ServerSocket init(ServerSocket socket) {
064            ((SSLServerSocket) socket).setUseClientMode(true);
065            return socket;
066        }
067    }
068