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),
81                              entityDetails != null ? "entity len " + entityDetails.getContentLength() : "null entity");
82                  }
83                  channel.sendRequest(request, entityDetails, context);
84              }
85  
86          }, context);
87      }
88  
89      @Override
90      public int available() {
91          return handler.available();
92      }
93  
94      @Override
95      public void produce(final DataStreamChannel channel) throws IOException {
96          if (log.isDebugEnabled()) {
97              log.debug("{} produce request data", exchangeId);
98          }
99          handler.produce(new DataStreamChannel() {
100 
101             @Override
102             public void requestOutput() {
103                 channel.requestOutput();
104             }
105 
106             @Override
107             public int write(final ByteBuffer src) throws IOException {
108                 if (log.isDebugEnabled()) {
109                     log.debug("{} produce request data, len {} bytes", exchangeId, src.remaining());
110                 }
111                 return channel.write(src);
112             }
113 
114             @Override
115             public void endStream() throws IOException {
116                 if (log.isDebugEnabled()) {
117                     log.debug("{} end of request data", exchangeId);
118                 }
119                 channel.endStream();
120             }
121 
122             @Override
123             public void endStream(final List<? extends Header> trailers) throws IOException {
124                 if (log.isDebugEnabled()) {
125                     log.debug("{} end of request data", exchangeId);
126                 }
127                 channel.endStream(trailers);
128             }
129 
130         });
131     }
132 
133     @Override
134     public void consumeInformation(
135             final HttpResponse response,
136             final HttpContext context) throws HttpException, IOException {
137         if (log.isDebugEnabled()) {
138             log.debug("{} information response {}", exchangeId, new StatusLine(response));
139         }
140         handler.consumeInformation(response, context);
141     }
142 
143     @Override
144     public void consumeResponse(
145             final HttpResponse response,
146             final EntityDetails entityDetails,
147             final HttpContext context) throws HttpException, IOException {
148         if (log.isDebugEnabled()) {
149             log.debug("{} consume response {}, {}", exchangeId, new StatusLine(response), entityDetails != null ? "entity len " + entityDetails.getContentLength() : " null entity");
150         }
151         handler.consumeResponse(response, entityDetails, context);
152     }
153 
154 
155     @Override
156     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
157         handler.updateCapacity(new CapacityChannel() {
158 
159             @Override
160             public void update(final int increment) throws IOException {
161                 if (log.isDebugEnabled()) {
162                     log.debug("{} capacity update {}", exchangeId, increment);
163                 }
164                 capacityChannel.update(increment);
165             }
166 
167         });
168     }
169 
170     @Override
171     public void consume(final ByteBuffer src) throws IOException {
172         if (log.isDebugEnabled()) {
173             log.debug("{} consume response data, len {} bytes", exchangeId, src.remaining());
174         }
175         handler.consume(src);
176     }
177 
178     @Override
179     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
180         if (log.isDebugEnabled()) {
181             log.debug("{} end of response data", exchangeId);
182         }
183         handler.streamEnd(trailers);
184     }
185 
186     @Override
187     public void failed(final Exception cause) {
188         if (log.isDebugEnabled()) {
189             log.debug("{} execution failed: {}", exchangeId, cause.getMessage());
190         }
191         handler.failed(cause);
192     }
193 
194     @Override
195     public void cancel() {
196         if (log.isDebugEnabled()) {
197             log.debug("{} execution cancelled", exchangeId);
198         }
199         handler.cancel();
200     }
201 
202 }