Skip to content

Commit a2129a1

Browse files
committed
consistent "connectTimeout" and "readTimeout" properties (SPR-8615)
1 parent 0f1c10b commit a2129a1

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,24 @@ public HttpClient getHttpClient() {
8888
}
8989

9090
/**
91-
* Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
91+
* Set the connection timeout for the underlying HttpClient.
92+
* A timeout value of 0 specifies an infinite timeout.
93+
* @param timeout the timeout value in milliseconds
94+
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)
95+
*/
96+
public void setConnectTimeout(int timeout) {
97+
Assert.isTrue(timeout < 0, "Timeout must be a non-negative value");
98+
this.httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
99+
}
100+
101+
/**
102+
* Set the socket read timeout for the underlying HttpClient.
103+
* A timeout value of 0 specifies an infinite timeout.
92104
* @param timeout the timeout value in milliseconds
93105
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
94106
*/
95107
public void setReadTimeout(int timeout) {
96-
if (timeout < 0) {
97-
throw new IllegalArgumentException("timeout must be a non-negative value");
98-
}
108+
Assert.isTrue(timeout < 0, "Timeout must be a non-negative value");
99109
getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout);
100110
}
101111

org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/CommonsHttpInvokerRequestExecutor.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
import org.springframework.context.i18n.LocaleContext;
3232
import org.springframework.context.i18n.LocaleContextHolder;
3333
import org.springframework.remoting.support.RemoteInvocationResult;
34+
import org.springframework.util.Assert;
3435
import org.springframework.util.StringUtils;
3536

3637
/**
@@ -66,7 +67,7 @@ public class CommonsHttpInvokerRequestExecutor extends AbstractHttpInvokerReques
6667
*/
6768
public CommonsHttpInvokerRequestExecutor() {
6869
this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
69-
this.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
70+
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
7071
}
7172

7273
/**
@@ -95,16 +96,25 @@ public HttpClient getHttpClient() {
9596
}
9697

9798
/**
98-
* Set the socket read timeout for the underlying HttpClient. A value
99-
* of 0 means <emphasis>never</emphasis> timeout.
99+
* Set the connection timeout for the underlying HttpClient.
100+
* A timeout value of 0 specifies an infinite timeout.
101+
* @param timeout the timeout value in milliseconds
102+
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)
103+
*/
104+
public void setConnectTimeout(int timeout) {
105+
Assert.isTrue(timeout < 0, "Timeout must be a non-negative value");
106+
this.httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
107+
}
108+
109+
/**
110+
* Set the socket read timeout for the underlying HttpClient.
111+
* A timeout value of 0 specifies an infinite timeout.
100112
* @param timeout the timeout value in milliseconds
101113
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
102114
* @see #DEFAULT_READ_TIMEOUT_MILLISECONDS
103115
*/
104116
public void setReadTimeout(int timeout) {
105-
if (timeout < 0) {
106-
throw new IllegalArgumentException("timeout must be a non-negative value");
107-
}
117+
Assert.isTrue(timeout < 0, "Timeout must be a non-negative value");
108118
this.httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
109119
}
110120

org.springframework.web/src/main/java/org/springframework/remoting/httpinvoker/SimpleHttpInvokerRequestExecutor.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,32 @@
4545
*/
4646
public class SimpleHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor {
4747

48+
private int connectTimeout = -1;
49+
50+
private int readTimeout = -1;
51+
52+
53+
/**
54+
* Set the underlying URLConnection's connect timeout (in milliseconds).
55+
* A timeout value of 0 specifies an infinite timeout.
56+
* <p>Default is the system's default timeout.
57+
* @see URLConnection#setConnectTimeout(int)
58+
*/
59+
public void setConnectTimeout(int connectTimeout) {
60+
this.connectTimeout = connectTimeout;
61+
}
62+
63+
/**
64+
* Set the underlying URLConnection's read timeout (in milliseconds).
65+
* A timeout value of 0 specifies an infinite timeout.
66+
* <p>Default is the system's default timeout.
67+
* @see URLConnection#setReadTimeout(int)
68+
*/
69+
public void setReadTimeout(int readTimeout) {
70+
this.readTimeout = readTimeout;
71+
}
72+
73+
4874
/**
4975
* Execute the given request through a standard J2SE HttpURLConnection.
5076
* <p>This method implements the basic processing workflow:
@@ -90,23 +116,29 @@ protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config
90116
* <p>The default implementation specifies POST as method,
91117
* "application/x-java-serialized-object" as "Content-Type" header,
92118
* and the given content length as "Content-Length" header.
93-
* @param con the HTTP connection to prepare
119+
* @param connection the HTTP connection to prepare
94120
* @param contentLength the length of the content to send
95121
* @throws IOException if thrown by HttpURLConnection methods
96122
* @see java.net.HttpURLConnection#setRequestMethod
97123
* @see java.net.HttpURLConnection#setRequestProperty
98124
*/
99-
protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {
100-
con.setDoOutput(true);
101-
con.setRequestMethod(HTTP_METHOD_POST);
102-
con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
103-
con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));
125+
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
126+
if (this.connectTimeout >= 0) {
127+
connection.setConnectTimeout(this.connectTimeout);
128+
}
129+
if (this.readTimeout >= 0) {
130+
connection.setReadTimeout(this.readTimeout);
131+
}
132+
connection.setDoOutput(true);
133+
connection.setRequestMethod(HTTP_METHOD_POST);
134+
connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
135+
connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));
104136
LocaleContext locale = LocaleContextHolder.getLocaleContext();
105137
if (locale != null) {
106-
con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));
138+
connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));
107139
}
108140
if (isAcceptGzipEncoding()) {
109-
con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
141+
connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
110142
}
111143
}
112144

@@ -187,7 +219,7 @@ protected InputStream readResponseBody(HttpInvokerClientConfiguration config, Ht
187219
*/
188220
protected boolean isGzipResponse(HttpURLConnection con) {
189221
String encodingHeader = con.getHeaderField(HTTP_HEADER_CONTENT_ENCODING);
190-
return (encodingHeader != null && encodingHeader.toLowerCase().indexOf(ENCODING_GZIP) != -1);
222+
return (encodingHeader != null && encodingHeader.toLowerCase().contains(ENCODING_GZIP));
191223
}
192224

193225
}

0 commit comments

Comments
 (0)