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.InterruptedIOException;
32 import java.net.Socket;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import javax.net.ssl.SSLSession;
37 import javax.net.ssl.SSLSocket;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.http.Header;
42 import org.apache.http.HttpException;
43 import org.apache.http.HttpHost;
44 import org.apache.http.HttpRequest;
45 import org.apache.http.HttpResponse;
46 import org.apache.http.HttpResponseFactory;
47 import org.apache.http.annotation.NotThreadSafe;
48 import org.apache.http.conn.OperatedClientConnection;
49 import org.apache.http.conn.ManagedHttpClientConnection;
50 import org.apache.http.impl.SocketHttpClientConnection;
51 import org.apache.http.io.HttpMessageParser;
52 import org.apache.http.io.SessionInputBuffer;
53 import org.apache.http.io.SessionOutputBuffer;
54 import org.apache.http.params.BasicHttpParams;
55 import org.apache.http.params.HttpParams;
56 import org.apache.http.params.HttpProtocolParams;
57 import org.apache.http.protocol.HttpContext;
58 import org.apache.http.util.Args;
59
60
61
62
63
64
65
66
67 @NotThreadSafe
68 @Deprecated
69 public class DefaultClientConnection extends SocketHttpClientConnection
70 implements OperatedClientConnection, ManagedHttpClientConnection, HttpContext {
71
72 private final Log log = LogFactory.getLog(getClass());
73 private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
74 private final Log wireLog = LogFactory.getLog("org.apache.http.wire");
75
76
77 private volatile Socket socket;
78
79
80 private HttpHost targetHost;
81
82
83 private boolean connSecure;
84
85
86 private volatile boolean shutdown;
87
88
89 private final Map<String, Object> attributes;
90
91 public DefaultClientConnection() {
92 super();
93 this.attributes = new HashMap<String, Object>();
94 }
95
96 public String getId() {
97 return null;
98 }
99
100 public final HttpHost getTargetHost() {
101 return this.targetHost;
102 }
103
104 public final boolean isSecure() {
105 return this.connSecure;
106 }
107
108 @Override
109 public final Socket getSocket() {
110 return this.socket;
111 }
112
113 public SSLSession getSSLSession() {
114 if (this.socket instanceof SSLSocket) {
115 return ((SSLSocket) this.socket).getSession();
116 } else {
117 return null;
118 }
119 }
120
121 public void opening(final Socket sock, final HttpHost target) throws IOException {
122 assertNotOpen();
123 this.socket = sock;
124 this.targetHost = target;
125
126
127 if (this.shutdown) {
128 sock.close();
129
130 throw new InterruptedIOException("Connection already shutdown");
131 }
132 }
133
134 public void openCompleted(final boolean secure, final HttpParams params) throws IOException {
135 Args.notNull(params, "Parameters");
136 assertNotOpen();
137 this.connSecure = secure;
138 bind(this.socket, params);
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public void shutdown() throws IOException {
156 shutdown = true;
157 try {
158 super.shutdown();
159 if (log.isDebugEnabled()) {
160 log.debug("Connection " + this + " shut down");
161 }
162 final Socket sock = this.socket;
163 if (sock != null) {
164 sock.close();
165 }
166 } catch (final IOException ex) {
167 log.debug("I/O error shutting down connection", ex);
168 }
169 }
170
171 @Override
172 public void close() throws IOException {
173 try {
174 super.close();
175 if (log.isDebugEnabled()) {
176 log.debug("Connection " + this + " closed");
177 }
178 } catch (final IOException ex) {
179 log.debug("I/O error closing connection", ex);
180 }
181 }
182
183 @Override
184 protected SessionInputBuffer createSessionInputBuffer(
185 final Socket socket,
186 int buffersize,
187 final HttpParams params) throws IOException {
188 if (buffersize == -1) {
189 buffersize = 8192;
190 }
191 SessionInputBuffer inbuffer = super.createSessionInputBuffer(
192 socket,
193 buffersize,
194 params);
195 if (wireLog.isDebugEnabled()) {
196 inbuffer = new LoggingSessionInputBuffer(
197 inbuffer,
198 new Wire(wireLog),
199 HttpProtocolParams.getHttpElementCharset(params));
200 }
201 return inbuffer;
202 }
203
204 @Override
205 protected SessionOutputBuffer createSessionOutputBuffer(
206 final Socket socket,
207 int buffersize,
208 final HttpParams params) throws IOException {
209 if (buffersize == -1) {
210 buffersize = 8192;
211 }
212 SessionOutputBuffer outbuffer = super.createSessionOutputBuffer(
213 socket,
214 buffersize,
215 params);
216 if (wireLog.isDebugEnabled()) {
217 outbuffer = new LoggingSessionOutputBuffer(
218 outbuffer,
219 new Wire(wireLog),
220 HttpProtocolParams.getHttpElementCharset(params));
221 }
222 return outbuffer;
223 }
224
225 @Override
226 protected HttpMessageParser<HttpResponse> createResponseParser(
227 final SessionInputBuffer buffer,
228 final HttpResponseFactory responseFactory,
229 final HttpParams params) {
230
231 return new DefaultHttpResponseParser
232 (buffer, null, responseFactory, params);
233 }
234
235 public void bind(final Socket socket) throws IOException {
236 bind(socket, new BasicHttpParams());
237 }
238
239 public void update(final Socket sock, final HttpHost target,
240 final boolean secure, final HttpParams params)
241 throws IOException {
242
243 assertOpen();
244 Args.notNull(target, "Target host");
245 Args.notNull(params, "Parameters");
246
247 if (sock != null) {
248 this.socket = sock;
249 bind(sock, params);
250 }
251 targetHost = target;
252 connSecure = secure;
253 }
254
255 @Override
256 public HttpResponse receiveResponseHeader() throws HttpException, IOException {
257 final HttpResponse response = super.receiveResponseHeader();
258 if (log.isDebugEnabled()) {
259 log.debug("Receiving response: " + response.getStatusLine());
260 }
261 if (headerLog.isDebugEnabled()) {
262 headerLog.debug("<< " + response.getStatusLine().toString());
263 final Header[] headers = response.getAllHeaders();
264 for (final Header header : headers) {
265 headerLog.debug("<< " + header.toString());
266 }
267 }
268 return response;
269 }
270
271 @Override
272 public void sendRequestHeader(final HttpRequest request) throws HttpException, IOException {
273 if (log.isDebugEnabled()) {
274 log.debug("Sending request: " + request.getRequestLine());
275 }
276 super.sendRequestHeader(request);
277 if (headerLog.isDebugEnabled()) {
278 headerLog.debug(">> " + request.getRequestLine().toString());
279 final Header[] headers = request.getAllHeaders();
280 for (final Header header : headers) {
281 headerLog.debug(">> " + header.toString());
282 }
283 }
284 }
285
286 public Object getAttribute(final String id) {
287 return this.attributes.get(id);
288 }
289
290 public Object removeAttribute(final String id) {
291 return this.attributes.remove(id);
292 }
293
294 public void setAttribute(final String id, final Object obj) {
295 this.attributes.put(id, obj);
296 }
297
298 }