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.Socket;
023import java.net.UnknownHostException;
024
025import javax.net.SocketFactory;
026import javax.net.ssl.SSLContext;
027
028/**
029 *
030 * Socket factory for FTPS connections.
031 *
032 * @since 2.0
033 */
034public class FTPSSocketFactory extends SocketFactory {
035
036    private final SSLContext context;
037
038    public FTPSSocketFactory(final SSLContext context) {
039        this.context = context;
040    }
041
042    /**
043     * @param port the port
044     * @return the socket
045     * @throws IOException on error
046     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int) instead}
047     */
048    @Deprecated
049    public java.net.ServerSocket createServerSocket(final int port) throws IOException {
050        return this.init(this.context.getServerSocketFactory().createServerSocket(port));
051    }
052
053    /**
054     * @param port    the port
055     * @param backlog the backlog
056     * @return the socket
057     * @throws IOException on error
058     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int) instead}
059     */
060    @Deprecated
061    public java.net.ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
062        return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
063    }
064
065    /**
066     * @param port      the port
067     * @param backlog   the backlog
068     * @param ifAddress the interface
069     * @return the socket
070     * @throws IOException on error
071     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int, InetAddress) instead}
072     */
073    @Deprecated
074    public java.net.ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
075        return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
076    }
077
078    // Override the default implementation
079    @Override
080    public Socket createSocket() throws IOException {
081        return this.context.getSocketFactory().createSocket();
082    }
083
084    @Override
085    public Socket createSocket(final InetAddress address, final int port) throws IOException {
086        return this.context.getSocketFactory().createSocket(address, port);
087    }
088
089    // DEPRECATED METHODS - for API compatibility only - DO NOT USE
090
091    @Override
092    public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
093        return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
094    }
095
096    @Override
097    public Socket createSocket(final String address, final int port) throws UnknownHostException, IOException {
098        return this.context.getSocketFactory().createSocket(address, port);
099    }
100
101    @Override
102    public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort)
103            throws UnknownHostException, IOException {
104        return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
105    }
106
107    /**
108     * @param socket the socket
109     * @return the socket
110     * @throws IOException on error
111     * @deprecated (2.2) use {@link FTPSServerSocketFactory#init(java.net.ServerSocket)}
112     */
113    @Deprecated
114    public java.net.ServerSocket init(final java.net.ServerSocket socket) throws IOException {
115        ((javax.net.ssl.SSLServerSocket) socket).setUseClientMode(true);
116        return socket;
117    }
118
119}