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.core5.testing.classic;
29
30 import java.io.IOException;
31 import java.util.concurrent.atomic.AtomicReference;
32
33 import javax.net.ssl.SSLContext;
34
35 import org.apache.hc.core5.http.ClassicHttpRequest;
36 import org.apache.hc.core5.http.ClassicHttpResponse;
37 import org.apache.hc.core5.http.HttpException;
38 import org.apache.hc.core5.http.HttpHost;
39 import org.apache.hc.core5.http.config.CharCodingConfig;
40 import org.apache.hc.core5.http.config.Http1Config;
41 import org.apache.hc.core5.http.impl.DefaultAddressResolver;
42 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
43 import org.apache.hc.core5.http.impl.HttpProcessors;
44 import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
45 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
46 import org.apache.hc.core5.http.io.HttpClientConnection;
47 import org.apache.hc.core5.http.io.SocketConfig;
48 import org.apache.hc.core5.http.protocol.HttpContext;
49 import org.apache.hc.core5.http.protocol.HttpProcessor;
50 import org.apache.hc.core5.io.CloseMode;
51 import org.apache.hc.core5.net.URIAuthority;
52 import org.apache.hc.core5.pool.PoolReusePolicy;
53 import org.apache.hc.core5.pool.StrictConnPool;
54 import org.apache.hc.core5.util.TimeValue;
55
56 public class ClassicTestClient {
57
58 private final SSLContext sslContext;
59 private final SocketConfig socketConfig;
60
61 private final AtomicReference<HttpRequester> requesterRef;
62
63 public ClassicTestClient(final SSLContext sslContext, final SocketConfig socketConfig) {
64 super();
65 this.sslContext = sslContext;
66 this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
67 this.requesterRef = new AtomicReference<>(null);
68 }
69
70 public ClassicTestClient(final SocketConfig socketConfig) {
71 this(null, socketConfig);
72 }
73
74 public ClassicTestClient() {
75 this(null, null);
76 }
77
78 public void start() {
79 start(null);
80 }
81
82 public void start(final HttpProcessor httpProcessor) {
83 if (requesterRef.get() == null) {
84 final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(
85 HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE,
86 DefaultConnectionReuseStrategy.INSTANCE,
87 LoggingHttp1StreamListener.INSTANCE);
88 final StrictConnPool<HttpHost, HttpClientConnection> connPool = new StrictConnPool<>(
89 20,
90 50,
91 TimeValue.NEG_ONE_MILLISECOND,
92 PoolReusePolicy.LIFO,
93 LoggingConnPoolListener.INSTANCE);
94 final HttpRequester requester = new HttpRequester(
95 requestExecutor,
96 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
97 connPool,
98 socketConfig,
99 new LoggingBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT),
100 sslContext != null ? sslContext.getSocketFactory() : null,
101 null,
102 null,
103 DefaultAddressResolver.INSTANCE);
104 requesterRef.compareAndSet(null, requester);
105 } else {
106 throw new IllegalStateException("Requester has already been started");
107 }
108 }
109
110 public void shutdown(final CloseMode closeMode) {
111 final HttpRequester requester = requesterRef.getAndSet(null);
112 if (requester != null) {
113 requester.close(closeMode);
114 }
115 }
116
117 public ClassicHttpResponse execute(
118 final HttpHost targetHost,
119 final ClassicHttpRequest request,
120 final HttpContext context) throws HttpException, IOException {
121 final HttpRequester requester = this.requesterRef.get();
122 if (requester == null) {
123 throw new IllegalStateException("Requester has not been started");
124 }
125 if (request.getAuthority() == null) {
126 request.setAuthority(new URIAuthority(targetHost));
127 }
128 request.setScheme(targetHost.getSchemeName());
129 return requester.execute(targetHost, request, socketConfig.getSoTimeout(), context);
130 }
131
132 }