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;
29
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.Socket;
34 import java.net.SocketAddress;
35 import java.net.SocketException;
36
37 import org.apache.http.HttpInetConnection;
38 import org.apache.http.impl.io.SocketInputBuffer;
39 import org.apache.http.impl.io.SocketOutputBuffer;
40 import org.apache.http.io.SessionInputBuffer;
41 import org.apache.http.io.SessionOutputBuffer;
42 import org.apache.http.params.CoreConnectionPNames;
43 import org.apache.http.params.HttpParams;
44 import org.apache.http.util.Args;
45 import org.apache.http.util.Asserts;
46
47
48
49
50
51
52
53
54
55
56 @Deprecated
57 public class SocketHttpClientConnection
58 extends AbstractHttpClientConnection implements HttpInetConnection {
59
60 private volatile boolean open;
61 private volatile Socket socket = null;
62
63 public SocketHttpClientConnection() {
64 super();
65 }
66
67 protected void assertNotOpen() {
68 Asserts.check(!this.open, "Connection is already open");
69 }
70
71 @Override
72 protected void assertOpen() {
73 Asserts.check(this.open, "Connection is not open");
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 protected SessionInputBuffer createSessionInputBuffer(
92 final Socket socket,
93 final int bufferSize,
94 final HttpParams params) throws IOException {
95 return new SocketInputBuffer(socket, bufferSize, params);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 protected SessionOutputBuffer createSessionOutputBuffer(
114 final Socket socket,
115 final int bufferSize,
116 final HttpParams params) throws IOException {
117 return new SocketOutputBuffer(socket, bufferSize, params);
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 protected void bind(
139 final Socket socket,
140 final HttpParams params) throws IOException {
141 Args.notNull(socket, "Socket");
142 Args.notNull(params, "HTTP parameters");
143 this.socket = socket;
144
145 final int bufferSize = params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1);
146 init(
147 createSessionInputBuffer(socket, bufferSize, params),
148 createSessionOutputBuffer(socket, bufferSize, params),
149 params);
150
151 this.open = true;
152 }
153
154 @Override
155 public boolean isOpen() {
156 return this.open;
157 }
158
159 protected Socket getSocket() {
160 return this.socket;
161 }
162
163 @Override
164 public InetAddress getLocalAddress() {
165 if (this.socket != null) {
166 return this.socket.getLocalAddress();
167 }
168 return null;
169 }
170
171 @Override
172 public int getLocalPort() {
173 if (this.socket != null) {
174 return this.socket.getLocalPort();
175 }
176 return -1;
177 }
178
179 @Override
180 public InetAddress getRemoteAddress() {
181 if (this.socket != null) {
182 return this.socket.getInetAddress();
183 }
184 return null;
185 }
186
187 @Override
188 public int getRemotePort() {
189 if (this.socket != null) {
190 return this.socket.getPort();
191 }
192 return -1;
193 }
194
195 @Override
196 public void setSocketTimeout(final int timeout) {
197 assertOpen();
198 if (this.socket != null) {
199 try {
200 this.socket.setSoTimeout(timeout);
201 } catch (final SocketException ignore) {
202
203
204
205 }
206 }
207 }
208
209 @Override
210 public int getSocketTimeout() {
211 if (this.socket != null) {
212 try {
213 return this.socket.getSoTimeout();
214 } catch (final SocketException ignore) {
215
216 }
217 }
218 return -1;
219 }
220
221 @Override
222 public void shutdown() throws IOException {
223 this.open = false;
224 final Socket tmpsocket = this.socket;
225 if (tmpsocket != null) {
226 tmpsocket.close();
227 }
228 }
229
230 @Override
231 public void close() throws IOException {
232 if (!this.open) {
233 return;
234 }
235 this.open = false;
236 final Socket sock = this.socket;
237 try {
238 doFlush();
239 try {
240 try {
241 sock.shutdownOutput();
242 } catch (final IOException ignore) {
243
244 }
245 try {
246 sock.shutdownInput();
247 } catch (final IOException ignore) {
248
249 }
250 } catch (final UnsupportedOperationException ignore) {
251
252 }
253 } finally {
254 sock.close();
255 }
256 }
257
258 private static void formatAddress(final StringBuilder buffer, final SocketAddress socketAddress) {
259 if (socketAddress instanceof InetSocketAddress) {
260 final InetSocketAddress addr = ((InetSocketAddress) socketAddress);
261 buffer.append(addr.getAddress() != null ? addr.getAddress().getHostAddress() :
262 addr.getAddress())
263 .append(':')
264 .append(addr.getPort());
265 } else {
266 buffer.append(socketAddress);
267 }
268 }
269
270 @Override
271 public String toString() {
272 if (this.socket != null) {
273 final StringBuilder buffer = new StringBuilder();
274 final SocketAddress remoteAddress = this.socket.getRemoteSocketAddress();
275 final SocketAddress localAddress = this.socket.getLocalSocketAddress();
276 if (remoteAddress != null && localAddress != null) {
277 formatAddress(buffer, localAddress);
278 buffer.append("<->");
279 formatAddress(buffer, remoteAddress);
280 }
281 return buffer.toString();
282 }
283 return super.toString();
284 }
285
286 }