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