olegk 2004/05/08 03:26:44 Modified: httpclient/src/java/org/apache/commons/httpclient HttpMethodDirector.java httpclient/src/java/org/apache/commons/httpclient/params HttpConnectionParams.java HttpMethodParams.java Log: PR #24154 (Setting CONNECTION_TIMEOUT and SO_TIMEOUT on a per-method basis) Changelog: * The value defined at the HTTP connection level sets the initial/default socket timeout value. * The default socket timeout can be overridden at the HTTP method level. The default value is used if socket timeout is undefined * Connection timeout logic unchanged. Setting connection timeout at the HTTP method level does not make sense as the method may end up reusing an already open connection Contributed by Oleg Kalnichevski Reviewed by Michael Becke Revision Changes Path 1.24 +15 -5 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java Index: HttpMethodDirector.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodDirector.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- HttpMethodDirector.java 8 May 2004 10:12:07 -0000 1.23 +++ HttpMethodDirector.java 8 May 2004 10:26:44 -0000 1.24 @@ -45,6 +45,7 @@ import org.apache.commons.httpclient.auth.HttpAuthRealm; import org.apache.commons.httpclient.auth.MalformedChallengeException; import org.apache.commons.httpclient.params.HttpClientParams; +import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.params.HttpParams; import org.apache.commons.logging.Log; @@ -349,8 +350,17 @@ } } } - - this.conn.setSocketTimeout(this.conn.getParams().getSoTimeout()); + int timeout = 0; + // see if a timeout is given for this method + Object param = this.params.getParameter(HttpMethodParams.SO_TIMEOUT); + if (param == null) { + // if not, use the default value + param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT); + } + if (param != null) { + timeout = ((Integer)param).intValue(); + } + this.conn.setSocketTimeout(timeout); try { method.execute(state, this.conn); 1.4 +16 -13 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionParams.java Index: HttpConnectionParams.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionParams.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpConnectionParams.java 18 Apr 2004 23:51:37 -0000 1.3 +++ HttpConnectionParams.java 8 May 2004 10:26:44 -0000 1.4 @@ -43,9 +43,10 @@ public class HttpConnectionParams extends DefaultHttpParams { /** - * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the - * timeout for waiting for data. A timeout value of zero is interpreted as an - * infinite timeout. + * Defines the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the + * timeout for waiting for data. A timeout value of zero is interpreted as an infinite + * timeout. This value is used when no socket timeout is set in the + * [EMAIL PROTECTED] HttpMethodParams HTTP method parameters}. * <p> * This parameter expects a value of type [EMAIL PROTECTED] Integer}. * </p> @@ -123,9 +124,10 @@ } /** - * Returns the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the - * timeout for waiting for data. A timeout value of zero is interpreted as an - * infinite timeout. + * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the + * timeout for waiting for data. A timeout value of zero is interpreted as an infinite + * timeout. This value is used when no socket timeout is set in the + * [EMAIL PROTECTED] HttpMethodParams HTTP method parameters}. * * @return timeout in milliseconds */ @@ -134,9 +136,10 @@ } /** - * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the - * timeout for waiting for data. A timeout value of zero is interpreted as an - * infinite timeout. + * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the + * timeout for waiting for data. A timeout value of zero is interpreted as an infinite + * timeout. This value is used when no socket timeout is set in the + * [EMAIL PROTECTED] HttpMethodParams HTTP method parameters}. * * @param timeout Timeout in milliseconds */ 1.12 +35 -4 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpMethodParams.java Index: HttpMethodParams.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpMethodParams.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- HttpMethodParams.java 18 Apr 2004 23:51:37 -0000 1.11 +++ HttpMethodParams.java 8 May 2004 10:26:44 -0000 1.12 @@ -218,6 +218,16 @@ public static final String STATUS_LINE_GARBAGE_LIMIT = "http.protocol.status-line-garbage-limit"; /** + * Sets the socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds to be used when executing the method. + * A timeout value of zero is interpreted as an infinite timeout. + * <p> + * This parameter expects a value of type [EMAIL PROTECTED] Integer}. + * </p> + * @see java.net.SocketOptions#SO_TIMEOUT + */ + public static final String SO_TIMEOUT = "http.socket.timeout"; + + /** * Creates a new collection of parameters with the collection returned * by [EMAIL PROTECTED] #getDefaultParams()} as a parent. The collection will defer * to its parent for a default value if a particular parameter is not @@ -363,6 +373,27 @@ setParameter(COOKIE_POLICY, policy); } + /** + * Returns the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the + * timeout for waiting for data. A timeout value of zero is interpreted as an infinite + * timeout. + * + * @return timeout in milliseconds + */ + public int getSoTimeout() { + return getIntParameter(SO_TIMEOUT, 0); + } + + /** + * Sets the default socket timeout (<tt>SO_TIMEOUT</tt>) in milliseconds which is the + * timeout for waiting for data. A timeout value of zero is interpreted as an infinite + * timeout. + * + * @param timeout Timeout in milliseconds + */ + public void setSoTimeout(int timeout) { + setIntParameter(SO_TIMEOUT, timeout); + } private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = { UNAMBIGUOUS_STATUS_LINE,
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]