/lib/src/org/apache/http/params/HttpConnectionParams.java

http://github.com/onedanshow/Screen-Courter · Java · 244 lines · 96 code · 19 blank · 129 comment · 28 complexity · 627e47483cd61174093d8c9a8def4f1a 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.params;
  28. /**
  29. * Utility class for accessing connection parameters in {@link HttpParams}.
  30. *
  31. * @since 4.0
  32. */
  33. public final class HttpConnectionParams implements CoreConnectionPNames {
  34. private HttpConnectionParams() {
  35. super();
  36. }
  37. /**
  38. * Obtains value of the {@link CoreConnectionPNames#SO_TIMEOUT} parameter.
  39. * If not set, defaults to <code>0</code>.
  40. *
  41. * @param params HTTP parameters.
  42. * @return SO_TIMEOUT.
  43. */
  44. public static int getSoTimeout(final HttpParams params) {
  45. if (params == null) {
  46. throw new IllegalArgumentException("HTTP parameters may not be null");
  47. }
  48. return params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0);
  49. }
  50. /**
  51. * Sets value of the {@link CoreConnectionPNames#SO_TIMEOUT} parameter.
  52. *
  53. * @param params HTTP parameters.
  54. * @param timeout SO_TIMEOUT.
  55. */
  56. public static void setSoTimeout(final HttpParams params, int timeout) {
  57. if (params == null) {
  58. throw new IllegalArgumentException("HTTP parameters may not be null");
  59. }
  60. params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
  61. }
  62. /**
  63. * Obtains value of the {@link CoreConnectionPNames#SO_REUSEADDR} parameter.
  64. * If not set, defaults to <code>false</code>.
  65. *
  66. * @param params HTTP parameters.
  67. * @return SO_REUSEADDR.
  68. *
  69. * @since 4.1
  70. */
  71. public static boolean getSoReuseaddr(final HttpParams params) {
  72. if (params == null) {
  73. throw new IllegalArgumentException("HTTP parameters may not be null");
  74. }
  75. return params.getBooleanParameter(CoreConnectionPNames.SO_REUSEADDR, false);
  76. }
  77. /**
  78. * Sets value of the {@link CoreConnectionPNames#SO_REUSEADDR} parameter.
  79. *
  80. * @param params HTTP parameters.
  81. * @param reuseaddr SO_REUSEADDR.
  82. *
  83. * @since 4.1
  84. */
  85. public static void setSoReuseaddr(final HttpParams params, boolean reuseaddr) {
  86. if (params == null) {
  87. throw new IllegalArgumentException("HTTP parameters may not be null");
  88. }
  89. params.setBooleanParameter(CoreConnectionPNames.SO_REUSEADDR, reuseaddr);
  90. }
  91. /**
  92. * Obtains value of the {@link CoreConnectionPNames#TCP_NODELAY} parameter.
  93. * If not set, defaults to <code>true</code>.
  94. *
  95. * @param params HTTP parameters.
  96. * @return Nagle's algorithm flag
  97. */
  98. public static boolean getTcpNoDelay(final HttpParams params) {
  99. if (params == null) {
  100. throw new IllegalArgumentException("HTTP parameters may not be null");
  101. }
  102. return params.getBooleanParameter
  103. (CoreConnectionPNames.TCP_NODELAY, true);
  104. }
  105. /**
  106. * Sets value of the {@link CoreConnectionPNames#TCP_NODELAY} parameter.
  107. *
  108. * @param params HTTP parameters.
  109. * @param value Nagle's algorithm flag
  110. */
  111. public static void setTcpNoDelay(final HttpParams params, boolean value) {
  112. if (params == null) {
  113. throw new IllegalArgumentException("HTTP parameters may not be null");
  114. }
  115. params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, value);
  116. }
  117. /**
  118. * Obtains value of the {@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}
  119. * parameter. If not set, defaults to <code>-1</code>.
  120. *
  121. * @param params HTTP parameters.
  122. * @return socket buffer size
  123. */
  124. public static int getSocketBufferSize(final HttpParams params) {
  125. if (params == null) {
  126. throw new IllegalArgumentException("HTTP parameters may not be null");
  127. }
  128. return params.getIntParameter
  129. (CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1);
  130. }
  131. /**
  132. * Sets value of the {@link CoreConnectionPNames#SOCKET_BUFFER_SIZE}
  133. * parameter.
  134. *
  135. * @param params HTTP parameters.
  136. * @param size socket buffer size
  137. */
  138. public static void setSocketBufferSize(final HttpParams params, int size) {
  139. if (params == null) {
  140. throw new IllegalArgumentException("HTTP parameters may not be null");
  141. }
  142. params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, size);
  143. }
  144. /**
  145. * Obtains value of the {@link CoreConnectionPNames#SO_LINGER} parameter.
  146. * If not set, defaults to <code>-1</code>.
  147. *
  148. * @param params HTTP parameters.
  149. * @return SO_LINGER.
  150. */
  151. public static int getLinger(final HttpParams params) {
  152. if (params == null) {
  153. throw new IllegalArgumentException("HTTP parameters may not be null");
  154. }
  155. return params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1);
  156. }
  157. /**
  158. * Sets value of the {@link CoreConnectionPNames#SO_LINGER} parameter.
  159. *
  160. * @param params HTTP parameters.
  161. * @param value SO_LINGER.
  162. */
  163. public static void setLinger(final HttpParams params, int value) {
  164. if (params == null) {
  165. throw new IllegalArgumentException("HTTP parameters may not be null");
  166. }
  167. params.setIntParameter(CoreConnectionPNames.SO_LINGER, value);
  168. }
  169. /**
  170. * Obtains value of the {@link CoreConnectionPNames#CONNECTION_TIMEOUT}
  171. * parameter. If not set, defaults to <code>0</code>.
  172. *
  173. * @param params HTTP parameters.
  174. * @return connect timeout.
  175. */
  176. public static int getConnectionTimeout(final HttpParams params) {
  177. if (params == null) {
  178. throw new IllegalArgumentException("HTTP parameters may not be null");
  179. }
  180. return params.getIntParameter
  181. (CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
  182. }
  183. /**
  184. * Sets value of the {@link CoreConnectionPNames#CONNECTION_TIMEOUT}
  185. * parameter.
  186. *
  187. * @param params HTTP parameters.
  188. * @param timeout connect timeout.
  189. */
  190. public static void setConnectionTimeout(final HttpParams params, int timeout) {
  191. if (params == null) {
  192. throw new IllegalArgumentException("HTTP parameters may not be null");
  193. }
  194. params.setIntParameter
  195. (CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
  196. }
  197. /**
  198. * Obtains value of the {@link CoreConnectionPNames#STALE_CONNECTION_CHECK}
  199. * parameter. If not set, defaults to <code>true</code>.
  200. *
  201. * @param params HTTP parameters.
  202. * @return stale connection check flag.
  203. */
  204. public static boolean isStaleCheckingEnabled(final HttpParams params) {
  205. if (params == null) {
  206. throw new IllegalArgumentException("HTTP parameters may not be null");
  207. }
  208. return params.getBooleanParameter
  209. (CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
  210. }
  211. /**
  212. * Sets value of the {@link CoreConnectionPNames#STALE_CONNECTION_CHECK}
  213. * parameter.
  214. *
  215. * @param params HTTP parameters.
  216. * @param value stale connection check flag.
  217. */
  218. public static void setStaleCheckingEnabled(final HttpParams params, boolean value) {
  219. if (params == null) {
  220. throw new IllegalArgumentException("HTTP parameters may not be null");
  221. }
  222. params.setBooleanParameter
  223. (CoreConnectionPNames.STALE_CONNECTION_CHECK, value);
  224. }
  225. }