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