View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.message.RequestLine;
38  import org.apache.hc.core5.http.message.StatusLine;
39  import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
40  import org.apache.hc.core5.http.nio.CapacityChannel;
41  import org.apache.hc.core5.http.nio.DataStreamChannel;
42  import org.apache.hc.core5.http.nio.RequestChannel;
43  import org.apache.hc.core5.http.protocol.HttpContext;
44  import org.apache.hc.core5.util.Identifiable;
45  import org.slf4j.Logger;
46  
47  final class LoggingAsyncClientExchangeHandler implements AsyncClientExchangeHandler, Identifiable {
48  
49      private final Logger log;
50      private final String exchangeId;
51      private final AsyncClientExchangeHandler handler;
52  
53      LoggingAsyncClientExchangeHandler(final Logger log, final String exchangeId, final AsyncClientExchangeHandler handler) {
54          this.log = log;
55          this.exchangeId = exchangeId;
56          this.handler = handler;
57      }
58  
59      @Override
60      public String getId() {
61          return exchangeId;
62      }
63  
64      @Override
65      public void releaseResources() {
66          handler.releaseResources();
67      }
68  
69      @Override
70      public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
71          handler.produceRequest((request, entityDetails, context1) -> {
72              if (log.isDebugEnabled()) {
73                  log.debug("{} send request {}, {}", exchangeId, new RequestLine(request),
74                          entityDetails != null ? "entity len " + entityDetails.getContentLength() : "null entity");
75              }
76              channel.sendRequest(request, entityDetails, context1);
77          }, context);
78      }
79  
80      @Override
81      public int available() {
82          return handler.available();
83      }
84  
85      @Override
86      public void produce(final DataStreamChannel channel) throws IOException {
87          if (log.isDebugEnabled()) {
88              log.debug("{}: produce request data", exchangeId);
89          }
90          handler.produce(new DataStreamChannel() {
91  
92              @Override
93              public void requestOutput() {
94                  channel.requestOutput();
95              }
96  
97              @Override
98              public int write(final ByteBuffer src) throws IOException {
99                  if (log.isDebugEnabled()) {
100                     log.debug("{}: produce request data, len {} bytes", exchangeId, src.remaining());
101                 }
102                 return channel.write(src);
103             }
104 
105             @Override
106             public void endStream() throws IOException {
107                 if (log.isDebugEnabled()) {
108                     log.debug("{}: end of request data", exchangeId);
109                 }
110                 channel.endStream();
111             }
112 
113             @Override
114             public void endStream(final List<? extends Header> trailers) throws IOException {
115                 if (log.isDebugEnabled()) {
116                     log.debug("{}: end of request data", exchangeId);
117                 }
118                 channel.endStream(trailers);
119             }
120 
121         });
122     }
123 
124     @Override
125     public void consumeInformation(
126             final HttpResponse response,
127             final HttpContext context) throws HttpException, IOException {
128         if (log.isDebugEnabled()) {
129             log.debug("{}: information response {}", exchangeId, new StatusLine(response));
130         }
131         handler.consumeInformation(response, context);
132     }
133 
134     @Override
135     public void consumeResponse(
136             final HttpResponse response,
137             final EntityDetails entityDetails,
138             final HttpContext context) throws HttpException, IOException {
139         if (log.isDebugEnabled()) {
140             log.debug("{}: consume response {}, {}", exchangeId, new StatusLine(response), entityDetails != null ? "entity len " + entityDetails.getContentLength() : " null entity");
141         }
142         handler.consumeResponse(response, entityDetails, context);
143     }
144 
145 
146     @Override
147     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
148         handler.updateCapacity(increment -> {
149             if (log.isDebugEnabled()) {
150                 log.debug("{} capacity update {}", exchangeId, increment);
151             }
152             capacityChannel.update(increment);
153         });
154     }
155 
156     @Override
157     public void consume(final ByteBuffer src) throws IOException {
158         if (log.isDebugEnabled()) {
159             log.debug("{}: consume response data, len {} bytes", exchangeId, src.remaining());
160         }
161         handler.consume(src);
162     }
163 
164     @Override
165     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
166         if (log.isDebugEnabled()) {
167             log.debug("{}: end of response data", exchangeId);
168         }
169         handler.streamEnd(trailers);
170     }
171 
172     @Override
173     public void failed(final Exception cause) {
174         if (log.isDebugEnabled()) {
175             log.debug("{}: execution failed: {}", exchangeId, cause.getMessage());
176         }
177         handler.failed(cause);
178     }
179 
180     @Override
181     public void cancel() {
182         if (log.isDebugEnabled()) {
183             log.debug("{}: execution cancelled", exchangeId);
184         }
185         handler.cancel();
186     }
187 
188 }