001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/params/HttpConnectionParams.java $
003: * $Revision: 576089 $
004: * $Date: 2007-09-16 14:39:56 +0200 (Sun, 16 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.params;
033:
034: /**
035: * An adaptor for accessing connection parameters in {@link HttpParams}.
036: * <br/>
037: * Note that the <i>implements</i> relation to {@link CoreConnectionPNames}
038: * is for compatibility with existing application code only. References to
039: * the parameter names should use the interface, not this class.
040: *
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: *
043: * @version $Revision: 576089 $
044: *
045: * @since 4.0
046: */
047: public final class HttpConnectionParams implements CoreConnectionPNames {
048:
049: /**
050: */
051: private HttpConnectionParams() {
052: super ();
053: }
054:
055: /**
056: * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
057: * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
058: * timeout. This value is used when no socket timeout is set in the
059: * method parameters.
060: *
061: * @return timeout in milliseconds
062: */
063: public static int getSoTimeout(final HttpParams params) {
064: if (params == null) {
065: throw new IllegalArgumentException(
066: "HTTP parameters may not be null");
067: }
068: return params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT,
069: 0);
070: }
071:
072: /**
073: * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the
074: * timeout for waiting for data. A timeout value of zero is interpreted as an infinite
075: * timeout. This value is used when no socket timeout is set in the
076: * method parameters.
077: *
078: * @param timeout Timeout in milliseconds
079: */
080: public static void setSoTimeout(final HttpParams params, int timeout) {
081: if (params == null) {
082: throw new IllegalArgumentException(
083: "HTTP parameters may not be null");
084: }
085: params
086: .setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
087: timeout);
088:
089: }
090:
091: /**
092: * Tests if Nagle's algorithm is to be used.
093: *
094: * @return <tt>true</tt> if the Nagle's algorithm is to NOT be used
095: * (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
096: */
097: public static boolean getTcpNoDelay(final HttpParams params) {
098: if (params == null) {
099: throw new IllegalArgumentException(
100: "HTTP parameters may not be null");
101: }
102: return params.getBooleanParameter(
103: CoreConnectionPNames.TCP_NODELAY, true);
104: }
105:
106: /**
107: * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm
108: * tries to conserve bandwidth by minimizing the number of segments that are
109: * sent. When applications wish to decrease network latency and increase
110: * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY).
111: * Data will be sent earlier, at the cost of an increase in bandwidth consumption.
112: *
113: * @param value <tt>true</tt> if the Nagle's algorithm is to NOT be used
114: * (that is enable TCP_NODELAY), <tt>false</tt> otherwise.
115: */
116: public static void setTcpNoDelay(final HttpParams params,
117: boolean value) {
118: if (params == null) {
119: throw new IllegalArgumentException(
120: "HTTP parameters may not be null");
121: }
122: params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY,
123: value);
124: }
125:
126: public static int getSocketBufferSize(final HttpParams params) {
127: if (params == null) {
128: throw new IllegalArgumentException(
129: "HTTP parameters may not be null");
130: }
131: return params.getIntParameter(
132: CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1);
133: }
134:
135: public static void setSocketBufferSize(final HttpParams params,
136: int size) {
137: if (params == null) {
138: throw new IllegalArgumentException(
139: "HTTP parameters may not be null");
140: }
141: params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
142: size);
143: }
144:
145: /**
146: * Returns linger-on-close timeout. Value <tt>0</tt> implies that the option is
147: * disabled. Value <tt>-1</tt> implies that the JRE default is used.
148: *
149: * @return the linger-on-close timeout
150: */
151: public static int getLinger(final HttpParams params) {
152: if (params == null) {
153: throw new IllegalArgumentException(
154: "HTTP parameters may not be null");
155: }
156: return params.getIntParameter(CoreConnectionPNames.SO_LINGER,
157: -1);
158: }
159:
160: /**
161: * Returns linger-on-close timeout. This option disables/enables immediate return
162: * from a close() of a TCP Socket. Enabling this option with a non-zero Integer
163: * timeout means that a close() will block pending the transmission and
164: * acknowledgement of all data written to the peer, at which point the socket is
165: * closed gracefully. Value <tt>0</tt> implies that the option is
166: * disabled. Value <tt>-1</tt> implies that the JRE default is used.
167: *
168: * @param value the linger-on-close timeout
169: */
170: public static void setLinger(final HttpParams params, int value) {
171: if (params == null) {
172: throw new IllegalArgumentException(
173: "HTTP parameters may not be null");
174: }
175: params.setIntParameter(CoreConnectionPNames.SO_LINGER, value);
176: }
177:
178: /**
179: * Returns the timeout until a connection is etablished. A value of zero
180: * means the timeout is not used. The default value is zero.
181: *
182: * @return timeout in milliseconds.
183: */
184: public static int getConnectionTimeout(final HttpParams params) {
185: if (params == null) {
186: throw new IllegalArgumentException(
187: "HTTP parameters may not be null");
188: }
189: return params.getIntParameter(
190: CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
191: }
192:
193: /**
194: * Sets the timeout until a connection is etablished. A value of zero
195: * means the timeout is not used. The default value is zero.
196: *
197: * @param timeout Timeout in milliseconds.
198: */
199: public static void setConnectionTimeout(final HttpParams params,
200: int timeout) {
201: if (params == null) {
202: throw new IllegalArgumentException(
203: "HTTP parameters may not be null");
204: }
205: params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
206: timeout);
207: }
208:
209: /**
210: * Tests whether stale connection check is to be used. Disabling
211: * stale connection check may result in slight performance improvement
212: * at the risk of getting an I/O error when executing a request over a
213: * connection that has been closed at the server side.
214: *
215: * @return <tt>true</tt> if stale connection check is to be used,
216: * <tt>false</tt> otherwise.
217: */
218: public static boolean isStaleCheckingEnabled(final HttpParams params) {
219: if (params == null) {
220: throw new IllegalArgumentException(
221: "HTTP parameters may not be null");
222: }
223: return params.getBooleanParameter(
224: CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
225: }
226:
227: /**
228: * Defines whether stale connection check is to be used. Disabling
229: * stale connection check may result in slight performance improvement
230: * at the risk of getting an I/O error when executing a request over a
231: * connection that has been closed at the server side.
232: *
233: * @param value <tt>true</tt> if stale connection check is to be used,
234: * <tt>false</tt> otherwise.
235: */
236: public static void setStaleCheckingEnabled(final HttpParams params,
237: boolean value) {
238: if (params == null) {
239: throw new IllegalArgumentException(
240: "HTTP parameters may not be null");
241: }
242: params.setBooleanParameter(
243: CoreConnectionPNames.STALE_CONNECTION_CHECK, value);
244: }
245:
246: }
|