Skip to content

Commit 39d0895

Browse files
kazuki43zoogregturn
authored andcommitted
SWS-1014 - Support custom timeout settings on HttpUrlConnectionMessageSender.
1 parent f7803a5 commit 39d0895

File tree

8 files changed

+77
-18
lines changed

8 files changed

+77
-18
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787

8888
<activemq.version>4.1.2</activemq.version>
8989
<aspectj.version>1.8.13</aspectj.version>
90+
<assertj.version>3.9.0</assertj.version>
9091
<axiom.version>1.2.20</axiom.version>
9192
<commons-httpclient.version>3.1</commons-httpclient.version>
9293
<commons-io.version>2.5</commons-io.version>
@@ -154,6 +155,12 @@
154155
<version>${xmlunit.version}</version>
155156
<scope>test</scope>
156157
</dependency>
158+
<dependency>
159+
<groupId>org.assertj</groupId>
160+
<artifactId>assertj-core</artifactId>
161+
<version>${assertj.version}</version>
162+
<scope>test</scope>
163+
</dependency>
157164
<dependency>
158165
<groupId>com.sun.mail</groupId>
159166
<artifactId>javax.mail</artifactId>

spring-ws-core/src/main/java/org/springframework/ws/transport/AbstractWebServiceConnection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,6 @@ private void checkClosed() {
165165
protected void onClose() throws IOException {
166166
}
167167

168+
169+
168170
}

spring-ws-core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @see WebServiceMessageSender#createConnection(URI)
3434
* @since 1.0.0
3535
*/
36-
public interface WebServiceConnection {
36+
public interface WebServiceConnection extends AutoCloseable {
3737

3838
/**
3939
* Sends the given message using this connection.

spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2014 the original author or authors.
2+
* Copyright 2005-2018 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.
@@ -21,6 +21,7 @@
2121
import java.net.URI;
2222
import java.net.URL;
2323
import java.net.URLConnection;
24+
import java.time.Duration;
2425

2526
import org.springframework.ws.transport.WebServiceConnection;
2627

@@ -33,11 +34,37 @@
3334
* is rather limited in its capabilities.
3435
*
3536
* @author Arjen Poutsma
37+
* @author Kazuki Shimizu
3638
* @see java.net.HttpURLConnection
3739
* @since 1.0.0
3840
*/
3941
public class HttpUrlConnectionMessageSender extends AbstractHttpWebServiceMessageSender {
4042

43+
private Duration connectionTimeout = Duration.ofSeconds(60);
44+
private Duration readTimeout = Duration.ofSeconds(60);
45+
46+
/**
47+
* Sets the timeout until a connection is established.
48+
*
49+
* @param connectTimeout the timeout value
50+
* @see URLConnection#setConnectTimeout(int)
51+
* @since 3.0.1
52+
*/
53+
public void setConnectionTimeout(Duration connectTimeout) {
54+
this.connectionTimeout = connectTimeout;
55+
}
56+
57+
/**
58+
* Set the socket read timeout.
59+
*
60+
* @param readTimeout the timeout value
61+
* @see URLConnection#setReadTimeout(int)
62+
* @since 3.0.1
63+
*/
64+
public void setReadTimeout(Duration readTimeout) {
65+
this.readTimeout = readTimeout;
66+
}
67+
4168
@Override
4269
public WebServiceConnection createConnection(URI uri) throws IOException {
4370
URL url = uri.toURL();
@@ -71,7 +98,8 @@ protected void prepareConnection(HttpURLConnection connection) throws IOExceptio
7198
connection.setRequestProperty(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
7299
HttpTransportConstants.CONTENT_ENCODING_GZIP);
73100
}
101+
connection.setConnectTimeout(Math.toIntExact(this.connectionTimeout.toMillis()));
102+
connection.setReadTimeout(Math.toIntExact(this.readTimeout.toMillis()));
74103
}
75104

76-
77105
}

spring-ws-core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2014 the original author or authors.
2+
* Copyright 2005-2018 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.
@@ -60,7 +60,7 @@
6060
import static org.custommonkey.xmlunit.XMLAssert.*;
6161
import static org.junit.Assert.assertEquals;
6262

63-
public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
63+
public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase<T extends AbstractHttpWebServiceMessageSender> {
6464

6565
private Server jettyServer;
6666

@@ -84,8 +84,6 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
8484
"<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'><SOAP-ENV:Header/><SOAP-ENV:Body>" +
8585
RESPONSE + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
8686

87-
private AbstractHttpWebServiceMessageSender messageSender;
88-
8987
private Context jettyContext;
9088

9189
private MessageFactory saajMessageFactory;
@@ -94,7 +92,9 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
9492

9593
private WebServiceMessageFactory messageFactory;
9694

97-
private URI connectionUri;
95+
protected T messageSender;
96+
97+
protected URI connectionUri;
9898

9999
@Before
100100
public final void setUp() throws Exception {
@@ -112,7 +112,7 @@ public final void setUp() throws Exception {
112112
transformerFactory = TransformerFactory.newInstance();
113113
}
114114

115-
protected abstract AbstractHttpWebServiceMessageSender createMessageSender();
115+
protected abstract T createMessageSender();
116116

117117
@After
118118
public final void tearDown() throws Exception {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2014 the original author or authors.
2+
* Copyright 2005-2018 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.
@@ -40,10 +40,11 @@
4040
import org.springframework.ws.transport.WebServiceConnection;
4141
import org.springframework.ws.transport.support.FreePortScanner;
4242

43-
public class CommonsHttpMessageSenderIntegrationTest extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
43+
public class CommonsHttpMessageSenderIntegrationTest
44+
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<CommonsHttpMessageSender> {
4445

4546
@Override
46-
protected AbstractHttpWebServiceMessageSender createMessageSender() {
47+
protected CommonsHttpMessageSender createMessageSender() {
4748
return new CommonsHttpMessageSender();
4849
}
4950

spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponentsMessageSenderIntegrationTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2014 the original author or authors.
2+
* Copyright 2005-2018 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.
@@ -47,10 +47,11 @@
4747
import org.springframework.ws.transport.WebServiceConnection;
4848
import org.springframework.ws.transport.support.FreePortScanner;
4949

50-
public class HttpComponentsMessageSenderIntegrationTest extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
50+
public class HttpComponentsMessageSenderIntegrationTest
51+
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<HttpComponentsMessageSender> {
5152

5253
@Override
53-
protected AbstractHttpWebServiceMessageSender createMessageSender() {
54+
protected HttpComponentsMessageSender createMessageSender() {
5455
return new HttpComponentsMessageSender();
5556
}
5657

spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSenderIntegrationTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2018 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.
@@ -16,11 +16,31 @@
1616

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

19+
import java.time.Duration;
20+
21+
import org.junit.Test;
22+
23+
import static org.assertj.core.api.Assertions.*;
24+
1925
public class HttpUrlConnectionMessageSenderIntegrationTest
20-
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
26+
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<HttpUrlConnectionMessageSender> {
2127

2228
@Override
23-
protected AbstractHttpWebServiceMessageSender createMessageSender() {
29+
protected HttpUrlConnectionMessageSender createMessageSender() {
2430
return new HttpUrlConnectionMessageSender();
2531
}
32+
33+
@Test
34+
public void testSetTimeout() throws Exception {
35+
36+
this.messageSender.setConnectionTimeout(Duration.ofSeconds(3));
37+
this.messageSender.setReadTimeout(Duration.ofSeconds(5));
38+
39+
try (HttpUrlConnection connection =
40+
(HttpUrlConnection) this.messageSender.createConnection(this.connectionUri)) {
41+
assertThat(connection.getConnection().getConnectTimeout()).isEqualTo(3000);
42+
assertThat(connection.getConnection().getReadTimeout()).isEqualTo(5000);
43+
}
44+
}
45+
2646
}

0 commit comments

Comments
 (0)