From 2ffcb20e38671315cc4d56c6e9bf2ec81f122af7 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 31 Dec 2015 00:47:48 +0100 Subject: [PATCH] jaxb2 and cxf: custom HTTP request message sender (for custom HTTP headers) --- .../http/CustomHttpRequestMessageSender.java | 57 ++++++++++++++++++++++ .../ws/client-spring-configuration.xml | 6 +++ .../http/CustomHttpRequestMessageSender.java | 57 ++++++++++++++++++++++ .../ws/client-spring-configuration.xml | 6 +++ 4 files changed, 126 insertions(+) create mode 100644 cxf/web-services-spring-cxf-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java create mode 100644 jaxb2/web-services-spring-jaxb2-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java diff --git a/cxf/web-services-spring-cxf-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java b/cxf/web-services-spring-cxf-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java new file mode 100644 index 0000000..cf44220 --- /dev/null +++ b/cxf/web-services-spring-cxf-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java @@ -0,0 +1,57 @@ +package de.spring.webservices.client.transport.http; + +import java.io.IOException; +import java.net.URI; + +import org.springframework.http.HttpMethod; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.ws.transport.WebServiceConnection; +import org.springframework.ws.transport.http.AbstractHttpWebServiceMessageSender; +import org.springframework.ws.transport.http.ClientHttpRequestConnection; +import org.springframework.ws.transport.http.HttpTransportConstants; + + +/** + * Based on ClientHttpRequestMessageSender from the Spring WS framework. + * + *

+ * Spring WS framework also provides implementations based on the HTTP clients by Jakarta and Apache HttpClient: + * {@link https://hc.apache.org/httpcomponents-client-ga/} and {@link http://hc.apache.org/httpclient-3.x/} + *

+ * + *

+ * Four implementations for four HTTP clients: + *

+ *

+ */ +public class CustomHttpRequestMessageSender extends AbstractHttpWebServiceMessageSender { + private static final String MY_CUSTOM_HEADER = "MY_CUSTOM_HEADER"; + + private final ClientHttpRequestFactory requestFactory; + + public CustomHttpRequestMessageSender() { + requestFactory = new SimpleClientHttpRequestFactory(); + } + + @Override + public WebServiceConnection createConnection(URI uri) throws IOException { + ClientHttpRequest request = requestFactory.createRequest(uri, HttpMethod.POST); + if (isAcceptGzipEncoding()) { + request.getHeaders().add(HttpTransportConstants.HEADER_ACCEPT_ENCODING, + HttpTransportConstants.CONTENT_ENCODING_GZIP); + } + + request.getHeaders().add(MY_CUSTOM_HEADER, "gumartinm.name"); + + return new ClientHttpRequestConnection(request); + } + +} diff --git a/cxf/web-services-spring-cxf-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml b/cxf/web-services-spring-cxf-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml index a824d54..7f6f1f3 100644 --- a/cxf/web-services-spring-cxf-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml +++ b/cxf/web-services-spring-cxf-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml @@ -177,6 +177,12 @@ + + + + + +