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    
019    package org.apache.commons.net.ftp;
020    
021    import java.io.IOException;
022    import java.net.InetAddress;
023    import java.net.ServerSocket;
024    import java.net.Socket;
025    import java.net.UnknownHostException;
026    
027    import javax.net.SocketFactory;
028    import javax.net.ssl.SSLContext;
029    import javax.net.ssl.SSLServerSocket;
030    
031    
032    /**
033     * 
034     * Implementation of org.apache.commons.net.SocketFactory
035     *
036     * @since 2.0
037     */
038    public class FTPSSocketFactory extends SocketFactory {
039    
040        private SSLContext context;
041        
042        public FTPSSocketFactory(SSLContext context) {
043            this.context = context;
044        }
045        
046        @Override
047        public Socket createSocket(String address, int port) throws UnknownHostException, IOException {
048            return this.context.getSocketFactory().createSocket(address, port);
049        }
050    
051        @Override
052        public Socket createSocket(InetAddress address, int port) throws IOException {
053            return this.context.getSocketFactory().createSocket(address, port);
054        }
055    
056        @Override
057        public Socket createSocket(String address, int port, InetAddress localAddress, int localPort) throws UnknownHostException, IOException {
058            return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
059        }
060    
061        @Override
062        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
063            return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
064        }
065        
066        public ServerSocket createServerSocket(int port) throws IOException {
067            return this.init(this.context.getServerSocketFactory().createServerSocket(port));
068        }
069    
070        public ServerSocket createServerSocket(int port, int backlog) throws IOException {
071            return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
072        }
073    
074        public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
075            return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
076        }
077            
078        public ServerSocket init(ServerSocket socket) throws IOException {
079            ((SSLServerSocket) socket).setUseClientMode(true);
080            return socket;
081        }
082    }