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 package org.apache.hc.core5.http.nio.support;
28
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.util.List;
32 import java.util.concurrent.atomic.AtomicReference;
33
34 import org.apache.hc.core5.concurrent.CallbackContribution;
35 import org.apache.hc.core5.concurrent.FutureCallback;
36 import org.apache.hc.core5.function.Supplier;
37 import org.apache.hc.core5.http.EntityDetails;
38 import org.apache.hc.core5.http.Header;
39 import org.apache.hc.core5.http.HttpException;
40 import org.apache.hc.core5.http.HttpResponse;
41 import org.apache.hc.core5.http.Message;
42 import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
43 import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
44 import org.apache.hc.core5.http.nio.CapacityChannel;
45 import org.apache.hc.core5.http.protocol.HttpContext;
46 import org.apache.hc.core5.util.Args;
47
48
49
50
51
52
53
54
55 public class BasicResponseConsumer<T> implements AsyncResponseConsumer<Message<HttpResponse, T>> {
56
57 private final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier;
58 private final AtomicReference<AsyncEntityConsumer<T>> dataConsumerRef;
59
60 public BasicResponseConsumer(final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier) {
61 this.dataConsumerSupplier = Args.notNull(dataConsumerSupplier, "Data consumer supplier");
62 this.dataConsumerRef = new AtomicReference<>();
63 }
64
65 public BasicResponseConsumer(final AsyncEntityConsumer<T> dataConsumer) {
66 this(() -> dataConsumer);
67 }
68
69 @Override
70 public void consumeResponse(
71 final HttpResponse response,
72 final EntityDetails entityDetails,
73 final HttpContext httpContext, final FutureCallback<Message<HttpResponse, T>> resultCallback) throws HttpException, IOException {
74 Args.notNull(response, "Response");
75
76 if (entityDetails != null) {
77 final AsyncEntityConsumer<T> dataConsumer = dataConsumerSupplier.get();
78 if (dataConsumer == null) {
79 throw new HttpException("Supplied data consumer is null");
80 }
81 dataConsumerRef.set(dataConsumer);
82 dataConsumer.streamStart(entityDetails, new CallbackContribution<T>(resultCallback) {
83
84 @Override
85 public void completed(final T body) {
86 final Message<HttpResponse, T> result = new Message<>(response, body);
87 if (resultCallback != null) {
88 resultCallback.completed(result);
89 }
90 }
91
92 });
93 } else {
94 final Message<HttpResponse, T> result = new Message<>(response, null);
95 if (resultCallback != null) {
96 resultCallback.completed(result);
97 }
98 }
99 }
100
101 @Override
102 public void informationResponse(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
103 }
104
105 @Override
106 public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
107 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
108 dataConsumer.updateCapacity(capacityChannel);
109 }
110
111 @Override
112 public void consume(final ByteBuffer src) throws IOException {
113 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
114 dataConsumer.consume(src);
115 }
116
117 @Override
118 public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
119 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
120 dataConsumer.streamEnd(trailers);
121 }
122
123 @Override
124 public void failed(final Exception cause) {
125 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
126 if (dataConsumer != null) {
127 dataConsumer.failed(cause);
128 }
129 releaseResources();
130 }
131
132 @Override
133 public void releaseResources() {
134 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.getAndSet(null);
135 if (dataConsumer != null) {
136 dataConsumer.releaseResources();
137 }
138 }
139
140 }