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.nio.testserver;
29
30 import java.io.IOException;
31 import java.util.concurrent.atomic.AtomicLong;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.http.Header;
36 import org.apache.http.HttpException;
37 import org.apache.http.HttpRequest;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.impl.nio.DefaultNHttpClientConnection;
40 import org.apache.http.nio.NHttpClientEventHandler;
41 import org.apache.http.nio.reactor.IOSession;
42
43 public class LoggingNHttpClientConnection extends DefaultNHttpClientConnection {
44
45 private static final AtomicLong COUNT = new AtomicLong();
46
47 private final Log log;
48 private final Log iolog;
49 private final Log headerLog;
50 private final Log wireLog;
51 private final String id;
52
53 public LoggingNHttpClientConnection(final IOSession session) {
54 super(session, 8 * 1024);
55 this.log = LogFactory.getLog(getClass());
56 this.iolog = LogFactory.getLog(session.getClass());
57 this.headerLog = LogFactory.getLog("org.apache.http.headers");
58 this.wireLog = LogFactory.getLog("org.apache.http.wire");
59 this.id = "http-outgoing-" + COUNT.incrementAndGet();
60 if (this.iolog.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
61 this.session = new LoggingIOSession(session, this.id, this.iolog, this.wireLog);
62 }
63 }
64
65 @Override
66 public void close() throws IOException {
67 if (this.log.isDebugEnabled()) {
68 this.log.debug(this.id + ": Close connection");
69 }
70 super.close();
71 }
72
73 @Override
74 public void shutdown() throws IOException {
75 if (this.log.isDebugEnabled()) {
76 this.log.debug(this.id + ": Shutdown connection");
77 }
78 super.shutdown();
79 }
80
81 @Override
82 public void submitRequest(final HttpRequest request) throws IOException, HttpException {
83 if (this.log.isDebugEnabled()) {
84 this.log.debug(this.id + ": " + request.getRequestLine().toString());
85 }
86 super.submitRequest(request);
87 }
88
89 @Override
90 public void consumeInput(final NHttpClientEventHandler handler) {
91 if (this.log.isDebugEnabled()) {
92 this.log.debug(this.id + ": Consume input");
93 }
94 super.consumeInput(handler);
95 }
96
97 @Override
98 public void produceOutput(final NHttpClientEventHandler handler) {
99 if (this.log.isDebugEnabled()) {
100 this.log.debug(this.id + ": Produce output");
101 }
102 super.produceOutput(handler);
103 }
104
105 @Override
106 protected void onResponseReceived(final HttpResponse response) {
107 if (response != null && this.headerLog.isDebugEnabled()) {
108 this.headerLog.debug(this.id + " << " + response.getStatusLine().toString());
109 final Header[] headers = response.getAllHeaders();
110 for (final Header header : headers) {
111 this.headerLog.debug(this.id + " << " + header.toString());
112 }
113 }
114 }
115
116 @Override
117 protected void onRequestSubmitted(final HttpRequest request) {
118 if (request != null && this.headerLog.isDebugEnabled()) {
119 this.headerLog.debug(id + " >> " + request.getRequestLine().toString());
120 final Header[] headers = request.getAllHeaders();
121 for (final Header header : headers) {
122 this.headerLog.debug(this.id + " >> " + header.toString());
123 }
124 }
125 }
126
127 @Override
128 public String toString() {
129 return this.id;
130 }
131
132 }