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
28 package org.apache.hc.core5.testing.classic;
29
30 import java.io.IOException;
31 import java.net.Socket;
32 import java.nio.charset.CharsetDecoder;
33 import java.nio.charset.CharsetEncoder;
34 import java.util.concurrent.atomic.AtomicLong;
35
36 import org.apache.hc.core5.http.ClassicHttpRequest;
37 import org.apache.hc.core5.http.ClassicHttpResponse;
38 import org.apache.hc.core5.http.ContentLengthStrategy;
39 import org.apache.hc.core5.http.Header;
40 import org.apache.hc.core5.http.config.Http1Config;
41 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
42 import org.apache.hc.core5.http.impl.io.SocketHolder;
43 import org.apache.hc.core5.http.io.HttpMessageParserFactory;
44 import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
45 import org.apache.hc.core5.http.message.RequestLine;
46 import org.apache.hc.core5.http.message.StatusLine;
47 import org.apache.hc.core5.io.CloseMode;
48 import org.apache.hc.core5.util.Identifiable;
49 import org.slf4j.LoggerFactory;
50 import org.slf4j.Logger;
51
52 public class LoggingBHttpClientConnection extends DefaultBHttpClientConnection implements Identifiable {
53
54 private static final AtomicLong COUNT = new AtomicLong();
55
56 private final String id;
57 private final Logger log;
58 private final Logger headerLog;
59 private final Wire wire;
60
61 public LoggingBHttpClientConnection(
62 final Http1Config http1Config,
63 final CharsetDecoder charDecoder,
64 final CharsetEncoder charEncoder,
65 final ContentLengthStrategy incomingContentStrategy,
66 final ContentLengthStrategy outgoingContentStrategy,
67 final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
68 final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
69 super(http1Config, charDecoder, charEncoder,
70 incomingContentStrategy, outgoingContentStrategy,
71 requestWriterFactory, responseParserFactory);
72 this.id = "http-outgoing-" + COUNT.incrementAndGet();
73 this.log = LoggerFactory.getLogger(getClass());
74 this.headerLog = LoggerFactory.getLogger("org.apache.hc.core5.http.headers");
75 this.wire = new Wire(LoggerFactory.getLogger("org.apache.hc.core5.http.wire"), this.id);
76 }
77
78 public LoggingBHttpClientConnection(final Http1Config http1Config) {
79 this(http1Config, null, null, null, null, null, null);
80 }
81
82 @Override
83 public String getId() {
84 return id;
85 }
86
87 @Override
88 public void close() throws IOException {
89 if (this.log.isDebugEnabled()) {
90 this.log.debug("{}: Close connection", this.id);
91 }
92 super.close();
93 }
94
95 @Override
96 public void close(final CloseMode closeMode) {
97 if (this.log.isDebugEnabled()) {
98 this.log.debug("{}: Shutdown connection", this.id);
99 }
100 super.close(closeMode);
101 }
102
103 @Override
104 public void bind(final Socket socket) throws IOException {
105 super.bind(this.wire.isEnabled() ? new LoggingSocketHolder(socket, wire) : new SocketHolder(socket));
106 }
107
108 @Override
109 protected void onResponseReceived(final ClassicHttpResponse response) {
110 if (response != null && this.headerLog.isDebugEnabled()) {
111 this.headerLog.debug("{} << {}", this.id, new StatusLine(response));
112 final Header[] headers = response.getHeaders();
113 for (final Header header : headers) {
114 this.headerLog.debug("{} << {}", this.id, header);
115 }
116 }
117 }
118
119 @Override
120 protected void onRequestSubmitted(final ClassicHttpRequest request) {
121 if (request != null && this.headerLog.isDebugEnabled()) {
122 this.headerLog.debug("{} >> {}", id, new RequestLine(request));
123 final Header[] headers = request.getHeaders();
124 for (final Header header : headers) {
125 this.headerLog.debug("{} >> {}", this.id, header);
126 }
127 }
128 }
129
130 }