1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java $
3  * $Revision: 673450 $
4  * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl.conn;
33 
34 
35 import java.io.IOException;
36 import java.net.Socket;
37 
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.http.Header;
41 import org.apache.http.HttpException;
42 import org.apache.http.HttpHost;
43 import org.apache.http.HttpRequest;
44 import org.apache.http.HttpResponse;
45 import org.apache.http.HttpResponseFactory;
46 import org.apache.http.params.HttpParams;
47 import org.apache.http.impl.SocketHttpClientConnection;
48 import org.apache.http.io.HttpMessageParser;
49 import org.apache.http.io.SessionInputBuffer;
50 import org.apache.http.io.SessionOutputBuffer;
51 
52 import org.apache.http.conn.OperatedClientConnection;
53 
54 
55 /**
56  * Default implementation of an operated client connection.
57  *
58  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
59  *
60  *
61  * <!-- empty lines to avoid svn diff problems -->
62  * @version   $Revision: 673450 $ $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
63  *
64  * @since 4.0
65  *
66  * @deprecated Please use {@link java.net.URL#openConnection} instead.
67  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
68  *     for further details.
69  */
70 @Deprecated
71 public class DefaultClientConnection extends SocketHttpClientConnection
72     implements OperatedClientConnection {
73 
74     private final Log log = LogFactory.getLog(getClass());
75     private final Log headerLog = LogFactory.getLog("org.apache.http.headers");
76     private final Log wireLog = LogFactory.getLog("org.apache.http.wire");
77 
78     /** The unconnected socket */
79     private volatile Socket socket;
80 
81     /** The target host of this connection. */
82     private HttpHost targetHost;
83 
84     /** Whether this connection is secure. */
85     private boolean connSecure;
86 
87     /** True if this connection was shutdown. */
88     private volatile boolean shutdown;
89 
DefaultClientConnection()90     public DefaultClientConnection() {
91         super();
92     }
93 
94 
95     // non-javadoc, see interface OperatedClientConnection
getTargetHost()96     public final HttpHost getTargetHost() {
97         return this.targetHost;
98     }
99 
100 
101     // non-javadoc, see interface OperatedClientConnection
isSecure()102     public final boolean isSecure() {
103         return this.connSecure;
104     }
105 
106 
107     @Override
getSocket()108     public final Socket getSocket() {
109         return this.socket;
110     }
111 
112 
opening(Socket sock, HttpHost target)113     public void opening(Socket sock, HttpHost target) throws IOException {
114         assertNotOpen();
115         this.socket = sock;
116         this.targetHost = target;
117 
118         // Check for shutdown after assigning socket, so that
119         if (this.shutdown) {
120             sock.close(); // allow this to throw...
121             // ...but if it doesn't, explicitly throw one ourselves.
122             throw new IOException("Connection already shutdown");
123         }
124     }
125 
126 
openCompleted(boolean secure, HttpParams params)127     public void openCompleted(boolean secure, HttpParams params) throws IOException {
128         assertNotOpen();
129         if (params == null) {
130             throw new IllegalArgumentException
131                 ("Parameters must not be null.");
132         }
133         this.connSecure = secure;
134         bind(this.socket, params);
135     }
136 
137     /**
138      * Force-closes this connection.
139      * If the connection is still in the process of being open (the method
140      * {@link #opening opening} was already called but
141      * {@link #openCompleted openCompleted} was not), the associated
142      * socket that is being connected to a remote address will be closed.
143      * That will interrupt a thread that is blocked on connecting
144      * the socket.
145      * If the connection is not yet open, this will prevent the connection
146      * from being opened.
147      *
148      * @throws IOException      in case of a problem
149      */
150     @Override
shutdown()151     public void shutdown() throws IOException {
152         log.debug("Connection shut down");
153         shutdown = true;
154 
155         super.shutdown();
156         Socket sock = this.socket; // copy volatile attribute
157         if (sock != null)
158             sock.close();
159 
160     } // shutdown
161 
162 
163     @Override
close()164     public void close() throws IOException {
165         log.debug("Connection closed");
166         super.close();
167     }
168 
169 
170     @Override
createSessionInputBuffer( final Socket socket, int buffersize, final HttpParams params)171     protected SessionInputBuffer createSessionInputBuffer(
172             final Socket socket,
173             int buffersize,
174             final HttpParams params) throws IOException {
175         SessionInputBuffer inbuffer = super.createSessionInputBuffer(
176                 socket,
177                 buffersize,
178                 params);
179         if (wireLog.isDebugEnabled()) {
180             inbuffer = new LoggingSessionInputBuffer(inbuffer, new Wire(wireLog));
181         }
182         return inbuffer;
183     }
184 
185 
186     @Override
createSessionOutputBuffer( final Socket socket, int buffersize, final HttpParams params)187     protected SessionOutputBuffer createSessionOutputBuffer(
188             final Socket socket,
189             int buffersize,
190             final HttpParams params) throws IOException {
191         SessionOutputBuffer outbuffer = super.createSessionOutputBuffer(
192                 socket,
193                 buffersize,
194                 params);
195         if (wireLog.isDebugEnabled()) {
196             outbuffer = new LoggingSessionOutputBuffer(outbuffer, new Wire(wireLog));
197         }
198         return outbuffer;
199     }
200 
201 
202     @Override
createResponseParser( final SessionInputBuffer buffer, final HttpResponseFactory responseFactory, final HttpParams params)203     protected HttpMessageParser createResponseParser(
204             final SessionInputBuffer buffer,
205             final HttpResponseFactory responseFactory,
206             final HttpParams params) {
207         // override in derived class to specify a line parser
208         return new DefaultResponseParser
209             (buffer, null, responseFactory, params);
210     }
211 
212 
213     // non-javadoc, see interface OperatedClientConnection
update(Socket sock, HttpHost target, boolean secure, HttpParams params)214     public void update(Socket sock, HttpHost target,
215                        boolean secure, HttpParams params)
216         throws IOException {
217 
218         assertOpen();
219         if (target == null) {
220             throw new IllegalArgumentException
221                 ("Target host must not be null.");
222         }
223         if (params == null) {
224             throw new IllegalArgumentException
225                 ("Parameters must not be null.");
226         }
227 
228         if (sock != null) {
229             this.socket = sock;
230             bind(sock, params);
231         }
232         targetHost = target;
233         connSecure = secure;
234 
235     } // update
236 
237 
238     @Override
receiveResponseHeader()239     public HttpResponse receiveResponseHeader() throws HttpException, IOException {
240         HttpResponse response = super.receiveResponseHeader();
241         if (headerLog.isDebugEnabled()) {
242             headerLog.debug("<< " + response.getStatusLine().toString());
243             Header[] headers = response.getAllHeaders();
244             for (Header header : headers) {
245                 headerLog.debug("<< " + header.toString());
246             }
247         }
248         return response;
249     }
250 
251 
252     @Override
sendRequestHeader(HttpRequest request)253     public void sendRequestHeader(HttpRequest request) throws HttpException, IOException {
254         super.sendRequestHeader(request);
255         if (headerLog.isDebugEnabled()) {
256             headerLog.debug(">> " + request.getRequestLine().toString());
257             Header[] headers = request.getAllHeaders();
258             for (Header header : headers) {
259                 headerLog.debug(">> " + header.toString());
260             }
261         }
262     }
263 
264 } // class DefaultClientConnection
265