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