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 package org.apache.http.impl.nio.conn;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.http.Header;
32 import org.apache.http.HttpRequest;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpResponseFactory;
35 import org.apache.http.impl.nio.DefaultNHttpClientConnection;
36 import org.apache.http.nio.conn.ClientAsyncConnection;
37 import org.apache.http.nio.reactor.IOSession;
38 import org.apache.http.nio.util.ByteBufferAllocator;
39 import org.apache.http.params.HttpParams;
40
41 @Deprecated
42 public class DefaultClientAsyncConnection
43 extends DefaultNHttpClientConnection implements ClientAsyncConnection {
44
45 private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
46 private final Log wireLog = LogFactory.getLog("org.apache.http.wire");
47 private final Log log;
48
49 private final String id;
50 private IOSession original;
51
52 public DefaultClientAsyncConnection(
53 final String id,
54 final IOSession ioSession,
55 final HttpResponseFactory responseFactory,
56 final ByteBufferAllocator allocator,
57 final HttpParams params) {
58 super(ioSession, responseFactory, allocator, params);
59 this.id = id;
60 this.original = ioSession;
61 this.log = LogFactory.getLog(ioSession.getClass());
62 if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
63 bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog));
64 }
65 }
66
67 @Override
68 public void upgrade(final IOSession ioSession) {
69 this.original = ioSession;
70 if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
71 this.log.debug(this.id + " Upgrade session " + ioSession);
72 bind(new LoggingIOSession(ioSession, this.id, this.headerLog, this.wireLog));
73 } else {
74 bind(ioSession);
75 }
76 }
77
78 @Override
79 public IOSession getIOSession() {
80 return this.original;
81 }
82
83 public String getId() {
84 return this.id;
85 }
86
87 @Override
88 protected void onResponseReceived(final HttpResponse response) {
89 if (response != null && this.headerLog.isDebugEnabled()) {
90 this.headerLog.debug(this.id + " << " + response.getStatusLine().toString());
91 final Header[] headers = response.getAllHeaders();
92 for (final Header header : headers) {
93 this.headerLog.debug(this.id + " << " + header.toString());
94 }
95 }
96 }
97
98 @Override
99 protected void onRequestSubmitted(final HttpRequest request) {
100 if (request != null && this.headerLog.isDebugEnabled()) {
101 this.headerLog.debug(this.id + " >> " + request.getRequestLine().toString());
102 final Header[] headers = request.getAllHeaders();
103 for (final Header header : headers) {
104 this.headerLog.debug(this.id + " >> " + header.toString());
105 }
106 }
107 }
108
109 @Override
110 public String toString() {
111 final StringBuilder buf = new StringBuilder();
112 buf.append(this.id);
113 buf.append(" [");
114 switch (this.status) {
115 case ACTIVE:
116 buf.append("ACTIVE");
117 if (this.inbuf.hasData()) {
118 buf.append("(").append(this.inbuf.length()).append(")");
119 }
120 break;
121 case CLOSING:
122 buf.append("CLOSING");
123 break;
124 case CLOSED:
125 buf.append("CLOSED");
126 break;
127 }
128 buf.append("]");
129 return buf.toString();
130 }
131
132 }