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.http.testserver;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.net.Socket;
34 import java.nio.charset.CharsetDecoder;
35 import java.nio.charset.CharsetEncoder;
36 import java.util.concurrent.atomic.AtomicLong;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.http.Header;
41 import org.apache.http.HttpRequest;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.config.MessageConstraints;
44 import org.apache.http.entity.ContentLengthStrategy;
45 import org.apache.http.impl.DefaultBHttpClientConnection;
46 import org.apache.http.io.HttpMessageParserFactory;
47 import org.apache.http.io.HttpMessageWriterFactory;
48
49 public class LoggingBHttpClientConnection extends DefaultBHttpClientConnection {
50
51 private static final AtomicLong COUNT = new AtomicLong();
52
53 private final String id;
54 private final Log log;
55 private final Log headerLog;
56 private final Wire wire;
57
58 public LoggingBHttpClientConnection(
59 final int bufferSize,
60 final int fragmentSizeHint,
61 final CharsetDecoder charDecoder,
62 final CharsetEncoder charEncoder,
63 final MessageConstraints constraints,
64 final ContentLengthStrategy incomingContentStrategy,
65 final ContentLengthStrategy outgoingContentStrategy,
66 final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
67 final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
68 super(bufferSize, fragmentSizeHint, charDecoder, charEncoder,
69 constraints, incomingContentStrategy, outgoingContentStrategy,
70 requestWriterFactory, responseParserFactory);
71 this.id = "http-outgoing-" + COUNT.incrementAndGet();
72 this.log = LogFactory.getLog(getClass());
73 this.headerLog = LogFactory.getLog("org.apache.http.headers");
74 this.wire = new Wire(LogFactory.getLog("org.apache.http.wire"), this.id);
75 }
76
77 public LoggingBHttpClientConnection(final int bufferSize) {
78 this(bufferSize, bufferSize, null, null, null, null, null, null, null);
79 }
80
81 @Override
82 public void close() throws IOException {
83 if (this.log.isDebugEnabled()) {
84 this.log.debug(this.id + ": Close connection");
85 }
86 super.close();
87 }
88
89 @Override
90 public void shutdown() throws IOException {
91 if (this.log.isDebugEnabled()) {
92 this.log.debug(this.id + ": Shutdown connection");
93 }
94 super.shutdown();
95 }
96
97 @Override
98 protected InputStream getSocketInputStream(final Socket socket) throws IOException {
99 InputStream in = super.getSocketInputStream(socket);
100 if (wire.isEnabled()) {
101 in = new LoggingInputStream(in, wire);
102 }
103 return in;
104 }
105
106 @Override
107 protected OutputStream getSocketOutputStream(final Socket socket) throws IOException {
108 OutputStream out = super.getSocketOutputStream(socket);
109 if (wire.isEnabled()) {
110 out = new LoggingOutputStream(out, wire);
111 }
112 return out;
113 }
114
115 @Override
116 protected void onResponseReceived(final HttpResponse response) {
117 if (response != null && this.headerLog.isDebugEnabled()) {
118 this.headerLog.debug(this.id + " << " + response.getStatusLine().toString());
119 final Header[] headers = response.getAllHeaders();
120 for (final Header header : headers) {
121 this.headerLog.debug(this.id + " << " + header.toString());
122 }
123 }
124 }
125
126 @Override
127 protected void onRequestSubmitted(final HttpRequest request) {
128 if (request != null && this.headerLog.isDebugEnabled()) {
129 this.headerLog.debug(id + " >> " + request.getRequestLine().toString());
130 final Header[] headers = request.getAllHeaders();
131 for (final Header header : headers) {
132 this.headerLog.debug(this.id + " >> " + header.toString());
133 }
134 }
135 }
136
137 }