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