/lib/src/org/apache/http/impl/SocketHttpServerConnection.java

http://github.com/onedanshow/Screen-Courter · Java · 281 lines · 154 code · 27 blank · 100 comment · 26 complexity · 843b813d129a71d9ef0993e8646b6ea4 MD5 · raw file

  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. package org.apache.http.impl;
  28. import java.io.IOException;
  29. import java.net.InetAddress;
  30. import java.net.Socket;
  31. import java.net.SocketException;
  32. import org.apache.http.HttpInetConnection;
  33. import org.apache.http.impl.io.SocketInputBuffer;
  34. import org.apache.http.impl.io.SocketOutputBuffer;
  35. import org.apache.http.io.SessionInputBuffer;
  36. import org.apache.http.io.SessionOutputBuffer;
  37. import org.apache.http.params.HttpConnectionParams;
  38. import org.apache.http.params.HttpParams;
  39. /**
  40. * Implementation of a server-side HTTP connection that can be bound to a
  41. * network Socket in order to receive and transmit data.
  42. * <p>
  43. * The following parameters can be used to customize the behavior of this
  44. * class:
  45. * <ul>
  46. * <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
  47. * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
  48. * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
  49. * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
  50. * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
  51. * </ul>
  52. *
  53. * @since 4.0
  54. */
  55. public class SocketHttpServerConnection extends
  56. AbstractHttpServerConnection implements HttpInetConnection {
  57. private volatile boolean open;
  58. private volatile Socket socket = null;
  59. public SocketHttpServerConnection() {
  60. super();
  61. }
  62. protected void assertNotOpen() {
  63. if (this.open) {
  64. throw new IllegalStateException("Connection is already open");
  65. }
  66. }
  67. protected void assertOpen() {
  68. if (!this.open) {
  69. throw new IllegalStateException("Connection is not open");
  70. }
  71. }
  72. /**
  73. * @deprecated Use {@link #createSessionInputBuffer(Socket, int, HttpParams)}
  74. */
  75. protected SessionInputBuffer createHttpDataReceiver(
  76. final Socket socket,
  77. int buffersize,
  78. final HttpParams params) throws IOException {
  79. return createSessionInputBuffer(socket, buffersize, params);
  80. }
  81. /**
  82. * @deprecated Use {@link #createSessionOutputBuffer(Socket, int, HttpParams)}
  83. */
  84. protected SessionOutputBuffer createHttpDataTransmitter(
  85. final Socket socket,
  86. int buffersize,
  87. final HttpParams params) throws IOException {
  88. return createSessionOutputBuffer(socket, buffersize, params);
  89. }
  90. /**
  91. * Creates an instance of {@link SocketInputBuffer} to be used for
  92. * receiving data from the given {@link Socket}.
  93. * <p>
  94. * This method can be overridden in a super class in order to provide
  95. * a custom implementation of {@link SessionInputBuffer} interface.
  96. *
  97. * @see SocketInputBuffer#SocketInputBuffer(Socket, int, HttpParams)
  98. *
  99. * @param socket the socket.
  100. * @param buffersize the buffer size.
  101. * @param params HTTP parameters.
  102. * @return session input buffer.
  103. * @throws IOException in case of an I/O error.
  104. */
  105. protected SessionInputBuffer createSessionInputBuffer(
  106. final Socket socket,
  107. int buffersize,
  108. final HttpParams params) throws IOException {
  109. return new SocketInputBuffer(socket, buffersize, params);
  110. }
  111. /**
  112. * Creates an instance of {@link SessionOutputBuffer} to be used for
  113. * sending data to the given {@link Socket}.
  114. * <p>
  115. * This method can be overridden in a super class in order to provide
  116. * a custom implementation of {@link SocketOutputBuffer} interface.
  117. *
  118. * @see SocketOutputBuffer#SocketOutputBuffer(Socket, int, HttpParams)
  119. *
  120. * @param socket the socket.
  121. * @param buffersize the buffer size.
  122. * @param params HTTP parameters.
  123. * @return session output buffer.
  124. * @throws IOException in case of an I/O error.
  125. */
  126. protected SessionOutputBuffer createSessionOutputBuffer(
  127. final Socket socket,
  128. int buffersize,
  129. final HttpParams params) throws IOException {
  130. return new SocketOutputBuffer(socket, buffersize, params);
  131. }
  132. /**
  133. * Binds this connection to the given {@link Socket}. This socket will be
  134. * used by the connection to send and receive data.
  135. * <p>
  136. * This method will invoke {@link #createSessionInputBuffer(Socket, int, HttpParams)}
  137. * and {@link #createSessionOutputBuffer(Socket, int, HttpParams)} methods
  138. * to create session input / output buffers bound to this socket and then
  139. * will invoke {@link #init(SessionInputBuffer, SessionOutputBuffer, HttpParams)}
  140. * method to pass references to those buffers to the underlying HTTP message
  141. * parser and formatter.
  142. * <p>
  143. * After this method's execution the connection status will be reported
  144. * as open and the {@link #isOpen()} will return <code>true</code>.
  145. *
  146. * @param socket the socket.
  147. * @param params HTTP parameters.
  148. * @throws IOException in case of an I/O error.
  149. */
  150. protected void bind(final Socket socket, final HttpParams params) throws IOException {
  151. if (socket == null) {
  152. throw new IllegalArgumentException("Socket may not be null");
  153. }
  154. if (params == null) {
  155. throw new IllegalArgumentException("HTTP parameters may not be null");
  156. }
  157. this.socket = socket;
  158. int buffersize = HttpConnectionParams.getSocketBufferSize(params);
  159. init(
  160. createHttpDataReceiver(socket, buffersize, params),
  161. createHttpDataTransmitter(socket, buffersize, params),
  162. params);
  163. this.open = true;
  164. }
  165. protected Socket getSocket() {
  166. return this.socket;
  167. }
  168. public boolean isOpen() {
  169. return this.open;
  170. }
  171. public InetAddress getLocalAddress() {
  172. if (this.socket != null) {
  173. return this.socket.getLocalAddress();
  174. } else {
  175. return null;
  176. }
  177. }
  178. public int getLocalPort() {
  179. if (this.socket != null) {
  180. return this.socket.getLocalPort();
  181. } else {
  182. return -1;
  183. }
  184. }
  185. public InetAddress getRemoteAddress() {
  186. if (this.socket != null) {
  187. return this.socket.getInetAddress();
  188. } else {
  189. return null;
  190. }
  191. }
  192. public int getRemotePort() {
  193. if (this.socket != null) {
  194. return this.socket.getPort();
  195. } else {
  196. return -1;
  197. }
  198. }
  199. public void setSocketTimeout(int timeout) {
  200. assertOpen();
  201. if (this.socket != null) {
  202. try {
  203. this.socket.setSoTimeout(timeout);
  204. } catch (SocketException ignore) {
  205. // It is not quite clear from the Sun's documentation if there are any
  206. // other legitimate cases for a socket exception to be thrown when setting
  207. // SO_TIMEOUT besides the socket being already closed
  208. }
  209. }
  210. }
  211. public int getSocketTimeout() {
  212. if (this.socket != null) {
  213. try {
  214. return this.socket.getSoTimeout();
  215. } catch (SocketException ignore) {
  216. return -1;
  217. }
  218. } else {
  219. return -1;
  220. }
  221. }
  222. public void shutdown() throws IOException {
  223. this.open = false;
  224. Socket tmpsocket = this.socket;
  225. if (tmpsocket != null) {
  226. tmpsocket.close();
  227. }
  228. }
  229. public void close() throws IOException {
  230. if (!this.open) {
  231. return;
  232. }
  233. this.open = false;
  234. this.open = false;
  235. Socket sock = this.socket;
  236. try {
  237. doFlush();
  238. try {
  239. try {
  240. sock.shutdownOutput();
  241. } catch (IOException ignore) {
  242. }
  243. try {
  244. sock.shutdownInput();
  245. } catch (IOException ignore) {
  246. }
  247. } catch (UnsupportedOperationException ignore) {
  248. // if one isn't supported, the other one isn't either
  249. }
  250. } finally {
  251. sock.close();
  252. }
  253. }
  254. }