Skip to content

Commit 3aaa77e

Browse files
Mikhail Putilovgregturn
authored andcommitted
Align Content-Type headers with Spring Framework policies for HttpClient 5.
Some tools expect a non-blank Content-Type header from HttpEntity during execution. Resolves #1368.
1 parent 3ec8af7 commit 3aaa77e

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727

2828
import org.apache.hc.client5.http.classic.HttpClient;
2929
import org.apache.hc.client5.http.classic.methods.HttpPost;
30-
import org.apache.hc.core5.http.ClassicHttpResponse;
31-
import org.apache.hc.core5.http.HttpEntity;
32-
import org.apache.hc.core5.http.HttpResponse;
33-
import org.apache.hc.core5.http.NameValuePair;
30+
import org.apache.hc.core5.http.*;
3431
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
3532
import org.apache.hc.core5.http.io.entity.EntityUtils;
3633
import org.apache.hc.core5.http.protocol.HttpContext;
@@ -119,8 +116,8 @@ protected OutputStream getRequestOutputStream() throws IOException {
119116

120117
@Override
121118
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
122-
123-
httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray(), null));
119+
var contentType = ContentType.parse(httpPost.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
120+
httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray(), contentType));
124121
requestBuffer = null;
125122

126123
if (httpContext != null) {

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

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

19-
import static org.assertj.core.api.AssertionsForClassTypes.*;
20-
import static org.springframework.ws.transport.http.HttpComponents5ClientFactory.*;
21-
2219
import jakarta.servlet.http.HttpServlet;
2320
import jakarta.servlet.http.HttpServletRequest;
2421
import jakarta.servlet.http.HttpServletResponse;
2522
import jakarta.xml.soap.MessageFactory;
26-
27-
import java.io.IOException;
28-
import java.net.URI;
29-
import java.util.HashMap;
30-
import java.util.Map;
31-
3223
import org.apache.hc.client5.http.HttpRoute;
24+
import org.apache.hc.client5.http.classic.ExecChainHandler;
3325
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
26+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
3427
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
3528
import org.apache.hc.core5.http.HttpHost;
3629
import org.eclipse.jetty.server.Connector;
@@ -45,12 +38,33 @@
4538
import org.springframework.ws.transport.WebServiceConnection;
4639
import org.springframework.ws.transport.support.FreePortScanner;
4740

41+
import java.io.IOException;
42+
import java.net.URI;
43+
import java.util.HashMap;
44+
import java.util.Map;
45+
46+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
47+
import static org.springframework.ws.transport.http.HttpComponents5ClientFactory.getPort;
48+
4849
class HttpComponents5MessageSenderIntegrationTest
4950
extends AbstractHttpWebServiceMessageSenderIntegrationTestCase<HttpComponents5MessageSender> {
5051

52+
private static CloseableHttpClient createHttpClientWithAssertion() {
53+
ExecChainHandler handler = (request, scope, chain) -> {
54+
assertThat(request.getEntity().getContentType())
55+
.describedAs("Exec interceptors are supposed to receive content type. Verify that HttpEntity class is instantiated correctly")
56+
.isNotBlank();
57+
return chain.proceed(request, scope);
58+
};
59+
return HttpClientBuilder.create()
60+
.addRequestInterceptorFirst(new HttpComponents5MessageSender.RemoveSoapHeadersInterceptor())
61+
.addExecInterceptorFirst("logbook-alike exec interceptor", handler)
62+
.build();
63+
}
64+
5165
@Override
5266
protected HttpComponents5MessageSender createMessageSender() {
53-
return new HttpComponents5MessageSender();
67+
return new HttpComponents5MessageSender(createHttpClientWithAssertion());
5468
}
5569

5670
@Test // GH-1164

0 commit comments

Comments
 (0)