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 java.nio.charset.CharsetDecoder;
30 import java.nio.charset.CharsetEncoder;
31
32 import javax.net.ssl.SSLSession;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.http.Header;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.config.MessageConstraints;
39 import org.apache.http.entity.ContentLengthStrategy;
40 import org.apache.http.impl.nio.DefaultNHttpClientConnection;
41 import org.apache.http.nio.NHttpMessageParserFactory;
42 import org.apache.http.nio.NHttpMessageWriterFactory;
43 import org.apache.http.nio.conn.ManagedNHttpClientConnection;
44 import org.apache.http.nio.reactor.IOSession;
45 import org.apache.http.nio.reactor.ssl.SSLIOSession;
46 import org.apache.http.nio.util.ByteBufferAllocator;
47 import org.apache.http.util.Args;
48 import org.apache.http.util.Asserts;
49
50 class ManagedNHttpClientConnectionImpl
51 extends DefaultNHttpClientConnection implements ManagedNHttpClientConnection {
52
53 private final Log headerLog;
54 private final Log wireLog;
55 private final Log log;
56
57 private final String id;
58 private IOSession original;
59
60 public ManagedNHttpClientConnectionImpl(
61 final String id,
62 final Log log,
63 final Log headerLog,
64 final Log wireLog,
65 final IOSession ioSession,
66 final int bufferSize,
67 final int fragmentSizeHint,
68 final ByteBufferAllocator allocator,
69 final CharsetDecoder charDecoder,
70 final CharsetEncoder charEncoder,
71 final MessageConstraints constraints,
72 final ContentLengthStrategy incomingContentStrategy,
73 final ContentLengthStrategy outgoingContentStrategy,
74 final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
75 final NHttpMessageParserFactory<HttpResponse> responseParserFactory) {
76 super(ioSession, bufferSize, fragmentSizeHint, allocator, charDecoder, charEncoder, constraints,
77 incomingContentStrategy, outgoingContentStrategy,
78 requestWriterFactory, responseParserFactory);
79 this.id = id;
80 this.log = log;
81 this.headerLog = headerLog;
82 this.wireLog = wireLog;
83 this.original = ioSession;
84 if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
85 super.bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog));
86 }
87 }
88
89 @Override
90 public void bind(final IOSession ioSession) {
91 Args.notNull(ioSession, "I/O session");
92 Asserts.check(!ioSession.isClosed(), "I/O session is closed");
93 this.status = ACTIVE;
94 this.original = ioSession;
95 if (this.log.isDebugEnabled() || this.wireLog.isDebugEnabled()) {
96 this.log.debug(this.id + " Upgrade session " + ioSession);
97 super.bind(new LoggingIOSession(ioSession, this.id, this.log, this.wireLog));
98 } else {
99 super.bind(ioSession);
100 }
101 }
102
103 @Override
104 public IOSession getIOSession() {
105 return this.original;
106 }
107
108 @Override
109 public SSLSession getSSLSession() {
110 return this.original instanceof SSLIOSession
111 ? ((SSLIOSession) this.original).getSSLSession()
112 : null;
113 }
114
115 @Override
116 public String getId() {
117 return this.id;
118 }
119
120 @Override
121 protected void onResponseReceived(final HttpResponse response) {
122 if (response != null && this.headerLog.isDebugEnabled()) {
123 this.headerLog.debug(this.id + " << " + response.getStatusLine().toString());
124 final Header[] headers = response.getAllHeaders();
125 for (final Header header : headers) {
126 this.headerLog.debug(this.id + " << " + header.toString());
127 }
128 }
129 }
130
131 @Override
132 protected void onRequestSubmitted(final HttpRequest request) {
133 if (request != null && this.headerLog.isDebugEnabled()) {
134 this.headerLog.debug(this.id + " >> " + request.getRequestLine().toString());
135 final Header[] headers = request.getAllHeaders();
136 for (final Header header : headers) {
137 this.headerLog.debug(this.id + " >> " + header.toString());
138 }
139 }
140 }
141
142 @Override
143 public String toString() {
144 final StringBuilder buf = new StringBuilder();
145 buf.append(this.id);
146 buf.append(" [");
147 switch (this.status) {
148 case ACTIVE:
149 buf.append("ACTIVE");
150 if (this.inbuf.hasData()) {
151 buf.append("(").append(this.inbuf.length()).append(")");
152 }
153 break;
154 case CLOSING:
155 buf.append("CLOSING");
156 break;
157 case CLOSED:
158 buf.append("CLOSED");
159 break;
160 }
161 buf.append("]");
162 return buf.toString();
163 }
164
165 }