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;
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.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39
40 import org.apache.http.annotation.Contract;
41 import org.apache.http.annotation.ThreadingBehavior;
42 import org.apache.http.conn.scheme.SocketFactory;
43 import org.apache.http.params.HttpConnectionParams;
44 import org.apache.http.params.HttpParams;
45 import org.apache.http.util.Args;
46 import org.apache.http.util.Asserts;
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @Deprecated
61 @Contract(threading = ThreadingBehavior.IMMUTABLE)
62 public final class MultihomePlainSocketFactory implements SocketFactory {
63
64
65
66
67 private static final
68 MultihomePlainSocketFactoryihomePlainSocketFactory">MultihomePlainSocketFactory DEFAULT_FACTORY = new MultihomePlainSocketFactory();
69
70
71
72
73
74 public static MultihomePlainSocketFactory getSocketFactory() {
75 return DEFAULT_FACTORY;
76 }
77
78
79
80
81 private MultihomePlainSocketFactory() {
82 super();
83 }
84
85
86
87 @Override
88 public Socket createSocket() {
89 return new Socket();
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 @Override
108 public Socket connectSocket(final Socket socket, final String host, final int port,
109 final InetAddress localAddress, final int localPort,
110 final HttpParams params)
111 throws IOException {
112 Args.notNull(host, "Target host");
113 Args.notNull(params, "HTTP parameters");
114
115 Socket sock = socket;
116 if (sock == null) {
117 sock = createSocket();
118 }
119
120 if ((localAddress != null) || (localPort > 0)) {
121 final InetSocketAddress isa = new InetSocketAddress(localAddress,
122 localPort > 0 ? localPort : 0);
123 sock.bind(isa);
124 }
125
126 final int timeout = HttpConnectionParams.getConnectionTimeout(params);
127
128 final InetAddress[] inetadrs = InetAddress.getAllByName(host);
129 final List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length);
130 addresses.addAll(Arrays.asList(inetadrs));
131 Collections.shuffle(addresses);
132
133 IOException lastEx = null;
134 for (final InetAddress remoteAddress: addresses) {
135 try {
136 sock.connect(new InetSocketAddress(remoteAddress, port), timeout);
137 break;
138 } catch (final SocketTimeoutException ex) {
139 throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
140 } catch (final IOException ex) {
141
142 sock = new Socket();
143
144 lastEx = ex;
145 }
146 }
147 if (lastEx != null) {
148 throw lastEx;
149 }
150 return sock;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 @Override
166 public boolean isSecure(final Socket sock)
167 throws IllegalArgumentException {
168
169 Args.notNull(sock, "Socket");
170
171
172 Asserts.check(!sock.isClosed(), "Socket is closed");
173 return false;
174
175 }
176
177 }