JDK HttpClient implementation for WebServiceMessageSender.

Allow for setting the connection and request timeouts and also apply them
if applicable.

Resolves #1355.
This commit is contained in:
Marten Deinum
2023-05-11 20:50:40 +02:00
committed by Greg L. Turnquist
parent 4c5bd74505
commit 5db97a77ab
3 changed files with 260 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
/*
* Copyright 2023-2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.transport.WebServiceConnection;
/**
* Implementation of the {@link WebServiceConnection} interface that uses a Java
* {@link HttpClient}.
*
* @author Marten Deinum
* @see java.net.http.HttpClient
* @see java.net.http.HttpRequest
* @since 4.1
*/
public class JdkHttpClientConnection extends AbstractHttpSenderConnection {
private static final List<String> DISALLOWED_HEADERS =
List.of("connection", "content-length", "expect", "host", "upgrade");
private final Log logger = LogFactory.getLog(getClass());
private final HttpClient client;
private final Builder requestBuilder;
private final URI uri;
private HttpResponse<InputStream> response;
private HttpRequest request;
private ByteArrayOutputStream requestBuffer;
public JdkHttpClientConnection(HttpClient client, URI uri, Duration requestTimeout) {
this.client = client;
this.uri = uri;
this.requestBuilder = HttpRequest.newBuilder(uri).timeout(requestTimeout);
}
@Override
protected OutputStream getRequestOutputStream() throws IOException {
return this.requestBuffer;
}
@Override
public Iterator<String> getResponseHeaderNames() throws IOException {
return response.headers().map().keySet().iterator();
}
@Override
public Iterator<String> getResponseHeaders(String name) throws IOException {
return response.headers().allValues(name).iterator();
}
@Override
public void addRequestHeader(String name, String value) throws IOException {
if (DISALLOWED_HEADERS.contains(name.toLowerCase())) {
logger.info("HttpClient doesn't allow setting the '"+name + "' header, ignoring!");
return;
}
this.requestBuilder.header(name, value);
}
@Override
public URI getUri() throws URISyntaxException {
return this.uri;
}
@Override
protected int getResponseCode() throws IOException {
return this.response != null ? this.response.statusCode() : 0;
}
@Override
protected String getResponseMessage() throws IOException {
HttpStatus status = HttpStatus.resolve(getResponseCode());
return status != null ? status.getReasonPhrase() : "";
}
@Override
protected long getResponseContentLength() throws IOException {
if (this.response != null) {
return this.response.headers()
.firstValueAsLong(HttpTransportConstants.HEADER_CONTENT_LENGTH)
.orElse(-1);
}
return 0;
}
@Override
protected InputStream getRawResponseInputStream() throws IOException {
return this.response.body();
}
@Override
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
requestBuffer = new ByteArrayOutputStream();
}
@Override
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
byte[] body = this.requestBuffer.toByteArray();
this.request = requestBuilder.POST(BodyPublishers.ofByteArray(body)).build();
try {
this.response = this.client.send(this.request, BodyHandlers.ofInputStream());
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
throw new IllegalStateException(ex);
}
}
@Override
protected void onClose() throws IOException {
if (this.response != null) {
this.response.body().close();
}
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2023-2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.time.Duration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.ws.transport.WebServiceConnection;
/**
* {@code WebServiceMessageSender} implementation that uses the standard Java {@code HttpClient}
* facilities to execute POST requests.
* <p>
* Can be used with a simple default configured {@code HttpClient} or can be constructed with a
* pre-configured {@code HttpClient}.
*
* @author Marten Deinum
* @see java.net.http.HttpClient
* @since 4.1
*/
public class JdkHttpClientMessageSender extends AbstractHttpWebServiceMessageSender
implements InitializingBean {
private Duration connectionTimeout = Duration.ofSeconds(60);
private Duration requestTimeout = Duration.ofSeconds(60);
private HttpClient client;
public JdkHttpClientMessageSender() {}
public JdkHttpClientMessageSender(HttpClient client) {
this.client = client;
}
public void setConnectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public void setRequestTimeout(Duration requestTimeout) {
this.requestTimeout = requestTimeout;
}
@Override
public WebServiceConnection createConnection(URI uri) throws IOException {
JdkHttpClientConnection connection =
new JdkHttpClientConnection(this.client, uri, requestTimeout);
if (isAcceptGzipEncoding()) {
connection.addRequestHeader(
HttpTransportConstants.HEADER_ACCEPT_ENCODING,
HttpTransportConstants.CONTENT_ENCODING_GZIP);
}
return connection;
}
@Override
public void afterPropertiesSet() throws Exception {
if (this.client == null) {
this.client = HttpClient.newBuilder()
.connectTimeout(this.connectionTimeout)
.build();
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2023-2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
public class JdkHttpClientMessageSenderIntegrationTest
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<JdkHttpClientMessageSender> {
@Override
protected JdkHttpClientMessageSender createMessageSender() {
return new JdkHttpClientMessageSender();
}
}