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