From: Gustavo Martin Morcuende Date: Wed, 30 Dec 2015 23:47:48 +0000 (+0100) Subject: jaxb2 and cxf: custom HTTP request message sender (for custom HTTP headers) X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2ffcb20e38671315cc4d56c6e9bf2ec81f122af7;p=SpringWebServicesForFun%2F.git jaxb2 and cxf: custom HTTP request message sender (for custom HTTP headers) --- 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 @@ + + + + + +