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 *      https://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    /**
039     * Constructs a new instance.
040     *
041     * @param context The SSL context.
042     */
043    public FTPSSocketFactory(final SSLContext context) {
044        this.context = context;
045    }
046
047    /**
048     * @param port the port
049     * @return the socket
050     * @throws IOException on error
051     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int)}.
052     */
053    @Deprecated
054    public java.net.ServerSocket createServerSocket(final int port) throws IOException {
055        return init(context.getServerSocketFactory().createServerSocket(port));
056    }
057
058    /**
059     * @param port    the port
060     * @param backlog the backlog
061     * @return the socket
062     * @throws IOException on error
063     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int)}.
064     */
065    @Deprecated
066    public java.net.ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
067        return init(context.getServerSocketFactory().createServerSocket(port, backlog));
068    }
069
070    /**
071     * @param port      the port
072     * @param backlog   the backlog
073     * @param ifAddress the interface
074     * @return the socket
075     * @throws IOException on error
076     * @deprecated (2.2) use {@link FTPSServerSocketFactory#createServerSocket(int, int, InetAddress)}.
077     */
078    @Deprecated
079    public java.net.ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
080        return init(context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
081    }
082
083    // Override the default implementation
084    @Override
085    public Socket createSocket() throws IOException {
086        return context.getSocketFactory().createSocket();
087    }
088
089    @Override
090    public Socket createSocket(final InetAddress address, final int port) throws IOException {
091        return context.getSocketFactory().createSocket(address, port);
092    }
093
094    // DEPRECATED METHODS - for API compatibility only - DO NOT USE
095
096    @Override
097    public Socket createSocket(final InetAddress address, final int port, final InetAddress localAddress, final int localPort) throws IOException {
098        return context.getSocketFactory().createSocket(address, port, localAddress, localPort);
099    }
100
101    @Override
102    public Socket createSocket(final String address, final int port) throws UnknownHostException, IOException {
103        return context.getSocketFactory().createSocket(address, port);
104    }
105
106    @Override
107    public Socket createSocket(final String address, final int port, final InetAddress localAddress, final int localPort)
108            throws UnknownHostException, IOException {
109        return context.getSocketFactory().createSocket(address, port, localAddress, localPort);
110    }
111
112    /**
113     * @param socket the socket
114     * @return the socket
115     * @throws IOException on error
116     * @deprecated (2.2) use {@link FTPSServerSocketFactory#init(java.net.ServerSocket)}
117     */
118    @Deprecated
119    public java.net.ServerSocket init(final java.net.ServerSocket socket) throws IOException {
120        ((javax.net.ssl.SSLServerSocket) socket).setUseClientMode(true);
121        return socket;
122    }
123
124}