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 public final class HttpConnectionParams implements CoreConnectionPNames { 48 49 /** 50 */ 51 private HttpConnectionParams() { 52 super(); 53 } 54 55 /** 56 * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 57 * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 58 * timeout. This value is used when no socket timeout is set in the 59 * method parameters. 60 * 61 * @return timeout in milliseconds 62 */ 63 public static int getSoTimeout(final HttpParams params) { 64 if (params == null) { 65 throw new IllegalArgumentException("HTTP parameters may not be null"); 66 } 67 return params.getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0); 68 } 69 70 /** 71 * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the 72 * timeout for waiting for data. A timeout value of zero is interpreted as an infinite 73 * timeout. This value is used when no socket timeout is set in the 74 * method parameters. 75 * 76 * @param timeout Timeout in milliseconds 77 */ 78 public static void setSoTimeout(final HttpParams params, int timeout) { 79 if (params == null) { 80 throw new IllegalArgumentException("HTTP parameters may not be null"); 81 } 82 params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout); 83 84 } 85 86 /** 87 * Tests if Nagle's algorithm is to be used. 88 * 89 * @return <tt>true</tt> if the Nagle's algorithm is to NOT be used 90 * (that is enable TCP_NODELAY), <tt>false</tt> otherwise. 91 */ 92 public static boolean getTcpNoDelay(final HttpParams params) { 93 if (params == null) { 94 throw new IllegalArgumentException("HTTP parameters may not be null"); 95 } 96 return params.getBooleanParameter 97 (CoreConnectionPNames.TCP_NODELAY, true); 98 } 99 100 /** 101 * Determines whether Nagle's algorithm is to be used. The Nagle's algorithm 102 * tries to conserve bandwidth by minimizing the number of segments that are 103 * sent. When applications wish to decrease network latency and increase 104 * performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY). 105 * Data will be sent earlier, at the cost of an increase in bandwidth consumption. 106 * 107 * @param value <tt>true</tt> if the Nagle's algorithm is to NOT be used 108 * (that is enable TCP_NODELAY), <tt>false</tt> otherwise. 109 */ 110 public static void setTcpNoDelay(final HttpParams params, boolean value) { 111 if (params == null) { 112 throw new IllegalArgumentException("HTTP parameters may not be null"); 113 } 114 params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, value); 115 } 116 117 public static int getSocketBufferSize(final HttpParams params) { 118 if (params == null) { 119 throw new IllegalArgumentException("HTTP parameters may not be null"); 120 } 121 return params.getIntParameter 122 (CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1); 123 } 124 125 public static void setSocketBufferSize(final HttpParams params, int size) { 126 if (params == null) { 127 throw new IllegalArgumentException("HTTP parameters may not be null"); 128 } 129 params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, size); 130 } 131 132 /** 133 * Returns linger-on-close timeout. Value <tt>0</tt> implies that the option is 134 * disabled. Value <tt>-1</tt> implies that the JRE default is used. 135 * 136 * @return the linger-on-close timeout 137 */ 138 public static int getLinger(final HttpParams params) { 139 if (params == null) { 140 throw new IllegalArgumentException("HTTP parameters may not be null"); 141 } 142 return params.getIntParameter(CoreConnectionPNames.SO_LINGER, -1); 143 } 144 145 /** 146 * Returns linger-on-close timeout. This option disables/enables immediate return 147 * from a close() of a TCP Socket. Enabling this option with a non-zero Integer 148 * timeout means that a close() will block pending the transmission and 149 * acknowledgement of all data written to the peer, at which point the socket is 150 * closed gracefully. Value <tt>0</tt> implies that the option is 151 * disabled. Value <tt>-1</tt> implies that the JRE default is used. 152 * 153 * @param value the linger-on-close timeout 154 */ 155 public static void setLinger(final HttpParams params, int value) { 156 if (params == null) { 157 throw new IllegalArgumentException("HTTP parameters may not be null"); 158 } 159 params.setIntParameter(CoreConnectionPNames.SO_LINGER, value); 160 } 161 162 /** 163 * Returns the timeout until a connection is etablished. A value of zero 164 * means the timeout is not used. The default value is zero. 165 * 166 * @return timeout in milliseconds. 167 */ 168 public static int getConnectionTimeout(final HttpParams params) { 169 if (params == null) { 170 throw new IllegalArgumentException("HTTP parameters may not be null"); 171 } 172 return params.getIntParameter 173 (CoreConnectionPNames.CONNECTION_TIMEOUT, 0); 174 } 175 176 /** 177 * Sets the timeout until a connection is etablished. A value of zero 178 * means the timeout is not used. The default value is zero. 179 * 180 * @param timeout Timeout in milliseconds. 181 */ 182 public static void setConnectionTimeout(final HttpParams params, int timeout) { 183 if (params == null) { 184 throw new IllegalArgumentException("HTTP parameters may not be null"); 185 } 186 params.setIntParameter 187 (CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 188 } 189 190 /** 191 * Tests whether stale connection check is to be used. Disabling 192 * stale connection check may result in slight performance improvement 193 * at the risk of getting an I/O error when executing a request over a 194 * connection that has been closed at the server side. 195 * 196 * @return <tt>true</tt> if stale connection check is to be used, 197 * <tt>false</tt> otherwise. 198 */ 199 public static boolean isStaleCheckingEnabled(final HttpParams params) { 200 if (params == null) { 201 throw new IllegalArgumentException("HTTP parameters may not be null"); 202 } 203 return params.getBooleanParameter 204 (CoreConnectionPNames.STALE_CONNECTION_CHECK, true); 205 } 206 207 /** 208 * Defines whether stale connection check is to be used. Disabling 209 * stale connection check may result in slight performance improvement 210 * at the risk of getting an I/O error when executing a request over a 211 * connection that has been closed at the server side. 212 * 213 * @param value <tt>true</tt> if stale connection check is to be used, 214 * <tt>false</tt> otherwise. 215 */ 216 public static void setStaleCheckingEnabled(final HttpParams params, boolean value) { 217 if (params == null) { 218 throw new IllegalArgumentException("HTTP parameters may not be null"); 219 } 220 params.setBooleanParameter 221 (CoreConnectionPNames.STALE_CONNECTION_CHECK, value); 222 } 223 224 } 225