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