SWS-1014 - Support custom timeout settings on HttpUrlConnectionMessageSender.

This commit is contained in:
Kazuki Shimizu
2018-02-18 02:16:02 +09:00
committed by Greg Turnquist
parent f7803a5efa
commit 39d0895a9c
8 changed files with 77 additions and 18 deletions

View File

@@ -87,6 +87,7 @@
<activemq.version>4.1.2</activemq.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>3.9.0</assertj.version>
<axiom.version>1.2.20</axiom.version>
<commons-httpclient.version>3.1</commons-httpclient.version>
<commons-io.version>2.5</commons-io.version>
@@ -154,6 +155,12 @@
<version>${xmlunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>

View File

@@ -165,4 +165,6 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
protected void onClose() throws IOException {
}
}

View File

@@ -33,7 +33,7 @@ import org.springframework.ws.WebServiceMessageFactory;
* @see WebServiceMessageSender#createConnection(URI)
* @since 1.0.0
*/
public interface WebServiceConnection {
public interface WebServiceConnection extends AutoCloseable {
/**
* Sends the given message using this connection.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2014 the original author or authors.
* Copyright 2005-2018 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.
@@ -21,6 +21,7 @@ import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.time.Duration;
import org.springframework.ws.transport.WebServiceConnection;
@@ -33,11 +34,37 @@ import org.springframework.ws.transport.WebServiceConnection;
* is rather limited in its capabilities.
*
* @author Arjen Poutsma
* @author Kazuki Shimizu
* @see java.net.HttpURLConnection
* @since 1.0.0
*/
public class HttpUrlConnectionMessageSender extends AbstractHttpWebServiceMessageSender {
private Duration connectionTimeout = Duration.ofSeconds(60);
private Duration readTimeout = Duration.ofSeconds(60);
/**
* Sets the timeout until a connection is established.
*
* @param connectTimeout the timeout value
* @see URLConnection#setConnectTimeout(int)
* @since 3.0.1
*/
public void setConnectionTimeout(Duration connectTimeout) {
this.connectionTimeout = connectTimeout;
}
/**
* Set the socket read timeout.
*
* @param readTimeout the timeout value
* @see URLConnection#setReadTimeout(int)
* @since 3.0.1
*/
public void setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
}
@Override
public WebServiceConnection createConnection(URI uri) throws IOException {
URL url = uri.toURL();
@@ -71,7 +98,8 @@ public class HttpUrlConnectionMessageSender extends AbstractHttpWebServiceMessag
connection.setRequestProperty(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
HttpTransportConstants.CONTENT_ENCODING_GZIP);
}
connection.setConnectTimeout(Math.toIntExact(this.connectionTimeout.toMillis()));
connection.setReadTimeout(Math.toIntExact(this.readTimeout.toMillis()));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2014 the original author or authors.
* Copyright 2005-2018 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.
@@ -60,7 +60,7 @@ import org.springframework.xml.transform.StringSource;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase<T extends AbstractHttpWebServiceMessageSender> {
private Server jettyServer;
@@ -84,8 +84,6 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
"<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'><SOAP-ENV:Header/><SOAP-ENV:Body>" +
RESPONSE + "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
private AbstractHttpWebServiceMessageSender messageSender;
private Context jettyContext;
private MessageFactory saajMessageFactory;
@@ -94,7 +92,9 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
private WebServiceMessageFactory messageFactory;
private URI connectionUri;
protected T messageSender;
protected URI connectionUri;
@Before
public final void setUp() throws Exception {
@@ -112,7 +112,7 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase {
transformerFactory = TransformerFactory.newInstance();
}
protected abstract AbstractHttpWebServiceMessageSender createMessageSender();
protected abstract T createMessageSender();
@After
public final void tearDown() throws Exception {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2014 the original author or authors.
* Copyright 2005-2018 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.
@@ -40,10 +40,11 @@ import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.support.FreePortScanner;
public class CommonsHttpMessageSenderIntegrationTest extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
public class CommonsHttpMessageSenderIntegrationTest
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<CommonsHttpMessageSender> {
@Override
protected AbstractHttpWebServiceMessageSender createMessageSender() {
protected CommonsHttpMessageSender createMessageSender() {
return new CommonsHttpMessageSender();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2014 the original author or authors.
* Copyright 2005-2018 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.
@@ -47,10 +47,11 @@ import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.support.FreePortScanner;
public class HttpComponentsMessageSenderIntegrationTest extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
public class HttpComponentsMessageSenderIntegrationTest
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<HttpComponentsMessageSender> {
@Override
protected AbstractHttpWebServiceMessageSender createMessageSender() {
protected HttpComponentsMessageSender createMessageSender() {
return new HttpComponentsMessageSender();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2018 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.
@@ -16,11 +16,31 @@
package org.springframework.ws.transport.http;
import java.time.Duration;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class HttpUrlConnectionMessageSenderIntegrationTest
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase {
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<HttpUrlConnectionMessageSender> {
@Override
protected AbstractHttpWebServiceMessageSender createMessageSender() {
protected HttpUrlConnectionMessageSender createMessageSender() {
return new HttpUrlConnectionMessageSender();
}
@Test
public void testSetTimeout() throws Exception {
this.messageSender.setConnectionTimeout(Duration.ofSeconds(3));
this.messageSender.setReadTimeout(Duration.ofSeconds(5));
try (HttpUrlConnection connection =
(HttpUrlConnection) this.messageSender.createConnection(this.connectionUri)) {
assertThat(connection.getConnection().getConnectTimeout()).isEqualTo(3000);
assertThat(connection.getConnection().getReadTimeout()).isEqualTo(5000);
}
}
}