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.HttpRequest;
43 import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
44 import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
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 AbstractAsyncRequesterConsumer<T, E> implements AsyncRequestConsumer<T> {
59
60 private final Supplier<AsyncEntityConsumer<E>> dataConsumerSupplier;
61 private final AtomicReference<AsyncEntityConsumer<E>> dataConsumerRef;
62
63 public AbstractAsyncRequesterConsumer(final Supplier<AsyncEntityConsumer<E>> dataConsumerSupplier) {
64 this.dataConsumerSupplier = Args.notNull(dataConsumerSupplier, "Data consumer supplier");
65 this.dataConsumerRef = new AtomicReference<>(null);
66 }
67
68 public AbstractAsyncRequesterConsumer(final AsyncEntityConsumer<E> dataConsumer) {
69 this(new Supplier<AsyncEntityConsumer<E>>() {
70
71 @Override
72 public AsyncEntityConsumer<E> get() {
73 return dataConsumer;
74 }
75
76 });
77 }
78
79
80
81
82
83
84
85
86 protected abstract T buildResult(HttpRequest request, E entity, ContentType contentType);
87
88 @Override
89 public final void consumeRequest(
90 final HttpRequest request,
91 final EntityDetails entityDetails,
92 final HttpContext httpContext, final FutureCallback<T> resultCallback) throws HttpException, IOException {
93 if (entityDetails != null) {
94 final AsyncEntityConsumer<E> dataConsumer = dataConsumerSupplier.get();
95 if (dataConsumer == null) {
96 throw new HttpException("Supplied data consumer is null");
97 }
98 dataConsumerRef.set(dataConsumer);
99 dataConsumer.streamStart(entityDetails, new CallbackContribution<E>(resultCallback) {
100
101 @Override
102 public void completed(final E entity) {
103 final ContentType contentType;
104 try {
105 contentType = ContentType.parse(entityDetails.getContentType());
106 final T result = buildResult(request, entity, contentType);
107 resultCallback.completed(result);
108 } catch (final UnsupportedCharsetException ex) {
109 resultCallback.failed(ex);
110 }
111 }
112
113 });
114 } else {
115 resultCallback.completed(buildResult(request, null, null));
116 }
117
118 }
119
120 @Override
121 public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
122 final AsyncEntityConsumer<E> dataConsumer = dataConsumerRef.get();
123 dataConsumer.updateCapacity(capacityChannel);
124 }
125
126 @Override
127 public final void consume(final ByteBuffer src) throws IOException {
128 final AsyncEntityConsumer<E> dataConsumer = dataConsumerRef.get();
129 dataConsumer.consume(src);
130 }
131
132 @Override
133 public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
134 final AsyncEntityConsumer<E> dataConsumer = dataConsumerRef.get();
135 dataConsumer.streamEnd(trailers);
136 }
137
138 @Override
139 public final void failed(final Exception cause) {
140 releaseResources();
141 }
142
143 @Override
144 public final void releaseResources() {
145 final AsyncEntityConsumer<E> dataConsumer = dataConsumerRef.getAndSet(null);
146 if (dataConsumer != null) {
147 dataConsumer.releaseResources();
148 }
149 }
150
151 }