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.hc.client5.http.socket;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.net.Proxy;
33 import java.net.Socket;
34 import java.security.AccessController;
35 import java.security.PrivilegedActionException;
36 import java.security.PrivilegedExceptionAction;
37
38 import org.apache.hc.core5.annotation.Contract;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.http.HttpHost;
41 import org.apache.hc.core5.http.protocol.HttpContext;
42 import org.apache.hc.core5.io.Closer;
43 import org.apache.hc.core5.util.Asserts;
44 import org.apache.hc.core5.util.TimeValue;
45
46
47
48
49
50
51
52
53
54
55 @Deprecated
56 @Contract(threading = ThreadingBehavior.STATELESS)
57 public class PlainConnectionSocketFactory implements ConnectionSocketFactory {
58
59 public static final PlainConnectionSocketFactory INSTANCE = new PlainConnectionSocketFactory();
60
61 public static PlainConnectionSocketFactory getSocketFactory() {
62 return INSTANCE;
63 }
64
65 public PlainConnectionSocketFactory() {
66 super();
67 }
68
69 @Override
70 public Socket createSocket(final Proxy proxy, final HttpContext context) throws IOException {
71 return proxy != null ? new Socket(proxy) : createSocket(context);
72 }
73
74 @Override
75 public Socket createSocket(final HttpContext context) throws IOException {
76 return new Socket();
77 }
78
79 @Override
80 public Socket connectSocket(
81 final TimeValue connectTimeout,
82 final Socket socket,
83 final HttpHost host,
84 final InetSocketAddress remoteAddress,
85 final InetSocketAddress localAddress,
86 final HttpContext context) throws IOException {
87 final Socket sock = socket != null ? socket : createSocket(context);
88 if (localAddress != null) {
89 sock.bind(localAddress);
90 }
91 try {
92
93
94 try {
95 AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
96 sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisecondsIntBound() : 0);
97 return null;
98 });
99 } catch (final PrivilegedActionException e) {
100 Asserts.check(e.getCause() instanceof IOException,
101 "method contract violation only checked exceptions are wrapped: " + e.getCause());
102
103 throw (IOException) e.getCause();
104 }
105 } catch (final IOException ex) {
106 Closer.closeQuietly(sock);
107 throw ex;
108 }
109 return sock;
110 }
111
112 }