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.HttpRequest;
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.AsyncRequestConsumer;
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 public class BasicRequestConsumer<T> implements AsyncRequestConsumer<Message<HttpRequest, T>> {
55
56 private final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier;
57 private final AtomicReference<AsyncEntityConsumer<T>> dataConsumerRef;
58
59 public BasicRequestConsumer(final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier) {
60 this.dataConsumerSupplier = Args.notNull(dataConsumerSupplier, "Data consumer supplier");
61 this.dataConsumerRef = new AtomicReference<>();
62 }
63
64 public BasicRequestConsumer(final AsyncEntityConsumer<T> dataConsumer) {
65 this(new Supplier<AsyncEntityConsumer<T>>() {
66
67 @Override
68 public AsyncEntityConsumer<T> get() {
69 return dataConsumer;
70 }
71
72 });
73 }
74
75 @Override
76 public void consumeRequest(
77 final HttpRequest request,
78 final EntityDetails entityDetails,
79 final HttpContext httpContext,
80 final FutureCallback<Message<HttpRequest, T>> resultCallback) throws HttpException, IOException {
81 Args.notNull(request, "Request");
82 if (entityDetails != null) {
83 final AsyncEntityConsumer<T> dataConsumer = dataConsumerSupplier.get();
84 if (dataConsumer == null) {
85 throw new HttpException("Supplied data consumer is null");
86 }
87 dataConsumerRef.set(dataConsumer);
88
89 dataConsumer.streamStart(entityDetails, new CallbackContribution<T>(resultCallback) {
90
91 @Override
92 public void completed(final T body) {
93 final Message<HttpRequest, T> result = new Message<>(request, body);
94 if (resultCallback != null) {
95 resultCallback.completed(result);
96 }
97 }
98
99 });
100 } else {
101 final Message<HttpRequest, T> result = new Message<>(request, null);
102 if (resultCallback != null) {
103 resultCallback.completed(result);
104 }
105 }
106 }
107
108 @Override
109 public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
110 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
111 dataConsumer.updateCapacity(capacityChannel);
112 }
113
114 @Override
115 public void consume(final ByteBuffer src) throws IOException {
116 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
117 dataConsumer.consume(src);
118 }
119
120 @Override
121 public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
122 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
123 dataConsumer.streamEnd(trailers);
124 }
125
126 @Override
127 public void failed(final Exception cause) {
128 releaseResources();
129 }
130
131 @Override
132 public void releaseResources() {
133 final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.getAndSet(null);
134 if (dataConsumer != null) {
135 dataConsumer.releaseResources();
136 }
137 }
138
139 }