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