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