View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  @Deprecated
48  public class SocketHttpServerConnection extends
49          AbstractHttpServerConnection implements HttpInetConnection {
50  
51      private volatile boolean open;
52      private volatile Socket socket = null;
53  
54      public SocketHttpServerConnection() {
55          super();
56      }
57  
58      protected void assertNotOpen() {
59          Asserts.check(!this.open, "Connection is already open");
60      }
61  
62      @Override
63      protected void assertOpen() {
64          Asserts.check(this.open, "Connection is not open");
65      }
66  
67      /**
68       * Creates an instance of {@link SocketInputBuffer} to be used for
69       * receiving data from the given {@link Socket}.
70       * <p>
71       * This method can be overridden in a super class in order to provide
72       * a custom implementation of {@link SessionInputBuffer} interface.
73       *
74       * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams)
75       *
76       * @param socket the socket.
77       * @param buffersize the buffer size.
78       * @param params HTTP parameters.
79       * @return session input buffer.
80       * @throws IOException in case of an I/O error.
81       */
82      protected SessionInputBuffer createSessionInputBuffer(
83              final Socket socket,
84              final int buffersize,
85              final HttpParams params) throws IOException {
86          return new SocketInputBuffer(socket, buffersize, params);
87      }
88  
89      /**
90       * Creates an instance of {@link SessionOutputBuffer} to be used for
91       * sending data to the given {@link Socket}.
92       * <p>
93       * This method can be overridden in a super class in order to provide
94       * a custom implementation of {@link SocketOutputBuffer} interface.
95       *
96       * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams)
97       *
98       * @param socket the socket.
99       * @param buffersize the buffer size.
100      * @param params HTTP parameters.
101      * @return session output buffer.
102      * @throws IOException in case of an I/O error.
103      */
104     protected SessionOutputBuffer createSessionOutputBuffer(
105             final Socket socket,
106             final int buffersize,
107             final HttpParams params) throws IOException {
108         return new SocketOutputBuffer(socket, buffersize, params);
109     }
110 
111     /**
112      * Binds this connection to the given {@link Socket}. This socket will be
113      * used by the connection to send and receive data.
114      * <p>
115      * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)}
116      * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods
117      * to create session input / output buffers bound to this socket and then
118      * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)}
119      * method to pass references to those buffers to the underlying HTTP message
120      * parser and formatter.
121      * <p>
122      * After this method's execution the connection status will be reported
123      * as open and the {@link #isOpen()} will return {@code true}.
124      *
125      * @param socket the socket.
126      * @param params HTTP parameters.
127      * @throws IOException in case of an I/O error.
128      */
129     protected void bind(final Socket socket, final HttpParams params) throws IOException {
130         Args.notNull(socket, "Socket");
131         Args.notNull(params, "HTTP parameters");
132         this.socket = socket;
133 
134         final int buffersize = params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1);
135         init(
136                 createSessionInputBuffer(socket, buffersize, params),
137                 createSessionOutputBuffer(socket, buffersize, params),
138                 params);
139 
140         this.open = true;
141     }
142 
143     protected Socket getSocket() {
144         return this.socket;
145     }
146 
147     @Override
148     public boolean isOpen() {
149         return this.open;
150     }
151 
152     @Override
153     public InetAddress getLocalAddress() {
154         if (this.socket != null) {
155             return this.socket.getLocalAddress();
156         } else {
157             return null;
158         }
159     }
160 
161     @Override
162     public int getLocalPort() {
163         if (this.socket != null) {
164             return this.socket.getLocalPort();
165         } else {
166             return -1;
167         }
168     }
169 
170     @Override
171     public InetAddress getRemoteAddress() {
172         if (this.socket != null) {
173             return this.socket.getInetAddress();
174         } else {
175             return null;
176         }
177     }
178 
179     @Override
180     public int getRemotePort() {
181         if (this.socket != null) {
182             return this.socket.getPort();
183         } else {
184             return -1;
185         }
186     }
187 
188     @Override
189     public void setSocketTimeout(final int timeout) {
190         assertOpen();
191         if (this.socket != null) {
192             try {
193                 this.socket.setSoTimeout(timeout);
194             } catch (final SocketException ignore) {
195                 // It is not quite clear from the Sun's documentation if there are any
196                 // other legitimate cases for a socket exception to be thrown when setting
197                 // SO_TIMEOUT besides the socket being already closed
198             }
199         }
200     }
201 
202     @Override
203     public int getSocketTimeout() {
204         if (this.socket != null) {
205             try {
206                 return this.socket.getSoTimeout();
207             } catch (final SocketException ignore) {
208                 return -1;
209             }
210         } else {
211             return -1;
212         }
213     }
214 
215     @Override
216     public void shutdown() throws IOException {
217         this.open = false;
218         final Socket tmpsocket = this.socket;
219         if (tmpsocket != null) {
220             tmpsocket.close();
221         }
222     }
223 
224     @Override
225     public void close() throws IOException {
226         if (!this.open) {
227             return;
228         }
229         this.open = false;
230         this.open = false;
231         final Socket sock = this.socket;
232         try {
233             doFlush();
234             try {
235                 try {
236                     sock.shutdownOutput();
237                 } catch (final IOException ignore) {
238                 }
239                 try {
240                     sock.shutdownInput();
241                 } catch (final IOException ignore) {
242                 }
243             } catch (final UnsupportedOperationException ignore) {
244                 // if one isn't supported, the other one isn't either
245             }
246         } finally {
247             sock.close();
248         }
249     }
250 
251     private static void formatAddress(final StringBuilder buffer, final SocketAddress socketAddress) {
252         if (socketAddress instanceof InetSocketAddress) {
253             final InetSocketAddress addr = ((InetSocketAddress) socketAddress);
254             buffer.append(addr.getAddress() != null ? addr.getAddress().getHostAddress() :
255                 addr.getAddress())
256             .append(':')
257             .append(addr.getPort());
258         } else {
259             buffer.append(socketAddress);
260         }
261     }
262 
263     @Override
264     public String toString() {
265         if (this.socket != null) {
266             final StringBuilder buffer = new StringBuilder();
267             final SocketAddress remoteAddress = this.socket.getRemoteSocketAddress();
268             final SocketAddress localAddress = this.socket.getLocalSocketAddress();
269             if (remoteAddress != null && localAddress != null) {
270                 formatAddress(buffer, localAddress);
271                 buffer.append("<->");
272                 formatAddress(buffer, remoteAddress);
273             }
274             return buffer.toString();
275         } else {
276             return super.toString();
277         }
278     }
279 
280 }