1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/params/HttpConnectionParams.java $
3  * $Revision: 576089 $
4  * $Date: 2007-09-16 05:39:56 -0700 (Sun, 16 Sep 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.params;
33 
34 /**
35  * An adaptor for accessing connection parameters in {@link HttpParams}.
36  * <br/>
37  * Note that the <i>implements</i> relation to {@link CoreConnectionPNames}
38  * is for compatibility with existing application code only. References to
39  * the parameter names should use the interface, not this class.
40  *
41  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
42  *
43  * @version $Revision: 576089 $
44  *
45  * @since 4.0
46  *
47  * @deprecated Please use {@link java.net.URL#openConnection} instead.
48  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
49  *     for further details.
50  */
51 @Deprecated
52 public final class HttpConnectionParams implements CoreConnectionPNames {
53 
54     /**
55      */
HttpConnectionParams()56     private HttpConnectionParams() {
57         super();
58     }
59 
60     /**
61      * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
62      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
63      * timeout. This value is used when no socket timeout is set in the
64      * method parameters.
65      *
66      * @return timeout in milliseconds
67      */
getSoTimeout(final HttpParams params)68     public static int getSoTimeout(final HttpParams params) {
69         if (params == null) {
70             throw new IllegalArgumentException("HTTP parameters may not be null");
71         }
72         return params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0);
73     }
74 
75     /**
76      * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
77      * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
78      * timeout. This value is used when no socket timeout is set in the
79      * method parameters.
80      *
81      * @param timeout Timeout in milliseconds
82      */
setSoTimeout(final HttpParams params, int timeout)83     public static void setSoTimeout(final HttpParams params, int timeout) {
84         if (params == null) {
85             throw new IllegalArgumentException("HTTP parameters may not be null");
86         }
87         params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
88 
89     }
90 
91     /**
92      * Tests if Nagle's algorithm is to be used.
93      *
94      * @return <tt>true</tt> if the Nagle's algorithm is to NOT be used
95      *   (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
96      */
getTcpNoDelay(final HttpParams params)97     public static boolean getTcpNoDelay(final HttpParams params) {
98         if (params == null) {
99             throw new IllegalArgumentException("HTTP parameters may not be null");
100         }
101         return params.getBooleanParameter
102             (CoreConnectionPNames.TCP_NODELAY, true);
103     }
104 
105     /**
106      * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm
107      * tries to conserve bandwidth by minimizing the number of segments that are
108      * sent. When applications wish to decrease network latency and increase
109      * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY).
110      * Data will be sent earlier, at the cost of an increase in bandwidth consumption.
111      *
112      * @param value <tt>true</tt> if the Nagle's algorithm is to NOT be used
113      *   (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
114      */
setTcpNoDelay(final HttpParams params, boolean value)115     public static void setTcpNoDelay(final HttpParams params, boolean value) {
116         if (params == null) {
117             throw new IllegalArgumentException("HTTP parameters may not be null");
118         }
119         params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, value);
120     }
121 
getSocketBufferSize(final HttpParams params)122     public static int getSocketBufferSize(final HttpParams params) {
123         if (params == null) {
124             throw new IllegalArgumentException("HTTP parameters may not be null");
125         }
126         return params.getIntParameter
127             (CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1);
128     }
129 
setSocketBufferSize(final HttpParams params, int size)130     public static void setSocketBufferSize(final HttpParams params, int size) {
131         if (params == null) {
132             throw new IllegalArgumentException("HTTP parameters may not be null");
133         }
134         params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, size);
135     }
136 
137     /**
138      * Returns linger-on-close timeout. Value <tt>0</tt> implies that the option is
139      * disabled. Value <tt>-1</tt> implies that the JRE default is used.
140      *
141      * @return the linger-on-close timeout
142      */
getLinger(final HttpParams params)143     public static int getLinger(final HttpParams params) {
144         if (params == null) {
145             throw new IllegalArgumentException("HTTP parameters may not be null");
146         }
147         return params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1);
148     }
149 
150     /**
151      * Returns linger-on-close timeout. This option disables/enables immediate return
152      * from a close() of a TCP Socket. Enabling this option with a non-zero Integer
153      * timeout means that a close() will block pending the transmission and
154      * acknowledgement of all data written to the peer, at which point the socket is
155      * closed gracefully. Value <tt>0</tt> implies that the option is
156      * disabled. Value <tt>-1</tt> implies that the JRE default is used.
157      *
158      * @param value the linger-on-close timeout
159      */
setLinger(final HttpParams params, int value)160     public static void setLinger(final HttpParams params, int value) {
161         if (params == null) {
162             throw new IllegalArgumentException("HTTP parameters may not be null");
163         }
164         params.setIntParameter(CoreConnectionPNames.SO_LINGER, value);
165     }
166 
167     /**
168      * Returns the timeout until a connection is etablished. A value of zero
169      * means the timeout is not used. The default value is zero.
170      *
171      * @return timeout in milliseconds.
172      */
getConnectionTimeout(final HttpParams params)173     public static int getConnectionTimeout(final HttpParams params) {
174         if (params == null) {
175             throw new IllegalArgumentException("HTTP parameters may not be null");
176         }
177         return params.getIntParameter
178             (CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
179     }
180 
181     /**
182      * Sets the timeout until a connection is etablished. A value of zero
183      * means the timeout is not used. The default value is zero.
184      *
185      * @param timeout Timeout in milliseconds.
186      */
setConnectionTimeout(final HttpParams params, int timeout)187     public static void setConnectionTimeout(final HttpParams params, int timeout) {
188         if (params == null) {
189             throw new IllegalArgumentException("HTTP parameters may not be null");
190         }
191         params.setIntParameter
192             (CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
193     }
194 
195     /**
196      * Tests whether stale connection check is to be used. Disabling
197      * stale connection check may result in slight performance improvement
198      * at the risk of getting an I/O error when executing a request over a
199      * connection that has been closed at the server side.
200      *
201      * @return <tt>true</tt> if stale connection check is to be used,
202      *   <tt>false</tt> otherwise.
203      */
isStaleCheckingEnabled(final HttpParams params)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     /**
213      * Defines whether stale connection check is to be used. Disabling
214      * stale connection check may result in slight performance improvement
215      * at the risk of getting an I/O error when executing a request over a
216      * connection that has been closed at the server side.
217      *
218      * @param value <tt>true</tt> if stale connection check is to be used,
219      *   <tt>false</tt> otherwise.
220      */
setStaleCheckingEnabled(final HttpParams params, boolean value)221     public static void setStaleCheckingEnabled(final HttpParams params, boolean value) {
222         if (params == null) {
223             throw new IllegalArgumentException("HTTP parameters may not be null");
224         }
225         params.setBooleanParameter
226             (CoreConnectionPNames.STALE_CONNECTION_CHECK, value);
227     }
228 
229 }
230