1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java $
3  * $Revision: 561083 $
4  * $Date: 2007-07-30 11:31:17 -0700 (Mon, 30 Jul 2007) $
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;
33 
34 import java.io.IOException;
35 import java.net.InetAddress;
36 import java.net.Socket;
37 import java.net.SocketException;
38 
39 import org.apache.http.HttpInetConnection;
40 import org.apache.http.impl.io.SocketInputBuffer;
41 import org.apache.http.impl.io.SocketOutputBuffer;
42 import org.apache.http.io.SessionInputBuffer;
43 import org.apache.http.io.SessionOutputBuffer;
44 import org.apache.http.params.HttpConnectionParams;
45 import org.apache.http.params.HttpParams;
46 
47 /**
48  * Implementation of a server-side HTTP connection that can be bound to a
49  * network Socket in order to receive and transmit data.
50  *
51  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
52  *
53  * @version $Revision: 561083 $
54  *
55  * @since 4.0
56  *
57  * @deprecated Please use {@link java.net.URL#openConnection} instead.
58  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
59  *     for further details.
60  */
61 @Deprecated
62 public class SocketHttpServerConnection extends
63         AbstractHttpServerConnection implements HttpInetConnection {
64 
65     private volatile boolean open;
66     private Socket socket = null;
67 
SocketHttpServerConnection()68     public SocketHttpServerConnection() {
69         super();
70     }
71 
assertNotOpen()72     protected void assertNotOpen() {
73         if (this.open) {
74             throw new IllegalStateException("Connection is already open");
75         }
76     }
77 
assertOpen()78     protected void assertOpen() {
79         if (!this.open) {
80             throw new IllegalStateException("Connection is not open");
81         }
82     }
83 
createHttpDataReceiver( final Socket socket, int buffersize, final HttpParams params)84     protected SessionInputBuffer createHttpDataReceiver(
85             final Socket socket,
86             int buffersize,
87             final HttpParams params) throws IOException {
88         return new SocketInputBuffer(socket, buffersize, params);
89     }
90 
createHttpDataTransmitter( final Socket socket, int buffersize, final HttpParams params)91     protected SessionOutputBuffer createHttpDataTransmitter(
92             final Socket socket,
93             int buffersize,
94             final HttpParams params) throws IOException {
95         return new SocketOutputBuffer(socket, buffersize, params);
96     }
97 
bind(final Socket socket, final HttpParams params)98     protected void bind(final Socket socket, final HttpParams params) throws IOException {
99         if (socket == null) {
100             throw new IllegalArgumentException("Socket may not be null");
101         }
102         if (params == null) {
103             throw new IllegalArgumentException("HTTP parameters may not be null");
104         }
105         this.socket = socket;
106 
107         int buffersize = HttpConnectionParams.getSocketBufferSize(params);
108 
109         init(
110                 createHttpDataReceiver(socket, buffersize, params),
111                 createHttpDataTransmitter(socket, buffersize, params),
112                 params);
113 
114         this.open = true;
115     }
116 
getSocket()117     protected Socket getSocket() {
118         return this.socket;
119     }
120 
isOpen()121     public boolean isOpen() {
122         return this.open;
123     }
124 
getLocalAddress()125     public InetAddress getLocalAddress() {
126         if (this.socket != null) {
127             return this.socket.getLocalAddress();
128         } else {
129             return null;
130         }
131     }
132 
getLocalPort()133     public int getLocalPort() {
134         if (this.socket != null) {
135             return this.socket.getLocalPort();
136         } else {
137             return -1;
138         }
139     }
140 
getRemoteAddress()141     public InetAddress getRemoteAddress() {
142         if (this.socket != null) {
143             return this.socket.getInetAddress();
144         } else {
145             return null;
146         }
147     }
148 
getRemotePort()149     public int getRemotePort() {
150         if (this.socket != null) {
151             return this.socket.getPort();
152         } else {
153             return -1;
154         }
155     }
156 
setSocketTimeout(int timeout)157     public void setSocketTimeout(int timeout) {
158         assertOpen();
159         if (this.socket != null) {
160             try {
161                 this.socket.setSoTimeout(timeout);
162             } catch (SocketException ignore) {
163                 // It is not quite clear from the Sun's documentation if there are any
164                 // other legitimate cases for a socket exception to be thrown when setting
165                 // SO_TIMEOUT besides the socket being already closed
166             }
167         }
168     }
169 
getSocketTimeout()170     public int getSocketTimeout() {
171         if (this.socket != null) {
172             try {
173                 return this.socket.getSoTimeout();
174             } catch (SocketException ignore) {
175                 return -1;
176             }
177         } else {
178             return -1;
179         }
180     }
181 
shutdown()182     public void shutdown() throws IOException {
183         this.open = false;
184         Socket tmpsocket = this.socket;
185         if (tmpsocket != null) {
186             tmpsocket.close();
187         }
188     }
189 
close()190     public void close() throws IOException {
191         if (!this.open) {
192             return;
193         }
194         this.open = false;
195         doFlush();
196         try {
197             this.socket.shutdownOutput();
198         } catch (IOException ignore) {
199         }
200         try {
201             this.socket.shutdownInput();
202         } catch (IOException ignore) {
203         }
204         this.socket.close();
205     }
206 
207 }
208