From a2129a132fe28c4b0980c587bef32004c8e41a3d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 15 Aug 2011 20:57:14 +0000 Subject: [PATCH] consistent "connectTimeout" and "readTimeout" properties (SPR-8615) --- .../CommonsClientHttpRequestFactory.java | 18 +++++-- .../CommonsHttpInvokerRequestExecutor.java | 24 ++++++--- .../SimpleHttpInvokerRequestExecutor.java | 52 +++++++++++++++---- 3 files changed, 73 insertions(+), 21 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java index 056ddea32887..dc1f3b807000 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java @@ -88,14 +88,24 @@ public HttpClient getHttpClient() { } /** - * Set the socket read timeout for the underlying HttpClient. A value of 0 means never timeout. + * Set the connection timeout for the underlying HttpClient. + * A timeout value of 0 specifies an infinite timeout. + * @param timeout the timeout value in milliseconds + * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int) + */ + public void setConnectTimeout(int timeout) { + Assert.isTrue(timeout < 0, "Timeout must be a non-negative value"); + this.httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); + } + + /** + * Set the socket read timeout for the underlying HttpClient. + * A timeout value of 0 specifies an infinite timeout. * @param timeout the timeout value in milliseconds * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int) */ public void setReadTimeout(int timeout) { - if (timeout < 0) { - throw new IllegalArgumentException("timeout must be a non-negative value"); - } + Assert.isTrue(timeout < 0, "Timeout must be a non-negative value"); getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout); } diff --git a/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/CommonsHttpInvokerRequestExecutor.java b/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/CommonsHttpInvokerRequestExecutor.java index 173ad2faa887..ad841afa887b 100644 --- a/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/CommonsHttpInvokerRequestExecutor.java +++ b/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/CommonsHttpInvokerRequestExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.context.i18n.LocaleContext; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.remoting.support.RemoteInvocationResult; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -66,7 +67,7 @@ public class CommonsHttpInvokerRequestExecutor extends AbstractHttpInvokerReques */ public CommonsHttpInvokerRequestExecutor() { this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); - this.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); + setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); } /** @@ -95,16 +96,25 @@ public HttpClient getHttpClient() { } /** - * Set the socket read timeout for the underlying HttpClient. A value - * of 0 means never timeout. + * Set the connection timeout for the underlying HttpClient. + * A timeout value of 0 specifies an infinite timeout. + * @param timeout the timeout value in milliseconds + * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int) + */ + public void setConnectTimeout(int timeout) { + Assert.isTrue(timeout < 0, "Timeout must be a non-negative value"); + this.httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); + } + + /** + * Set the socket read timeout for the underlying HttpClient. + * A timeout value of 0 specifies an infinite timeout. * @param timeout the timeout value in milliseconds * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int) * @see #DEFAULT_READ_TIMEOUT_MILLISECONDS */ public void setReadTimeout(int timeout) { - if (timeout < 0) { - throw new IllegalArgumentException("timeout must be a non-negative value"); - } + Assert.isTrue(timeout < 0, "Timeout must be a non-negative value"); this.httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout); } diff --git a/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/SimpleHttpInvokerRequestExecutor.java b/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/SimpleHttpInvokerRequestExecutor.java index 4f13e0de6c91..216b6d811293 100644 --- a/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/SimpleHttpInvokerRequestExecutor.java +++ b/org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/SimpleHttpInvokerRequestExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,32 @@ */ public class SimpleHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor { + private int connectTimeout = -1; + + private int readTimeout = -1; + + + /** + * Set the underlying URLConnection's connect timeout (in milliseconds). + * A timeout value of 0 specifies an infinite timeout. + *

Default is the system's default timeout. + * @see URLConnection#setConnectTimeout(int) + */ + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + /** + * Set the underlying URLConnection's read timeout (in milliseconds). + * A timeout value of 0 specifies an infinite timeout. + *

Default is the system's default timeout. + * @see URLConnection#setReadTimeout(int) + */ + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + + /** * Execute the given request through a standard J2SE HttpURLConnection. *

This method implements the basic processing workflow: @@ -90,23 +116,29 @@ protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config *

The default implementation specifies POST as method, * "application/x-java-serialized-object" as "Content-Type" header, * and the given content length as "Content-Length" header. - * @param con the HTTP connection to prepare + * @param connection the HTTP connection to prepare * @param contentLength the length of the content to send * @throws IOException if thrown by HttpURLConnection methods * @see java.net.HttpURLConnection#setRequestMethod * @see java.net.HttpURLConnection#setRequestProperty */ - protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException { - con.setDoOutput(true); - con.setRequestMethod(HTTP_METHOD_POST); - con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType()); - con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); + protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException { + if (this.connectTimeout >= 0) { + connection.setConnectTimeout(this.connectTimeout); + } + if (this.readTimeout >= 0) { + connection.setReadTimeout(this.readTimeout); + } + connection.setDoOutput(true); + connection.setRequestMethod(HTTP_METHOD_POST); + connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType()); + connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); LocaleContext locale = LocaleContextHolder.getLocaleContext(); if (locale != null) { - con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale())); + connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale())); } if (isAcceptGzipEncoding()) { - con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); + connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } } @@ -187,7 +219,7 @@ protected InputStream readResponseBody(HttpInvokerClientConfiguration config, Ht */ protected boolean isGzipResponse(HttpURLConnection con) { String encodingHeader = con.getHeaderField(HTTP_HEADER_CONTENT_ENCODING); - return (encodingHeader != null && encodingHeader.toLowerCase().indexOf(ENCODING_GZIP) != -1); + return (encodingHeader != null && encodingHeader.toLowerCase().contains(ENCODING_GZIP)); } }