001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.conn.scheme;
029
030import java.io.IOException;
031import java.net.InetAddress;
032import java.net.InetSocketAddress;
033import java.net.Socket;
034import java.net.UnknownHostException;
035
036import org.apache.http.conn.ConnectTimeoutException;
037import org.apache.http.params.BasicHttpParams;
038import org.apache.http.params.HttpParams;
039
040@Deprecated
041class SocketFactoryAdaptor implements SocketFactory {
042
043    private final SchemeSocketFactory factory;
044
045    SocketFactoryAdaptor(final SchemeSocketFactory factory) {
046        super();
047        this.factory = factory;
048    }
049
050    @Override
051    public Socket createSocket() throws IOException {
052        final HttpParams params = new BasicHttpParams();
053        return this.factory.createSocket(params);
054    }
055
056    @Override
057    public Socket connectSocket(
058            final Socket socket,
059            final String host, final int port,
060            final InetAddress localAddress, final int localPort,
061            final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
062        InetSocketAddress local = null;
063        if (localAddress != null || localPort > 0) {
064            local = new InetSocketAddress(localAddress, localPort > 0 ? localPort : 0);
065        }
066        final InetAddress remoteAddress = InetAddress.getByName(host);
067        final InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
068        return this.factory.connectSocket(socket, remote, local, params);
069    }
070
071    @Override
072    public boolean isSecure(final Socket socket) throws IllegalArgumentException {
073        return this.factory.isSecure(socket);
074    }
075
076    public SchemeSocketFactory getFactory() {
077        return this.factory;
078    }
079
080    @Override
081    public boolean equals(final Object obj) {
082        if (obj == null) {
083            return false;
084        }
085        if (this == obj) {
086            return true;
087        }
088        if (obj instanceof SocketFactoryAdaptor) {
089            return this.factory.equals(((SocketFactoryAdaptor)obj).factory);
090        } else {
091            return this.factory.equals(obj);
092        }
093    }
094
095    @Override
096    public int hashCode() {
097        return this.factory.hashCode();
098    }
099
100}