This repository has been archived by the owner on May 4, 2023. It is now read-only.
/
HC.java
340 lines (272 loc) · 10.3 KB
/
HC.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.github.rnewson.couchdb.lucene;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.CountDownLatch;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.nio.DefaultClientIOEventDispatch;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.ssl.SSLClientIOEventDispatch;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.NHttpClientHandler;
import org.apache.http.nio.NHttpConnection;
import org.apache.http.nio.entity.ConsumingNHttpEntity;
import org.apache.http.nio.protocol.AsyncNHttpClientHandler;
import org.apache.http.nio.protocol.EventListener;
import org.apache.http.nio.protocol.NHttpRequestExecutionHandler;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOEventDispatch;
import org.apache.http.nio.reactor.SessionRequest;
import org.apache.http.nio.reactor.SessionRequestCallback;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
public class HC {
private static class MyConsumingNHttpEntity implements ConsumingNHttpEntity {
private final ByteBuffer buffer = ByteBuffer.allocate(4096);
public void consumeContent() throws IOException {
}
public void consumeContent(final ContentDecoder decoder,
final IOControl ioctrl) throws IOException {
buffer.clear();
final int bytesRead = decoder.read(buffer);
buffer.flip();
final CharsetDecoder charsetDecoder = Charset.forName("US-ASCII")
.newDecoder();
final CharBuffer charBuffer = charsetDecoder.decode(buffer);
System.out.println(charBuffer);
}
public void finish() throws IOException {
System.err.println("finish");
}
public InputStream getContent() throws IOException,
IllegalStateException {
throw new UnsupportedOperationException("getContent not supported!");
}
public Header getContentEncoding() {
throw new UnsupportedOperationException(
"getContentEncoding not supported!");
}
public long getContentLength() {
throw new UnsupportedOperationException(
"getContentLength not supported!");
}
public Header getContentType() {
throw new UnsupportedOperationException(
"getContentType not supported!");
}
public boolean isChunked() {
throw new UnsupportedOperationException("isChunked not supported!");
}
public boolean isRepeatable() {
throw new UnsupportedOperationException(
"isRepeatable not supported!");
}
public boolean isStreaming() {
throw new UnsupportedOperationException(
"isStreaming not supported!");
}
public void writeTo(final OutputStream outstream) throws IOException {
throw new UnsupportedOperationException("writeTo not supported!");
}
}
static class EventLogger implements EventListener {
public void connectionClosed(final NHttpConnection conn) {
System.out.println("Connection closed: " + conn);
}
public void connectionOpen(final NHttpConnection conn) {
System.out.println("Connection open: " + conn);
}
public void connectionTimeout(final NHttpConnection conn) {
System.out.println("Connection timed out: " + conn);
}
public void fatalIOException(final IOException ex,
final NHttpConnection conn) {
System.err.println("I/O error: " + ex.getMessage());
}
public void fatalProtocolException(final HttpException ex,
final NHttpConnection conn) {
System.err.println("HTTP error: " + ex.getMessage());
}
}
private static class BlindTrust implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
static class MyHttpRequestExecutionHandler implements
NHttpRequestExecutionHandler {
private final static String REQUEST_SENT = "request-sent";
private final static String RESPONSE_RECEIVED = "response-received";
private final CountDownLatch requestCount;
public MyHttpRequestExecutionHandler(final CountDownLatch requestCount) {
super();
this.requestCount = requestCount;
}
public void finalizeContext(final HttpContext context) {
final Object flag = context.getAttribute(RESPONSE_RECEIVED);
if (flag == null) {
// Signal completion of the request execution
requestCount.countDown();
}
}
public void handleResponse(final HttpResponse response,
final HttpContext context) {
final HttpEntity entity = response.getEntity();
try {
final String content = EntityUtils.toString(entity);
System.out.println(response.getStatusLine());
System.out.println(content);
} catch (final IOException ex) {
System.err.println("I/O error: " + ex.getMessage());
}
context.setAttribute(RESPONSE_RECEIVED, Boolean.TRUE);
// Signal completion of the request execution
requestCount.countDown();
}
public void initalizeContext(final HttpContext context,
final Object attachment) {
final HttpHost targetHost = (HttpHost) attachment;
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
}
public ConsumingNHttpEntity responseEntity(final HttpResponse response,
final HttpContext context) throws IOException {
return new MyConsumingNHttpEntity();
}
public HttpRequest submitRequest(final HttpContext context) {
final Object flag = context.getAttribute(REQUEST_SENT);
if (flag == null) {
// Stick some object into the context
context.setAttribute(REQUEST_SENT, Boolean.TRUE);
return new BasicHttpRequest("GET",
"/db1/_changes?feed=continuous&heartbeat=20000");
} else {
// No new request to submit
return null;
}
}
}
static class MySessionRequestCallback implements SessionRequestCallback {
private final CountDownLatch requestCount;
public MySessionRequestCallback(final CountDownLatch requestCount) {
super();
this.requestCount = requestCount;
}
public void cancelled(final SessionRequest request) {
System.out.println("Connect request cancelled: "
+ request.getRemoteAddress());
this.requestCount.countDown();
}
public void completed(final SessionRequest request) {
}
public void failed(final SessionRequest request) {
System.out.println("Connect request failed: "
+ request.getRemoteAddress());
this.requestCount.countDown();
}
public void timeout(final SessionRequest request) {
System.out.println("Connect request timed out: "
+ request.getRemoteAddress());
this.requestCount.countDown();
}
}
public static void main(final String[] args) throws Exception {
final HttpParams params = new SyncBasicHttpParams();
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
8 * 1024);
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,
true);
params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
params.setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(2,
params);
final HttpProcessor httpproc = new ImmutableHttpProcessor(
new HttpRequestInterceptor[] { new RequestContent(),
new RequestTargetHost(), new RequestConnControl(),
new RequestUserAgent(), new RequestExpectContinue() });
// We are going to use this object to synchronize between the
// I/O event and main threads
final CountDownLatch requestCount = new CountDownLatch(1);
final boolean ssl = true;
final SSLContext sslcontext;
if (ssl) {
sslcontext = SSLContext.getInstance("TLS");
sslcontext
.init(null, new TrustManager[] { new BlindTrust() }, null);
} else {
sslcontext = null;
}
final NHttpClientHandler handler = new AsyncNHttpClientHandler(
httpproc, new MyHttpRequestExecutionHandler(requestCount),
new DefaultConnectionReuseStrategy(), params);
final IOEventDispatch ioEventDispatch;
if (ssl) {
ioEventDispatch = new SSLClientIOEventDispatch(handler, sslcontext,
params);
} else {
ioEventDispatch = new DefaultClientIOEventDispatch(handler, params);
}
final Thread t = new Thread(new Runnable() {
public void run() {
try {
ioReactor.execute(ioEventDispatch);
} catch (final InterruptedIOException ex) {
System.err.println("Interrupted");
} catch (final IOException e) {
System.err.println("I/O error: " + e.getMessage());
}
System.out.println("Shutdown");
}
});
t.start();
final int port = ssl ? 6984 : 5984;
ioReactor.connect(new InetSocketAddress("localhost", port), null,
new HttpHost("localhost", port), new MySessionRequestCallback(
requestCount));
// Block until all connections signal
// completion of the request execution
requestCount.await();
System.out.println("Shutting down I/O reactor");
ioReactor.shutdown();
System.out.println("Done");
}
}