diff --git a/pom.xml b/pom.xml index 93b791ea..53be2af2 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,7 @@ 4.1.2 1.8.13 + 3.9.0 1.2.20 3.1 2.5 @@ -154,6 +155,12 @@ ${xmlunit.version} test + + org.assertj + assertj-core + ${assertj.version} + test + com.sun.mail javax.mail diff --git a/spring-ws-core/src/main/java/org/springframework/ws/transport/AbstractWebServiceConnection.java b/spring-ws-core/src/main/java/org/springframework/ws/transport/AbstractWebServiceConnection.java index 9685999a..b9fc32d2 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/transport/AbstractWebServiceConnection.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/transport/AbstractWebServiceConnection.java @@ -165,4 +165,6 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti protected void onClose() throws IOException { } + + } diff --git a/spring-ws-core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java b/spring-ws-core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java index e2c62143..d9e2701f 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java @@ -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. diff --git a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.java b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.java index 3b3d0417..6ae53e68 100644 --- a/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.java +++ b/spring-ws-core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.java @@ -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())); } - } diff --git a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java index 15cbaa03..e8afa9f1 100644 --- a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java +++ b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/AbstractHttpWebServiceMessageSenderIntegrationTestCase.java @@ -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 { private Server jettyServer; @@ -84,8 +84,6 @@ public abstract class AbstractHttpWebServiceMessageSenderIntegrationTestCase { "" + RESPONSE + ""; - 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 { diff --git a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java index 7d8f9bc0..fd02c6ae 100644 --- a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java +++ b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/CommonsHttpMessageSenderIntegrationTest.java @@ -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 { @Override - protected AbstractHttpWebServiceMessageSender createMessageSender() { + protected CommonsHttpMessageSender createMessageSender() { return new CommonsHttpMessageSender(); } diff --git a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponentsMessageSenderIntegrationTest.java b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponentsMessageSenderIntegrationTest.java index 9f3904b6..605be290 100644 --- a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponentsMessageSenderIntegrationTest.java +++ b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpComponentsMessageSenderIntegrationTest.java @@ -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 { @Override - protected AbstractHttpWebServiceMessageSender createMessageSender() { + protected HttpComponentsMessageSender createMessageSender() { return new HttpComponentsMessageSender(); } diff --git a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSenderIntegrationTest.java b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSenderIntegrationTest.java index 19b3fd11..28b3a514 100644 --- a/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSenderIntegrationTest.java +++ b/spring-ws-core/src/test/java/org/springframework/ws/transport/http/HttpUrlConnectionMessageSenderIntegrationTest.java @@ -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 { @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); + } + } + } \ No newline at end of file