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.Contract;
38 import org.apache.http.annotation.ThreadingBehavior;
39 import org.apache.http.conn.ConnectTimeoutException;
40 import org.apache.http.params.HttpConnectionParams;
41 import org.apache.http.params.HttpParams;
42 import org.apache.http.util.Args;
43
44
45
46
47
48
49
50
51 @Contract(threading = ThreadingBehavior.IMMUTABLE)
52 @Deprecated
53 public class PlainSocketFactory implements SocketFactory, SchemeSocketFactory {
54
55 private final HostNameResolver nameResolver;
56
57
58
59
60
61
62 public static PlainSocketFactory getSocketFactory() {
63 return new PlainSocketFactory();
64 }
65
66
67
68
69 @Deprecated
70 public PlainSocketFactory(final HostNameResolver nameResolver) {
71 super();
72 this.nameResolver = nameResolver;
73 }
74
75 public PlainSocketFactory() {
76 super();
77 this.nameResolver = null;
78 }
79
80
81
82
83
84
85
86
87 @Override
88 public Socket createSocket(final HttpParams params) {
89 return new Socket();
90 }
91
92 @Override
93 public Socket createSocket() {
94 return new Socket();
95 }
96
97
98
99
100 @Override
101 public Socket connectSocket(
102 final Socket socket,
103 final InetSocketAddress remoteAddress,
104 final InetSocketAddress localAddress,
105 final HttpParams params) throws IOException, ConnectTimeoutException {
106 Args.notNull(remoteAddress, "Remote address");
107 Args.notNull(params, "HTTP parameters");
108 Socket sock = socket;
109 if (sock == null) {
110 sock = createSocket();
111 }
112 if (localAddress != null) {
113 sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
114 sock.bind(localAddress);
115 }
116 final int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
117 final int soTimeout = HttpConnectionParams.getSoTimeout(params);
118
119 try {
120 sock.setSoTimeout(soTimeout);
121 sock.connect(remoteAddress, connTimeout);
122 } catch (final SocketTimeoutException ex) {
123 throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
124 }
125 return sock;
126 }
127
128
129
130
131
132
133
134
135
136
137 @Override
138 public final boolean isSecure(final Socket sock) {
139 return false;
140 }
141
142
143
144
145 @Override
146 @Deprecated
147 public Socket connectSocket(
148 final Socket socket,
149 final String host, final int port,
150 final InetAddress localAddress, final int localPort,
151 final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
152 InetSocketAddress local = null;
153 if (localAddress != null || localPort > 0) {
154 local = new InetSocketAddress(localAddress, localPort > 0 ? localPort : 0);
155 }
156 final InetAddress remoteAddress;
157 if (this.nameResolver != null) {
158 remoteAddress = this.nameResolver.resolve(host);
159 } else {
160 remoteAddress = InetAddress.getByName(host);
161 }
162 final InetSocketAddress remote = new InetSocketAddress(remoteAddress, port);
163 return connectSocket(socket, remote, local, params);
164 }
165
166 }