Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/params/HttpConnectionParams.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/params/HttpConnectionParams.java $
3    * $Revision: 411109 $
4    * $Date: 2006-06-02 11:36:19 +0200 (Fri, 02 Jun 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 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  
30  package org.apache.http.params;
31  
32  /**
33   * This class implements an adaptor around the {@link HttpParams} interface
34   * to simplify manipulation of the HTTP connection specific parameters.
35   * 
36   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
37   * 
38   * @version $Revision: 411109 $
39   * 
40   * @since 3.0
41   */
42  public final class HttpConnectionParams {
43  
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       * 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      /**
57       * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm 
58       * tries to conserve bandwidth by minimizing the number of segments that are 
59       * sent. When applications wish to decrease network latency and increase 
60       * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY). 
61       * Data will be sent earlier, at the cost of an increase in bandwidth consumption. 
62       * <p>
63       * This parameter expects a value of type {@link Boolean}.
64       * </p>
65       * @see java.net.SocketOptions#TCP_NODELAY
66       */
67      public static final String TCP_NODELAY = "http.tcp.nodelay"; 
68  
69      /**
70       * Determines the size of the internal socket buffer used to buffer data
71       * while receiving / transmitting HTTP messages.
72       * <p>
73       * This parameter expects a value of type {@link Integer}.
74       * </p>
75       */
76      public static final String SOCKET_BUFFER_SIZE = "http.socket.buffer-size"; 
77  
78      /**
79       * Sets SO_LINGER with the specified linger time in seconds. The maximum timeout 
80       * value is platform specific. Value <tt>0</tt> implies that the option is disabled.
81       * Value <tt>-1</tt> implies that the JRE default is used. The setting only affects 
82       * socket close.  
83       * <p>
84       * This parameter expects a value of type {@link Integer}.
85       * </p>
86       * @see java.net.SocketOptions#SO_LINGER
87       */
88      public static final String SO_LINGER = "http.socket.linger"; 
89  
90      /**
91       * Determines the timeout until a connection is etablished. A value of zero 
92       * means the timeout is not used. The default value is zero.
93       * <p>
94       * This parameter expects a value of type {@link Integer}.
95       * </p>
96       */
97      public static final String CONNECTION_TIMEOUT = "http.connection.timeout"; 
98  
99      /**
100      * Determines whether stale connection check is to be used. Disabling 
101      * stale connection check may result in slight performance improvement 
102      * at the risk of getting an I/O error when executing a request over a
103      * connection that has been closed at the server side. 
104      * <p>
105      * This parameter expects a value of type {@link Boolean}.
106      * </p>
107      */
108     public static final String STALE_CONNECTION_CHECK = "http.connection.stalecheck"; 
109 
110     /**
111      * Determines the maximum line length limit. If set to a positive value, any HTTP 
112      * line exceeding this limit will cause an IOException. A negative or zero value
113      * will effectively disable the check.
114      * <p>
115      * This parameter expects a value of type {@link Integer}.
116      * </p>
117      */
118     public static final String MAX_LINE_LENGTH = "http.connection.max-line-length";
119     
120     /**
121      * Determines the maximum HTTP header count allowed. If set to a positive value, 
122      * the number of HTTP headers received from the data stream exceeding this limit 
123      * will cause an IOException. A negative or zero value will effectively disable 
124      * the check. 
125      * <p>
126      * This parameter expects a value of type {@link Integer}.
127      * </p>
128      */
129     public static final String MAX_HEADER_COUNT = "http.connection.max-header-count";
130     
131     /**
132      */
133     private HttpConnectionParams() {
134         super();
135     }
136 
137     /**
138      * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 
139      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 
140      * timeout. This value is used when no socket timeout is set in the 
141      * method parameters. 
142      *
143      * @return timeout in milliseconds
144      */
145     public static int getSoTimeout(final HttpParams params) {
146         if (params == null) {
147             throw new IllegalArgumentException("HTTP parameters may not be null");
148         }
149         return params.getIntParameter(SO_TIMEOUT, 0);
150     }
151 
152     /**
153      * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 
154      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 
155      * timeout. This value is used when no socket timeout is set in the 
156      * method parameters. 
157      *
158      * @param timeout Timeout in milliseconds
159      */
160     public static void setSoTimeout(final HttpParams params, int timeout) {
161         if (params == null) {
162             throw new IllegalArgumentException("HTTP parameters may not be null");
163         }
164         params.setIntParameter(SO_TIMEOUT, timeout);
165         
166     }
167 
168     /**
169      * Tests if Nagle's algorithm is to be used.  
170      *
171      * @return <tt>true</tt> if the Nagle's algorithm is to NOT be used
172      *   (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
173      */
174     public static boolean getTcpNoDelay(final HttpParams params) {
175         if (params == null) {
176             throw new IllegalArgumentException("HTTP parameters may not be null");
177         }
178         return params.getBooleanParameter(TCP_NODELAY, true);
179     }
180 
181     /**
182      * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm 
183      * tries to conserve bandwidth by minimizing the number of segments that are 
184      * sent. When applications wish to decrease network latency and increase 
185      * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY). 
186      * Data will be sent earlier, at the cost of an increase in bandwidth consumption. 
187      *
188      * @param value <tt>true</tt> if the Nagle's algorithm is to NOT be used
189      *   (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
190      */
191     public static void setTcpNoDelay(final HttpParams params, boolean value) {
192         if (params == null) {
193             throw new IllegalArgumentException("HTTP parameters may not be null");
194         }
195         params.setBooleanParameter(TCP_NODELAY, value);
196     }
197 
198     public static int getSocketBufferSize(final HttpParams params) {
199         if (params == null) {
200             throw new IllegalArgumentException("HTTP parameters may not be null");
201         }
202         return params.getIntParameter(SOCKET_BUFFER_SIZE, -1);
203     }
204     
205     public static void setSocketBufferSize(final HttpParams params, int size) {
206         if (params == null) {
207             throw new IllegalArgumentException("HTTP parameters may not be null");
208         }
209         params.setIntParameter(SOCKET_BUFFER_SIZE, size);
210     }
211 
212     /**
213      * Returns linger-on-close timeout. Value <tt>0</tt> implies that the option is 
214      * disabled. Value <tt>-1</tt> implies that the JRE default is used.
215      * 
216      * @return the linger-on-close timeout
217      */
218     public static int getLinger(final HttpParams params) {
219         if (params == null) {
220             throw new IllegalArgumentException("HTTP parameters may not be null");
221         }
222         return params.getIntParameter(SO_LINGER, -1);
223     }
224 
225     /**
226      * Returns linger-on-close timeout. This option disables/enables immediate return 
227      * from a close() of a TCP Socket. Enabling this option with a non-zero Integer 
228      * timeout means that a close() will block pending the transmission and 
229      * acknowledgement of all data written to the peer, at which point the socket is 
230      * closed gracefully. Value <tt>0</tt> implies that the option is 
231      * disabled. Value <tt>-1</tt> implies that the JRE default is used.
232      *
233      * @param value the linger-on-close timeout
234      */
235     public static void setLinger(final HttpParams params, int value) {
236         if (params == null) {
237             throw new IllegalArgumentException("HTTP parameters may not be null");
238         }
239         params.setIntParameter(SO_LINGER, value);
240     }
241 
242     /**
243      * Returns the timeout until a connection is etablished. A value of zero 
244      * means the timeout is not used. The default value is zero.
245      * 
246      * @return timeout in milliseconds.
247      */
248     public static int getConnectionTimeout(final HttpParams params) {
249         if (params == null) {
250             throw new IllegalArgumentException("HTTP parameters may not be null");
251         }
252         return params.getIntParameter(CONNECTION_TIMEOUT, 0);
253     }
254 
255     /**
256      * Sets the timeout until a connection is etablished. A value of zero 
257      * means the timeout is not used. The default value is zero.
258      * 
259      * @param timeout Timeout in milliseconds.
260      */
261     public static void setConnectionTimeout(final HttpParams params, int timeout) {
262         if (params == null) {
263             throw new IllegalArgumentException("HTTP parameters may not be null");
264         }
265         params.setIntParameter(CONNECTION_TIMEOUT, timeout);
266     }
267     
268     /**
269      * Tests whether stale connection check is to be used. Disabling 
270      * stale connection check may result in slight performance improvement 
271      * at the risk of getting an I/O error when executing a request over a
272      * connection that has been closed at the server side. 
273      * 
274      * @return <tt>true</tt> if stale connection check is to be used, 
275      *   <tt>false</tt> otherwise.
276      */
277     public static boolean isStaleCheckingEnabled(final HttpParams params) {
278         if (params == null) {
279             throw new IllegalArgumentException("HTTP parameters may not be null");
280         }
281         return params.getBooleanParameter(STALE_CONNECTION_CHECK, true);
282     }
283 
284     /**
285      * Defines whether stale connection check is to be used. Disabling 
286      * stale connection check may result in slight performance improvement 
287      * at the risk of getting an I/O error when executing a request over a
288      * connection that has been closed at the server side. 
289      * 
290      * @param value <tt>true</tt> if stale connection check is to be used, 
291      *   <tt>false</tt> otherwise.
292      */
293     public static void setStaleCheckingEnabled(final HttpParams params, boolean value) {
294         if (params == null) {
295             throw new IllegalArgumentException("HTTP parameters may not be null");
296         }
297         params.setBooleanParameter(STALE_CONNECTION_CHECK, value);
298     }
299     
300 }