1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.conn.scheme;
29
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.Socket;
34 import java.net.SocketTimeoutException;
35 import java.net.UnknownHostException;
36
37 import org.apache.http.annotation.Immutable;
38
39 import org.apache.http.conn.ConnectTimeoutException;
40 import org.apache.http.conn.DnsResolver;
41 import org.apache.http.params.HttpConnectionParams;
42 import org.apache.http.params.HttpParams;
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @SuppressWarnings("deprecation")
57 @Immutable
58 public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
59
60 private final HostNameResolver nameResolver;
61
62
63
64
65
66
67 public static PlainSocketFactory getSocketFactory() {
68 return new PlainSocketFactory();
69 }
70
71
72
73
74 @Deprecated
75 public PlainSocketFactory(final HostNameResolver nameResolver) {
76 super();
77 this.nameResolver = nameResolver;
78 }
79
80 public PlainSocketFactory() {
81 super();
82 this.nameResolver = null;
83 }
84
85
86
87
88
89
90
91
92 public Socket createSocket(final HttpParams params) {
93 return new Socket();
94 }
95
96 public Socket createSocket() {
97 return new Socket();
98 }
99
100
101
102
103 public Socket connectSocket(
104 final Socket socket,
105 final InetSocketAddress remoteAddress,
106 final InetSocketAddress localAddress,
107 final HttpParams params) throws IOException, ConnectTimeoutException {
108 if (remoteAddress == null) {
109 throw new IllegalArgumentException("Remote address may not be null");
110 }
111 if (params == null) {
112 throw new IllegalArgumentException("HTTP parameters may not be null");
113 }
114 Socket sock = socket;
115 if (sock == null) {
116 sock = createSocket();
117 }
118 if (localAddress != null) {
119 sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
120 sock.bind(localAddress);
121 }
122 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
123 int soTimeout = HttpConnectionParams.getSoTimeout(params);
124
125 try {
126 sock.setSoTimeout(soTimeout);
127 sock.connect(remoteAddress, connTimeout);
128 } catch (SocketTimeoutException ex) {
129 throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
130 }
131 return sock;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public final boolean isSecure(Socket sock)
146 throws IllegalArgumentException {
147
148 if (sock == null) {
149 throw new IllegalArgumentException("Socket may not be null.");
150 }
151
152
153 if (sock.isClosed()) {
154 throw new IllegalArgumentException("Socket is closed.");
155 }
156 return false;
157 }
158
159
160
161
162 @Deprecated
163 public Socket connectSocket(
164 final Socket socket,
165 final String host, int port,
166 final InetAddress localAddress, int localPort,
167 final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
168 InetSocketAddress local = null;
169 if (localAddress != null || localPort > 0) {
170
171 if (localPort < 0) {
172 localPort = 0;
173 }
174 local = new InetSocketAddress(localAddress, localPort);
175 }
176 InetAddress remoteAddress;
177 if (this.nameResolver != null) {
178 remoteAddress = this.nameResolver.resolve(host);
179 } else {
180 remoteAddress = InetAddress.getByName(host);
181 }
182 InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
183 return connectSocket(socket, remote, local, params);
184 }
185
186 }