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  /**
48   * @deprecated Do not use.
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       * Creates an instance of {@link SocketInputBuffer} to be used for
72       * receiving data from the given {@link Socket}.
73       * <p>
74       * This method can be overridden in a super class in order to provide
75       * a custom implementation of {@link SessionInputBuffer} interface.
76       *
77       * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams)
78       *
79       * @param socket the socket.
80       * @param bufferSize the buffer size.
81       * @param params HTTP parameters.
82       * @return session input buffer.
83       * @throws IOException in case of an I/O error.
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       * Creates an instance of {@link SessionOutputBuffer} to be used for
94       * sending data to the given {@link Socket}.
95       * <p>
96       * This method can be overridden in a super class in order to provide
97       * a custom implementation of {@link SocketOutputBuffer} interface.
98       *
99       * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams)
100      *
101      * @param socket the socket.
102      * @param bufferSize the buffer size.
103      * @param params HTTP parameters.
104      * @return session output buffer.
105      * @throws IOException in case of an I/O error.
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      * Binds this connection to the given {@link Socket}. This socket will be
116      * used by the connection to send and receive data.
117      * <p>
118      * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)}
119      * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods
120      * to create session input / output buffers bound to this socket and then
121      * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)}
122      * method to pass references to those buffers to the underlying HTTP message
123      * parser and formatter.
124      * <p>
125      * After this method's execution the connection status will be reported
126      * as open and the {@link #isOpen()} will return {@code true}.
127      *
128      * @param socket the socket.
129      * @param params HTTP parameters.
130      * @throws IOException in case of an I/O error.
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                 // It is not quite clear from the Sun's documentation if there are any
195                 // other legitimate cases for a socket exception to be thrown when setting
196                 // SO_TIMEOUT besides the socket being already closed
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                 // ignore
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                 // if one isn't supported, the other one isn't either
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 }