1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionParams.java,v 1.6 2004/09/15 20:32:21 olegk Exp $
  3. * $Revision: 1.6 $
  4. * $Date: 2004/09/15 20:32:21 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.params;
  30. /**
  31. * This class represents a collection of HTTP protocol parameters applicable to
  32. * {@link org.apache.commons.httpclient.HttpConnection HTTP connections}.
  33. * Protocol parameters may be linked together to form a hierarchy. If a particular
  34. * parameter value has not been explicitly defined in the collection itself, its
  35. * value will be drawn from the parent collection of parameters.
  36. *
  37. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  38. *
  39. * @version $Revision: 1.6 $
  40. *
  41. * @since 3.0
  42. */
  43. public class HttpConnectionParams extends DefaultHttpParams {
  44. /**
  45. * Defines the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
  46. * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
  47. * timeout. This value is used when no socket timeout is set in the
  48. * {@link HttpMethodParams HTTP method parameters}.
  49. * <p>
  50. * This parameter expects a value of type {@link Integer}.
  51. * </p>
  52. * @see java.net.SocketOptions#SO_TIMEOUT
  53. */
  54. public static final String SO_TIMEOUT = "http.socket.timeout";
  55. /**
  56. * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm
  57. * tries to conserve bandwidth by minimizing the number of segments that are
  58. * sent. When applications wish to decrease network latency and increase
  59. * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY).
  60. * Data will be sent earlier, at the cost of an increase in bandwidth consumption.
  61. * <p>
  62. * This parameter expects a value of type {@link Boolean}.
  63. * </p>
  64. * @see java.net.SocketOptions#TCP_NODELAY
  65. */
  66. public static final String TCP_NODELAY = "http.tcp.nodelay";
  67. /**
  68. * Determines a hint the size of the underlying buffers used by the platform
  69. * for outgoing network I/O. This value is a suggestion to the kernel from
  70. * the application about the size of buffers to use for the data to be sent
  71. * over the socket.
  72. * <p>
  73. * This parameter expects a value of type {@link Integer}.
  74. * </p>
  75. * @see java.net.SocketOptions#SO_SNDBUF
  76. */
  77. public static final String SO_SNDBUF = "http.socket.sendbuffer";
  78. /**
  79. * Determines a hint the size of the underlying buffers used by the platform
  80. * for incoming network I/O. This value is a suggestion to the kernel from
  81. * the application about the size of buffers to use for the data to be received
  82. * over the socket.
  83. * <p>
  84. * This parameter expects a value of type {@link Integer}.
  85. * </p>
  86. * @see java.net.SocketOptions#SO_RCVBUF
  87. */
  88. public static final String SO_RCVBUF = "http.socket.receivebuffer";
  89. /**
  90. * Sets SO_LINGER with the specified linger time in seconds. The maximum timeout
  91. * value is platform specific. Value <tt>0</tt> implies that the option is disabled.
  92. * Value <tt>-1</tt> implies that the JRE default is used. The setting only affects
  93. * socket close.
  94. * <p>
  95. * This parameter expects a value of type {@link Integer}.
  96. * </p>
  97. * @see java.net.SocketOptions#SO_LINGER
  98. */
  99. public static final String SO_LINGER = "http.socket.linger";
  100. /**
  101. * Determines the timeout until a connection is etablished. A value of zero
  102. * means the timeout is not used. The default value is zero.
  103. * <p>
  104. * This parameter expects a value of type {@link Integer}.
  105. * </p>
  106. */
  107. public static final String CONNECTION_TIMEOUT = "http.connection.timeout";
  108. /**
  109. * Determines whether stale connection check is to be used. Disabling
  110. * stale connection check may result in slight performance improvement
  111. * at the risk of getting an I/O error when executing a request over a
  112. * connection that has been closed at the server side.
  113. * <p>
  114. * This parameter expects a value of type {@link Boolean}.
  115. * </p>
  116. */
  117. public static final String STALE_CONNECTION_CHECK = "http.connection.stalecheck";
  118. /**
  119. * Creates a new collection of parameters with the collection returned
  120. * by {@link #getDefaultParams()} as a parent. The collection will defer
  121. * to its parent for a default value if a particular parameter is not
  122. * explicitly set in the collection itself.
  123. *
  124. * @see #getDefaultParams()
  125. */
  126. public HttpConnectionParams() {
  127. super();
  128. }
  129. /**
  130. * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
  131. * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
  132. * timeout. This value is used when no socket timeout is set in the
  133. * {@link HttpMethodParams HTTP method parameters}.
  134. *
  135. * @return timeout in milliseconds
  136. */
  137. public int getSoTimeout() {
  138. return getIntParameter(SO_TIMEOUT, 0);
  139. }
  140. /**
  141. * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
  142. * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
  143. * timeout. This value is used when no socket timeout is set in the
  144. * {@link HttpMethodParams HTTP method parameters}.
  145. *
  146. * @param timeout Timeout in milliseconds
  147. */
  148. public void setSoTimeout(int timeout) {
  149. setIntParameter(SO_TIMEOUT, timeout);
  150. }
  151. /**
  152. * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm
  153. * tries to conserve bandwidth by minimizing the number of segments that are
  154. * sent. When applications wish to decrease network latency and increase
  155. * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY).
  156. * Data will be sent earlier, at the cost of an increase in bandwidth consumption.
  157. *
  158. * @param value <tt>true</tt> if the Nagle's algorithm is to NOT be used
  159. * (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
  160. */
  161. public void setTcpNoDelay(boolean value) {
  162. setBooleanParameter(TCP_NODELAY, value);
  163. }
  164. /**
  165. * Tests if Nagle's algorithm is to be used.
  166. *
  167. * @return <tt>true</tt> if the Nagle's algorithm is to NOT be used
  168. * (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
  169. */
  170. public boolean getTcpNoDelay() {
  171. return getBooleanParameter(TCP_NODELAY, true);
  172. }
  173. /**
  174. * Returns a hint the size of the underlying buffers used by the platform for
  175. * outgoing network I/O. This value is a suggestion to the kernel from the
  176. * application about the size of buffers to use for the data to be sent over
  177. * the socket.
  178. *
  179. * @return the hint size of the send buffer
  180. */
  181. public int getSendBufferSize() {
  182. return getIntParameter(SO_SNDBUF, -1);
  183. }
  184. /**
  185. * Sets a hint the size of the underlying buffers used by the platform for
  186. * outgoing network I/O. This value is a suggestion to the kernel from the
  187. * application about the size of buffers to use for the data to be sent over
  188. * the socket.
  189. *
  190. * @param size the hint size of the send buffer
  191. */
  192. public void setSendBufferSize(int size) {
  193. setIntParameter(SO_SNDBUF, size);
  194. }
  195. /**
  196. * Returns a hint the size of the underlying buffers used by the platform
  197. * for incoming network I/O. This value is a suggestion to the kernel from
  198. * the application about the size of buffers to use for the data to be received
  199. * over the socket.
  200. *
  201. * @return the hint size of the send buffer
  202. */
  203. public int getReceiveBufferSize() {
  204. return getIntParameter(SO_RCVBUF, -1);
  205. }
  206. /**
  207. * Sets a hint the size of the underlying buffers used by the platform
  208. * for incoming network I/O. This value is a suggestion to the kernel from
  209. * the application about the size of buffers to use for the data to be received
  210. * over the socket.
  211. *
  212. * @param size the hint size of the send buffer
  213. */
  214. public void setReceiveBufferSize(int size) {
  215. setIntParameter(SO_RCVBUF, size);
  216. }
  217. /**
  218. * Returns linger-on-close timeout. Value <tt>0</tt> implies that the option is
  219. * disabled. Value <tt>-1</tt> implies that the JRE default is used.
  220. *
  221. * @return the linger-on-close timeout
  222. */
  223. public int getLinger() {
  224. return getIntParameter(SO_LINGER, -1);
  225. }
  226. /**
  227. * Returns linger-on-close timeout. This option disables/enables immediate return
  228. * from a close() of a TCP Socket. Enabling this option with a non-zero Integer
  229. * timeout means that a close() will block pending the transmission and
  230. * acknowledgement of all data written to the peer, at which point the socket is
  231. * closed gracefully. Value <tt>0</tt> implies that the option is
  232. * disabled. Value <tt>-1</tt> implies that the JRE default is used.
  233. *
  234. * @param value the linger-on-close timeout
  235. */
  236. public void setLinger(int value) {
  237. setIntParameter(SO_LINGER, value);
  238. }
  239. /**
  240. * Returns the timeout until a connection is etablished. A value of zero
  241. * means the timeout is not used. The default value is zero.
  242. *
  243. * @return timeout in milliseconds.
  244. */
  245. public int getConnectionTimeout() {
  246. return getIntParameter(CONNECTION_TIMEOUT, 0);
  247. }
  248. /**
  249. * Sets the timeout until a connection is etablished. A value of zero
  250. * means the timeout is not used. The default value is zero.
  251. *
  252. * @param timeout Timeout in milliseconds.
  253. */
  254. public void setConnectionTimeout(int timeout) {
  255. setIntParameter(CONNECTION_TIMEOUT, timeout);
  256. }
  257. /**
  258. * Tests whether stale connection check is to be used. Disabling
  259. * stale connection check may result in slight performance improvement
  260. * at the risk of getting an I/O error when executing a request over a
  261. * connection that has been closed at the server side.
  262. *
  263. * @return <tt>true</tt> if stale connection check is to be used,
  264. * <tt>false</tt> otherwise.
  265. */
  266. public boolean isStaleCheckingEnabled() {
  267. return getBooleanParameter(STALE_CONNECTION_CHECK, true);
  268. }
  269. /**
  270. * Defines whether stale connection check is to be used. Disabling
  271. * stale connection check may result in slight performance improvement
  272. * at the risk of getting an I/O error when executing a request over a
  273. * connection that has been closed at the server side.
  274. *
  275. * @param value <tt>true</tt> if stale connection check is to be used,
  276. * <tt>false</tt> otherwise.
  277. */
  278. public void setStaleCheckingEnabled(boolean value) {
  279. setBooleanParameter(STALE_CONNECTION_CHECK, value);
  280. }
  281. }