1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java $
3 * $Revision: 659194 $
4 * $Date: 2008-05-22 11:33:47 -0700 (Thu, 22 May 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.conn.scheme;
33
34import java.io.IOException;
35import java.net.InetAddress;
36import java.net.InetSocketAddress;
37import java.net.Socket;
38import java.net.SocketTimeoutException;
39
40import org.apache.http.conn.ConnectTimeoutException;
41import org.apache.http.params.HttpConnectionParams;
42import org.apache.http.params.HttpParams;
43
44/**
45 * The default class for creating sockets.
46 *
47 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
48 * @author Michael Becke
49 *
50 * @deprecated Please use {@link java.net.URL#openConnection} instead.
51 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
52 *     for further details.
53 */
54@Deprecated
55public final class PlainSocketFactory implements SocketFactory {
56
57    /**
58     * The factory singleton.
59     */
60    private static final
61        PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory();
62
63    private final HostNameResolver nameResolver;
64
65    /**
66     * Gets the singleton instance of this class.
67     * @return the one and only plain socket factory
68     */
69    public static PlainSocketFactory getSocketFactory() {
70        return DEFAULT_FACTORY;
71    }
72
73    public PlainSocketFactory(final HostNameResolver nameResolver) {
74        super();
75        this.nameResolver = nameResolver;
76    }
77
78
79    public PlainSocketFactory() {
80        this(null);
81    }
82
83    // non-javadoc, see interface org.apache.http.conn.SocketFactory
84    public Socket createSocket() {
85        return new Socket();
86    }
87
88    // non-javadoc, see interface org.apache.http.conn.SocketFactory
89    public Socket connectSocket(Socket sock, String host, int port,
90                                InetAddress localAddress, int localPort,
91                                HttpParams params)
92        throws IOException {
93
94        if (host == null) {
95            throw new IllegalArgumentException("Target host may not be null.");
96        }
97        if (params == null) {
98            throw new IllegalArgumentException("Parameters may not be null.");
99        }
100
101        if (sock == null)
102            sock = createSocket();
103
104        if ((localAddress != null) || (localPort > 0)) {
105
106            // we need to bind explicitly
107            if (localPort < 0)
108                localPort = 0; // indicates "any"
109
110            InetSocketAddress isa =
111                new InetSocketAddress(localAddress, localPort);
112            sock.bind(isa);
113        }
114
115        int timeout = HttpConnectionParams.getConnectionTimeout(params);
116
117        InetSocketAddress remoteAddress;
118        if (this.nameResolver != null) {
119            remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
120        } else {
121            remoteAddress = new InetSocketAddress(host, port);
122        }
123        try {
124            sock.connect(remoteAddress, timeout);
125        } catch (SocketTimeoutException ex) {
126            throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
127        }
128        return sock;
129
130    } // connectSocket
131
132
133    /**
134     * Checks whether a socket connection is secure.
135     * This factory creates plain socket connections
136     * which are not considered secure.
137     *
138     * @param sock      the connected socket
139     *
140     * @return  <code>false</code>
141     *
142     * @throws IllegalArgumentException if the argument is invalid
143     */
144    public final boolean isSecure(Socket sock)
145        throws IllegalArgumentException {
146
147        if (sock == null) {
148            throw new IllegalArgumentException("Socket may not be null.");
149        }
150        // This class check assumes that createSocket() calls the constructor
151        // directly. If it was using javax.net.SocketFactory, we couldn't make
152        // an assumption about the socket class here.
153        if (sock.getClass() != Socket.class) {
154            throw new IllegalArgumentException
155                ("Socket not created by this factory.");
156        }
157        // This check is performed last since it calls a method implemented
158        // by the argument object. getClass() is final in java.lang.Object.
159        if (sock.isClosed()) {
160            throw new IllegalArgumentException("Socket is closed.");
161        }
162
163        return false;
164
165    } // isSecure
166
167
168    /**
169     * Compares this factory with an object.
170     * There is only one instance of this class.
171     *
172     * @param obj       the object to compare with
173     *
174     * @return  iff the argument is this object
175     */
176    @Override
177    public boolean equals(Object obj) {
178        return (obj == this);
179    }
180
181    /**
182     * Obtains a hash code for this object.
183     * All instances of this class have the same hash code.
184     * There is only one instance of this class.
185     */
186    @Override
187    public int hashCode() {
188        return PlainSocketFactory.class.hashCode();
189    }
190
191}
192