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 if (this.original instanceof SSLIOSession) {
111 return ((SSLIOSession) this.original).getSSLSession();
112 } else {
113 return null;
114 }
115 }
116
117 @Override
118 public String getId() {
119 return this.id;
120 }
121
122 @Override
123 protected void onResponseReceived(final HttpResponse response) {
124 if (response != null && this.headerlog.isDebugEnabled()) {
125 this.headerlog.debug(this.id + " << " + response.getStatusLine().toString());
126 final Header[] headers = response.getAllHeaders();
127 for (final Header header : headers) {
128 this.headerlog.debug(this.id + " << " + header.toString());
129 }
130 }
131 }
132
133 @Override
134 protected void onRequestSubmitted(final HttpRequest request) {
135 if (request != null && this.headerlog.isDebugEnabled()) {
136 this.headerlog.debug(this.id + " >> " + request.getRequestLine().toString());
137 final Header[] headers = request.getAllHeaders();
138 for (final Header header : headers) {
139 this.headerlog.debug(this.id + " >> " + header.toString());
140 }
141 }
142 }
143
144 @Override
145 public String toString() {
146 final StringBuilder buf = new StringBuilder();
147 buf.append(this.id);
148 buf.append(" [");
149 switch (this.status) {
150 case ACTIVE:
151 buf.append("ACTIVE");
152 if (this.inbuf.hasData()) {
153 buf.append("(").append(this.inbuf.length()).append(")");
154 }
155 break;
156 case CLOSING:
157 buf.append("CLOSING");
158 break;
159 case CLOSED:
160 buf.append("CLOSED");
161 break;
162 }
163 buf.append("]");
164 return buf.toString();
165 }
166
167 }