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 * Implementation of org.apache.commons.net.SocketFactory
031 *
032 * @since 2.0
033 */
034public class FTPSSocketFactory extends SocketFactory {
035
036    private final SSLContext context;
037
038    public FTPSSocketFactory(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(String address, int port) throws UnknownHostException, IOException {
050        return this.context.getSocketFactory().createSocket(address, port);
051    }
052
053    @Override
054    public Socket createSocket(InetAddress address, int port) throws IOException {
055        return this.context.getSocketFactory().createSocket(address, port);
056    }
057
058    @Override
059    public Socket createSocket(String address, int port, InetAddress localAddress, int localPort)
060            throws UnknownHostException, IOException {
061        return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
062    }
063
064    @Override
065    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
066        return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
067    }
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(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(int port, 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(int port, int backlog, InetAddress ifAddress) throws IOException {
099        return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
100    }
101
102    /** @param socket the socket
103     * @return the socket
104     * @throws IOException  on error
105     * @deprecated  (2.2) use {@link FTPSServerSocketFactory#init(java.net.ServerSocket)} */
106    @Deprecated
107    public java.net.ServerSocket init(java.net.ServerSocket socket) throws IOException {
108        ((javax.net.ssl.SSLServerSocket) socket).setUseClientMode(true);
109        return socket;
110    }
111
112}