/
ApacheHttpAsyncClient4Engine.java
828 lines (743 loc) · 27.5 KB
/
ApacheHttpAsyncClient4Engine.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
package org.jboss.resteasy.client.jaxrs.engines;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.apache.http.ContentTooLongException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.BasicFuture;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.entity.ContentInputStream;
import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.nio.util.HeapByteBufferAllocator;
import org.apache.http.nio.util.SharedInputBuffer;
import org.apache.http.nio.util.SimpleInputBuffer;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.jboss.resteasy.client.jaxrs.i18n.LogMessages;
import org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration;
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
import org.jboss.resteasy.client.jaxrs.internal.ClientResponse;
import org.jboss.resteasy.util.CaseInsensitiveMap;
/**
* AsyncClientHttpEngine using apache http components HttpAsyncClient 4.<p>
*
* Some words of caution: <ul>
* <li>Asynchronous IO means non-blocking IO utilizing few threads, typically at most as much threads as number of cores.
* As such, performance may profit from fewer thread switches and less memory usage due to fewer thread-stacks. But doing
* synchronous, blocking IO (the invoke-methods not returning a future) may suffer, because the data has to be transferred
* piecewiese to/from the io-threads.</li>
* <li>Request-Entities are fully buffered in memory, thus this engine is unsuitable for very large uploads.</li>
* <li>Response-Entities are buffered in memory, except if requesting a Response, InputStream or Reader as Result. Thus
* for large downloads or COMET one of these three return types must be requested, but there may be a performance penalty
* because the response-body is transferred piecewise from the io-threads. When using InvocationCallbacks, the response is
* always fully buffered in memory.</li>
* <li>InvocationCallbacks are called from within the io-threads and thus must not block or else the application may
* slow down to a halt. Reading the response is safe (because the response is buffered in memory), as are other async
* (and in-memory) Client-invocations (the submit-calls returning a future not containing Response, InputStream or Reader).
* Again, there must be no blocking IO inside InvocationCallback! (If you are wondering why not to allow blocking calls by
* wrapping InvocationCallbacks in extra threads: Because then the main advantage of async IO, less threading, is lost.)
* <li>InvocationCallbacks may be called seemingly "after" the future-object returns. Thus, responses should be handled
* solely in the InvocationCallback.</li>
* <li>InvocationCallbacks will see the same result as the future-object and vice versa. Thus, if the invocationcallback
* throws an exception, the future-object will not see it. Another reason to handle responses only in the InvocationCallback.
* </li>
* </ul>
* @author Markus Kull
*/
public class ApacheHttpAsyncClient4Engine implements AsyncClientHttpEngine, Closeable
{
protected final CloseableHttpAsyncClient client;
protected final boolean closeHttpClient;
public ApacheHttpAsyncClient4Engine(final CloseableHttpAsyncClient client, final boolean closeHttpClient)
{
if (client == null) throw new NullPointerException("client");
this.client = client;
this.closeHttpClient = closeHttpClient;
if (closeHttpClient && !client.isRunning()) {
client.start();
}
}
@Override
public void close()
{
if (closeHttpClient)
{
IOUtils.closeQuietly(client);
}
}
@Override
public SSLContext getSslContext()
{
throw new UnsupportedOperationException();
}
@Override
public HostnameVerifier getHostnameVerifier()
{
throw new UnsupportedOperationException();
}
@Override
public Response invoke(Invocation request)
{
// Doing blocking requests with an async httpclient is quite useless.
// But it is better to use the same httpclient in any case just for sharing+configuring only one connectionpool.
Future<ClientResponse> future = submit((ClientInvocation)request, false, null, new ResultExtractor<ClientResponse>() {
@Override
public ClientResponse extractResult(ClientResponse response)
{
return response;
}
});
try
{
return future.get();
}
catch (InterruptedException e)
{
future.cancel(true);
throw clientException(e, null);
}
catch (ExecutionException e)
{
throw clientException(e.getCause(), null);
}
}
@Override
public <T> Future<T> submit(
ClientInvocation request, boolean buffered, InvocationCallback<T> callback, ResultExtractor<T> extractor)
{
HttpUriRequest httpRequest = buildHttpRequest(request);
if (buffered)
{
// Request+Response fully buffered in memory. Optional callback is called inside io-thread after response-body and
// after the returned future is signaled to be completed.
//
// This differs to Resteasy 3.0.8 and earlier (which called the callback before the future completed) due to the
// following reasons:
// * ApacheHttpcomponents BasicFuture, guavas ListenableFuture and also jersey calls the callback after completing
// the future. The earlier Resteasy-behaviour may be more "safe" but any users switching from resteasy to another
// jax-rs implementation may encounter a nasty surprise.
// * ensure the result returned by the future is the same given to the callback.
// * As good practice, the result should only be handled in one place (future OR callback, not both)
// * Invocation-javadoc says "the underlying response instance will be automatically closed" seemingly implying
// the future-response is unusable (bc. closed) together with a callback
// Of course the one big drawback is that exceptions inside the callback are not visible to the application,
// but callbacks are mostly treated as fire-and-forget, meaning their result is not checked anyway.
HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(httpRequest);
HttpAsyncResponseConsumer<T> responseConsumer = new BufferingResponseConsumer<T>(request, extractor);
FutureCallback<T> httpCallback = callback != null ? new CallbackAdapter<T>(callback) : null;
return client.execute(requestProducer, responseConsumer, httpCallback);
}
else
{
// unbuffered: Future returns immediately after headers. Reading the response-stream blocks, but one may check
// InputStream#available() to prevent blocking.
// would be easy to call an InvocationCallback after response-BODY, but cant see any usecase for it.
if (callback != null) throw new IllegalArgumentException("unbuffered InvocationCallback is not supported");
HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(httpRequest);
StreamingResponseConsumer<T> responseConsumer = new StreamingResponseConsumer<T>(request, extractor);
Future<T> httpFuture = client.execute(requestProducer, responseConsumer, null);
return responseConsumer.future(httpFuture);
}
}
/**
* ResponseConsumer which transfers the response piecewise from the io-thread to the blocking handler-thread.
* {@link #future(Future)} returns a Future which completes immediately after receiving the response-headers
* but reading the response-inputstream blocks until data is available.
*/
private static class StreamingResponseConsumer<T> implements HttpAsyncResponseConsumer<T>
{
private static final IOException unallowedBlockingReadException = new IOException("blocking reads inside an async io-handler are not allowed") {
public synchronized Throwable fillInStackTrace() {
//do nothing and return
return this;
}
};
private ClientConfiguration configuration;
private Map<String, Object> properties;
private ResultExtractor<T> extractor;
private ResultFuture<T> future;
private SharedInputStream sharedStream;
private volatile boolean hasResult;
private volatile T result;
private volatile Exception exception;
private volatile boolean completed;
StreamingResponseConsumer(final ClientInvocation request, final ResultExtractor<T> extractor)
{
this.configuration = request.getClientConfiguration();
this.properties = request.getMutableProperties();
this.extractor = extractor;
}
private void releaseResources()
{
this.configuration = null;
this.properties = null;
this.extractor = null;
this.future = null;
this.sharedStream = null;
}
public synchronized Future<T> future(Future<T> httpFuture)
{
if (completed)
{ // already failed or fully buffered
return httpFuture;
}
future = new ResultFuture<T>(httpFuture);
future.copyHttpFutureResult();
if (!future.isDone() && hasResult)
{ // response(-headers) is available, but not yet the full response-stream. Return immediately the result
future.completed(getResult());
}
return future;
}
@Override
public synchronized void responseReceived(HttpResponse httpResponse) throws IOException, HttpException
{
SharedInputStream sharedStream = null;
ConnectionResponse clientResponse = null;
T result = null;
Exception exception = null;
boolean success = false;
try {
clientResponse = new ConnectionResponse(configuration, properties);
copyResponse(httpResponse, clientResponse);
final HttpEntity entity = httpResponse.getEntity();
if (entity != null)
{
sharedStream = new SharedInputStream(new SharedInputBuffer(16 * 1024));
// one could also set the stream after extracting the response, but this would prevent wrapping the stream
clientResponse.setConnection(sharedStream);
sharedStream.setException(unallowedBlockingReadException);
result = extractor.extractResult(clientResponse);
sharedStream.setException(null);
}
else
{
result = extractor.extractResult(clientResponse);
}
success = true;
}
catch(Exception e)
{
exception = clientException(e, clientResponse);
}
finally
{
if (success)
{
this.sharedStream = sharedStream;
this.result = result;
this.hasResult = true;
if (future != null) future.completed(result);
}
else
{
this.exception = exception;
completed = true;
if (future != null) future.failed(exception);
releaseResources();
}
}
}
@Override
public synchronized void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException
{
if (sharedStream != null) sharedStream.consumeContent(decoder, ioctrl);
}
@Override
public synchronized void responseCompleted(HttpContext context)
{
this.completed = true;
try
{
if (sharedStream != null)
{ // only needed in case of empty response body (=null ioctrl)
sharedStream.consumeContent(EndOfStream.INSTANCE, null);
}
}
catch (IOException ioe)
{ // cannot happen
throw new RuntimeException(ioe);
}
finally
{
releaseResources();
}
}
@Override
public Exception getException()
{
return exception;
}
@Override
public T getResult()
{
return result;
}
@Override
public boolean isDone()
{ // cancels in case of closing the SharedInputStream
return completed;
}
@Override
public synchronized void close()
{
completed = true;
ResultFuture<T> future = this.future;
if (future != null)
{
// if connect fails, then the httpclient just calls close() after setting its future, but never our failed().
// so copy the httpFuture-result into our ResultFuture.
future.copyHttpFutureResult();
if (!future.isDone())
{ // doesnt happen?
future.failed(clientException(new IOException("connect failed"), null));
}
}
releaseResources();
}
@Override
public synchronized void failed(Exception ex)
{
completed = true;
if (future != null) future.failed(clientException(ex, null));
if (sharedStream != null)
{
sharedStream.setException(ioException(ex));
IOUtils.closeQuietly(sharedStream);
}
releaseResources();
}
@Override
public synchronized boolean cancel()
{
completed = true;
if (future != null) future.cancelledResult();
if (sharedStream != null)
{
sharedStream.setException(new IOException("cancelled"));
IOUtils.closeQuietly(sharedStream);
}
releaseResources();
return true;
}
private static class ResultFuture<T> extends BasicFuture<T>
{
private final Future<T> httpFuture;
ResultFuture(final Future<T> httpFuture)
{
super(null);
this.httpFuture = httpFuture;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning)
{
boolean cancelled = super.cancel(mayInterruptIfRunning);
httpFuture.cancel(mayInterruptIfRunning);
return cancelled;
}
public void cancelledResult() {
super.cancel(true);
}
public void copyHttpFutureResult()
{
if (!isDone() && httpFuture.isDone())
{
try
{
completed(httpFuture.get());
}
catch(ExecutionException e)
{
failed(clientException(e.getCause(), null));
}
catch (InterruptedException e)
{ // cant happen because already isDone
failed(e);
}
}
}
}
private class SharedInputStream extends ContentInputStream {
private final SharedInputBuffer sharedBuf;
private volatile IOException ex;
private volatile IOControl ioctrl;
SharedInputStream(final SharedInputBuffer sharedBuf)
{
super(sharedBuf);
this.sharedBuf = sharedBuf;
}
public void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException {
if (ioctrl != null) this.ioctrl = ioctrl;
sharedBuf.consumeContent(decoder, ioctrl);
}
@Override
public void close() throws IOException
{
completed = true; // next isDone() cancels.
// Workaround for deadlock: super.close() reads until no more data, but on cancellation no more data is
// pushed to consumeContent, thus deadlock. Instead notify the reactor by ioctrl.requestInput
sharedBuf.close(); // next reads will return EndOfStream. Also wakes up any waiting readers
IOControl ioctrl = this.ioctrl;
if (ioctrl != null) ioctrl.requestInput(); // notify reactor to check isDone()
super.close(); // does basically nothing due to closed buf
}
@Override
public int read(final byte[] b, final int off, final int len) throws IOException
{
throwIfError();
return super.read(b, off, len);
}
@Override
public int read(final byte[] b) throws IOException
{
throwIfError();
return super.read(b, 0, b.length);
}
@Override
public int read() throws IOException {
throwIfError();
return super.read();
}
private void throwIfError() throws IOException {
IOException ex = this.ex;
if (ex != null) {
//create a new exception here to make it easy figuring out where the offending blocking IO comes from
throw new IOException(ex);
}
}
public void setException(IOException e) {
this.ex = e;
}
}
}
/**
* Buffers response fully in memory.
*
* (Buffering is definitely easier to implement than streaming)
*/
private static class BufferingResponseConsumer<T> extends AbstractAsyncResponseConsumer<T>
{
private ClientConfiguration configuration;
private Map<String, Object> properties;
private ResultExtractor<T> responseExtractor;
private ConnectionResponse clientResponse;
private SimpleInputBuffer buf;
BufferingResponseConsumer(final ClientInvocation request, final ResultExtractor<T> responseExtractor)
{
this.configuration = request.getClientConfiguration();
this.properties = request.getMutableProperties();
this.responseExtractor = responseExtractor;
}
@Override
protected void onResponseReceived(HttpResponse response) throws HttpException, IOException
{
ConnectionResponse clientResponse = new ConnectionResponse(configuration, properties);
copyResponse(response, clientResponse);
final HttpEntity entity = response.getEntity();
if (entity != null)
{
long len = entity.getContentLength();
if (len > Integer.MAX_VALUE)
{
throw new ContentTooLongException("Entity content is too long: " + len);
}
if (len < 0)
{
len = 4096;
}
this.buf = new SimpleInputBuffer((int) len, new HeapByteBufferAllocator());
}
this.clientResponse = clientResponse;
}
@Override
protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException
{
}
@Override
protected void onContentReceived(ContentDecoder decoder, IOControl ioctrl) throws IOException
{
SimpleInputBuffer buf = this.buf;
if (buf == null) throw new NullPointerException("Content Buffer");
buf.consumeContent(decoder);
}
@Override
protected T buildResult(HttpContext context) throws Exception
{
if (buf != null) clientResponse.setConnection(new ContentInputStream(buf));
return responseExtractor.extractResult(clientResponse);
}
@Override
protected void releaseResources()
{
this.configuration = null;
this.properties = null;
this.responseExtractor = null;
this.clientResponse = null;
this.buf = null;
}
}
/**
* Adapter from http-FutureCallback<T> to InvocationCallback<T>
*/
private static class CallbackAdapter<T> implements FutureCallback<T>
{
private final InvocationCallback<T> invocationCallback;
CallbackAdapter(final InvocationCallback<T> invocationCallback)
{
this.invocationCallback = invocationCallback;
}
@Override
public void cancelled()
{
invocationCallback.failed(new ProcessingException("cancelled"));
}
@Override
public void completed(T response)
{
try
{
invocationCallback.completed(response);
}
catch (Throwable t)
{
LogMessages.LOGGER.exceptionIgnored(t);
}
finally
{
// just to promote proper callback usage, because HttpAsyncClient is responsible
// for cleaning up the (buffered) connection
if (response instanceof Response)
{
((Response) response).close();
}
}
}
@Override
public void failed(Exception ex)
{
invocationCallback.failed(clientException(ex, null));
}
}
/**
* ClientResponse with surefire releaseConnection
*/
private static class ConnectionResponse extends ClientResponse
{
private InputStream connection;
private InputStream stream;
ConnectionResponse(final ClientConfiguration configuration, final Map<String, Object> properties)
{
super(configuration);
setProperties(properties);
}
public synchronized void setConnection(InputStream connection)
{
this.connection = connection;
this.stream = connection;
}
@Override
protected synchronized void setInputStream(InputStream is)
{
stream = is;
resetEntity();
}
@Override
public synchronized InputStream getInputStream()
{
return stream;
}
@Override
public synchronized void releaseConnection() throws IOException
{
releaseConnection(false);
}
@Override
public synchronized void releaseConnection(boolean consumeInputStream) throws IOException
{
boolean thrown = true;
try
{
if (stream != null)
{
if (consumeInputStream)
{
while (stream.read() > 0)
{
}
}
stream.close();
}
thrown = false;
}
finally
{
if (connection != null)
{
if (thrown)
{
IOUtils.closeQuietly(connection);
}
else
{
connection.close();
}
}
}
}
}
private static class EndOfStream implements ContentDecoder
{
public static EndOfStream INSTANCE = new EndOfStream();
@Override
public int read(ByteBuffer dst) throws IOException
{
return -1;
}
@Override
public boolean isCompleted()
{
return true;
}
}
private static HttpUriRequest buildHttpRequest(ClientInvocation request)
{
// Writers may change headers. Thus buffer the content before committing the headers.
// For simplicity's sake the content is buffered in memory. File-buffering (ZeroCopyConsumer...) would be
// possible, but resource management is error-prone.
HttpRequestBase httpRequest = createHttpMethod(request.getUri(), request.getMethod());
if (request.getEntity() != null)
{
byte[] requestContent = requestContent(request);
ByteArrayEntity entity = new ByteArrayEntity(requestContent);
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, request.getHeaders().getMediaType().toString()));
commitHeaders(request, httpRequest);
((HttpEntityEnclosingRequest) httpRequest).setEntity(entity);
}
else
{
commitHeaders(request, httpRequest);
}
return httpRequest;
}
private static byte[] requestContent(ClientInvocation request)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
request.getDelegatingOutputStream().setDelegate(baos);
try
{
request.writeRequestBody(request.getEntityStream());
baos.close();
return baos.toByteArray();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
private static HttpRequestBase createHttpMethod(URI url, String restVerb)
{
if ("GET".equals(restVerb))
{
return new HttpGet(url);
}
else if ("POST".equals(restVerb))
{
return new HttpPost(url);
}
else
{
final String verb = restVerb;
return new HttpPost(url)
{
@Override
public String getMethod()
{
return verb;
}
};
}
}
private static void commitHeaders(ClientInvocation request, HttpRequestBase httpMethod)
{
MultivaluedMap<String, String> headers = request.getHeaders().asMap();
for (Map.Entry<String, List<String>> header : headers.entrySet())
{
List<String> values = header.getValue();
for (String value : values)
{
httpMethod.addHeader(header.getKey(), value);
}
}
}
private static void copyResponse(HttpResponse httpResponse, ClientResponse clientResponse)
{
clientResponse.setStatus(httpResponse.getStatusLine().getStatusCode());
CaseInsensitiveMap<String> headers = new CaseInsensitiveMap<String>();
for (Header header : httpResponse.getAllHeaders())
{
headers.add(header.getName(), header.getValue());
}
clientResponse.setHeaders(headers);
}
private static RuntimeException clientException(Throwable ex, Response clientResponse) {
RuntimeException ret;
if (ex == null)
{
ret = new ProcessingException(new NullPointerException());
}
else if (ex instanceof WebApplicationException)
{
ret = (WebApplicationException) ex;
}
else if (ex instanceof ProcessingException)
{
ret = (ProcessingException) ex;
}
else if (clientResponse != null)
{
ret = new ResponseProcessingException(clientResponse, ex);
}
else
{
ret = new ProcessingException(ex);
}
return ret;
}
private static IOException ioException(Exception ex) {
return (ex instanceof IOException) ? (IOException) ex : new IOException(ex);
}
}