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.client5.http.impl.async;
28
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.util.List;
32
33 import org.apache.hc.core5.http.EntityDetails;
34 import org.apache.hc.core5.http.Header;
35 import org.apache.hc.core5.http.HttpException;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.message.RequestLine;
39 import org.apache.hc.core5.http.message.StatusLine;
40 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
41 import org.apache.hc.core5.http.nio.CapacityChannel;
42 import org.apache.hc.core5.http.nio.DataStreamChannel;
43 import org.apache.hc.core5.http.nio.RequestChannel;
44 import org.apache.hc.core5.http.protocol.HttpContext;
45 import org.apache.hc.core5.util.Identifiable;
46 import org.slf4j.Logger;
47
48 final class LoggingAsyncClientExchangeHandler implements AsyncClientExchangeHandler, Identifiable {
49
50 private final Logger log;
51 private final String exchangeId;
52 private final AsyncClientExchangeHandler handler;
53
54 LoggingAsyncClientExchangeHandler(final Logger log, final String exchangeId, final AsyncClientExchangeHandler handler) {
55 this.log = log;
56 this.exchangeId = exchangeId;
57 this.handler = handler;
58 }
59
60 @Override
61 public String getId() {
62 return exchangeId;
63 }
64
65 @Override
66 public void releaseResources() {
67 handler.releaseResources();
68 }
69
70 @Override
71 public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
72 handler.produceRequest(new RequestChannel() {
73
74 @Override
75 public void sendRequest(
76 final HttpRequest request,
77 final EntityDetails entityDetails,
78 final HttpContext context) throws HttpException, IOException {
79 if (log.isDebugEnabled()) {
80 log.debug("{}: send request {}, {}", exchangeId, new RequestLine(request), entityDetails != null ? "entity len " + entityDetails.getContentLength() : "null entity");
81 }
82 channel.sendRequest(request, entityDetails, context);
83 }
84
85 }, context);
86 }
87
88 @Override
89 public int available() {
90 return handler.available();
91 }
92
93 @Override
94 public void produce(final DataStreamChannel channel) throws IOException {
95 if (log.isDebugEnabled()) {
96 log.debug("{}: produce request data", exchangeId);
97 }
98 handler.produce(new DataStreamChannel() {
99
100 @Override
101 public void requestOutput() {
102 channel.requestOutput();
103 }
104
105 @Override
106 public int write(final ByteBuffer src) throws IOException {
107 if (log.isDebugEnabled()) {
108 log.debug("{}: produce request data, len {} bytes", exchangeId, src.remaining());
109 }
110 return channel.write(src);
111 }
112
113 @Override
114 public void endStream() throws IOException {
115 if (log.isDebugEnabled()) {
116 log.debug("{}: end of request data", exchangeId);
117 }
118 channel.endStream();
119 }
120
121 @Override
122 public void endStream(final List<? extends Header> trailers) throws IOException {
123 if (log.isDebugEnabled()) {
124 log.debug("{}: end of request data", exchangeId);
125 }
126 channel.endStream(trailers);
127 }
128
129 });
130 }
131
132 @Override
133 public void consumeInformation(
134 final HttpResponse response,
135 final HttpContext context) throws HttpException, IOException {
136 if (log.isDebugEnabled()) {
137 log.debug("{}: information response {}", exchangeId, new StatusLine(response));
138 }
139 handler.consumeInformation(response, context);
140 }
141
142 @Override
143 public void consumeResponse(
144 final HttpResponse response,
145 final EntityDetails entityDetails,
146 final HttpContext context) throws HttpException, IOException {
147 if (log.isDebugEnabled()) {
148 log.debug("{}: consume response {}, {}", exchangeId, new StatusLine(response), entityDetails != null ? "entity len " + entityDetails.getContentLength() : " null entity");
149 }
150 handler.consumeResponse(response, entityDetails, context);
151 }
152
153
154 @Override
155 public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
156 handler.updateCapacity(new CapacityChannel() {
157
158 @Override
159 public void update(final int increment) throws IOException {
160 if (log.isDebugEnabled()) {
161 log.debug("{}: capacity update {}", exchangeId, increment);
162 }
163 capacityChannel.update(increment);
164 }
165
166 });
167 }
168
169 @Override
170 public void consume(final ByteBuffer src) throws IOException {
171 if (log.isDebugEnabled()) {
172 log.debug("{}: consume response data, len {} bytes", exchangeId, src.remaining());
173 }
174 handler.consume(src);
175 }
176
177 @Override
178 public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
179 if (log.isDebugEnabled()) {
180 log.debug("{}: end of response data", exchangeId);
181 }
182 handler.streamEnd(trailers);
183 }
184
185 @Override
186 public void failed(final Exception cause) {
187 if (log.isDebugEnabled()) {
188 log.debug("{}: execution failed: {}", exchangeId, cause.getMessage());
189 }
190 handler.failed(cause);
191 }
192
193 @Override
194 public void cancel() {
195 if (log.isDebugEnabled()) {
196 log.debug("{}: execution cancelled", exchangeId);
197 }
198 handler.cancel();
199 }
200
201 }