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.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 }