Skip to content

Commit d705622

Browse files
committed
SWS-320
1 parent 4fd9b99 commit d705622

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

core/src/main/java/org/springframework/ws/transport/http/CommonsHttpMessageSender.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
import java.io.IOException;
2020
import java.net.URI;
2121

22-
import org.springframework.beans.factory.DisposableBean;
23-
import org.springframework.beans.factory.InitializingBean;
24-
import org.springframework.util.Assert;
25-
import org.springframework.ws.transport.WebServiceConnection;
26-
2722
import org.apache.commons.httpclient.Credentials;
2823
import org.apache.commons.httpclient.HttpClient;
2924
import org.apache.commons.httpclient.HttpConnectionManager;
@@ -33,6 +28,11 @@
3328
import org.apache.commons.httpclient.auth.AuthScope;
3429
import org.apache.commons.httpclient.methods.PostMethod;
3530

31+
import org.springframework.beans.factory.DisposableBean;
32+
import org.springframework.beans.factory.InitializingBean;
33+
import org.springframework.util.Assert;
34+
import org.springframework.ws.transport.WebServiceConnection;
35+
3636
/**
3737
* <code>WebServiceMessageSender</code> implementation that uses <a href="http://jakarta.apache.org/commons/httpclient">Jakarta
3838
* Commons HttpClient</a> to execute POST requests.
@@ -50,6 +50,10 @@
5050
public class CommonsHttpMessageSender extends AbstractHttpWebServiceMessageSender
5151
implements InitializingBean, DisposableBean {
5252

53+
private static final int DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = (60 * 1000);
54+
55+
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
56+
5357
private HttpClient httpClient;
5458

5559
private Credentials credentials;
@@ -62,6 +66,8 @@ public class CommonsHttpMessageSender extends AbstractHttpWebServiceMessageSende
6266
*/
6367
public CommonsHttpMessageSender() {
6468
httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
69+
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
70+
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
6571
}
6672

6773
/**
@@ -99,6 +105,32 @@ public void setCredentials(Credentials credentials) {
99105
this.credentials = credentials;
100106
}
101107

108+
/**
109+
* Sets the timeout until a connection is etablished. A value of 0 means <em>never</em> timeout.
110+
*
111+
* @param timeout the timeout value in milliseconds
112+
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)
113+
*/
114+
public void setConnectionTimeout(int timeout) {
115+
if (timeout < 0) {
116+
throw new IllegalArgumentException("timeout must be a non-negative value");
117+
}
118+
getHttpClient().getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
119+
}
120+
121+
/**
122+
* Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
123+
*
124+
* @param timeout the timeout value in milliseconds
125+
* @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
126+
*/
127+
public void setReadTimeout(int timeout) {
128+
if (timeout < 0) {
129+
throw new IllegalArgumentException("timeout must be a non-negative value");
130+
}
131+
getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout);
132+
}
133+
102134
/**
103135
* Returns the authentication scope to be used. Only used when the <code>credentials</code> property has been set.
104136
* <p/>

core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,32 @@
1616

1717
package org.springframework.ws.transport.http;
1818

19+
import java.net.URI;
20+
21+
import org.apache.commons.httpclient.ConnectTimeoutException;
22+
23+
import org.springframework.ws.MockWebServiceMessage;
24+
import org.springframework.ws.WebServiceMessage;
25+
import org.springframework.ws.transport.WebServiceConnection;
26+
1927
public class CommonsHttpMessageSenderIntegrationTest extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
2028

2129
protected AbstractHttpWebServiceMessageSender createMessageSender() {
2230
return new CommonsHttpMessageSender();
2331
}
2432

33+
public void testConnectionTimeout() throws Exception {
34+
CommonsHttpMessageSender messageSender = new CommonsHttpMessageSender();
35+
messageSender.setConnectionTimeout(1);
36+
WebServiceConnection connection = messageSender.createConnection(new URI("http://example.com/"));
37+
WebServiceMessage message = new MockWebServiceMessage();
38+
try {
39+
connection.send(message);
40+
fail("ConnectTimeoutException expected");
41+
}
42+
catch (ConnectTimeoutException ex) {
43+
// expected
44+
}
45+
}
46+
2547
}

0 commit comments

Comments
 (0)