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