• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 package org.apache.http.conn.scheme;
33 
34 import java.io.IOException;
35 import java.net.InetAddress;
36 import java.net.InetSocketAddress;
37 import java.net.Socket;
38 import java.net.SocketTimeoutException;
39 
40 import org.apache.http.conn.ConnectTimeoutException;
41 import org.apache.http.params.HttpConnectionParams;
42 import 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 public final class PlainSocketFactory implements SocketFactory {
51 
52     /**
53      * The factory singleton.
54      */
55     private static final
56         PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory();
57 
58     private final HostNameResolver nameResolver;
59 
60     /**
61      * Gets the singleton instance of this class.
62      * @return the one and only plain socket factory
63      */
getSocketFactory()64     public static PlainSocketFactory getSocketFactory() {
65         return DEFAULT_FACTORY;
66     }
67 
PlainSocketFactory(final HostNameResolver nameResolver)68     public PlainSocketFactory(final HostNameResolver nameResolver) {
69         super();
70         this.nameResolver = nameResolver;
71     }
72 
73 
PlainSocketFactory()74     public PlainSocketFactory() {
75         this(null);
76     }
77 
78     // non-javadoc, see interface org.apache.http.conn.SocketFactory
createSocket()79     public Socket createSocket() {
80         return new Socket();
81     }
82 
83     // non-javadoc, see interface org.apache.http.conn.SocketFactory
connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params)84     public Socket connectSocket(Socket sock, String host, int port,
85                                 InetAddress localAddress, int localPort,
86                                 HttpParams params)
87         throws IOException {
88 
89         if (host == null) {
90             throw new IllegalArgumentException("Target host may not be null.");
91         }
92         if (params == null) {
93             throw new IllegalArgumentException("Parameters may not be null.");
94         }
95 
96         if (sock == null)
97             sock = createSocket();
98 
99         if ((localAddress != null) || (localPort > 0)) {
100 
101             // we need to bind explicitly
102             if (localPort < 0)
103                 localPort = 0; // indicates "any"
104 
105             InetSocketAddress isa =
106                 new InetSocketAddress(localAddress, localPort);
107             sock.bind(isa);
108         }
109 
110         int timeout = HttpConnectionParams.getConnectionTimeout(params);
111 
112         InetSocketAddress remoteAddress;
113         if (this.nameResolver != null) {
114             remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
115         } else {
116             remoteAddress = new InetSocketAddress(host, port);
117         }
118         try {
119             sock.connect(remoteAddress, timeout);
120         } catch (SocketTimeoutException ex) {
121             throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
122         }
123         return sock;
124 
125     } // connectSocket
126 
127 
128     /**
129      * Checks whether a socket connection is secure.
130      * This factory creates plain socket connections
131      * which are not considered secure.
132      *
133      * @param sock      the connected socket
134      *
135      * @return  <code>false</code>
136      *
137      * @throws IllegalArgumentException if the argument is invalid
138      */
isSecure(Socket sock)139     public final boolean isSecure(Socket sock)
140         throws IllegalArgumentException {
141 
142         if (sock == null) {
143             throw new IllegalArgumentException("Socket may not be null.");
144         }
145         // This class check assumes that createSocket() calls the constructor
146         // directly. If it was using javax.net.SocketFactory, we couldn't make
147         // an assumption about the socket class here.
148         if (sock.getClass() != Socket.class) {
149             throw new IllegalArgumentException
150                 ("Socket not created by this factory.");
151         }
152         // This check is performed last since it calls a method implemented
153         // by the argument object. getClass() is final in java.lang.Object.
154         if (sock.isClosed()) {
155             throw new IllegalArgumentException("Socket is closed.");
156         }
157 
158         return false;
159 
160     } // isSecure
161 
162 
163     /**
164      * Compares this factory with an object.
165      * There is only one instance of this class.
166      *
167      * @param obj       the object to compare with
168      *
169      * @return  iff the argument is this object
170      */
171     @Override
equals(Object obj)172     public boolean equals(Object obj) {
173         return (obj == this);
174     }
175 
176     /**
177      * Obtains a hash code for this object.
178      * All instances of this class have the same hash code.
179      * There is only one instance of this class.
180      */
181     @Override
hashCode()182     public int hashCode() {
183         return PlainSocketFactory.class.hashCode();
184     }
185 
186 }
187