jaxb2 and cxf: custom HTTP request message sender (for custom HTTP headers)
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 Dec 2015 23:47:48 +0000 (00:47 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 30 Dec 2015 23:47:48 +0000 (00:47 +0100)
cxf/web-services-spring-cxf-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java [new file with mode: 0644]
cxf/web-services-spring-cxf-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml
jaxb2/web-services-spring-jaxb2-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java [new file with mode: 0644]
jaxb2/web-services-spring-jaxb2-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml

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 (file)
index 0000000..cf44220
--- /dev/null
@@ -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.
+ * 
+ * <p>
+ * 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/}
+ * </p>
+ * 
+ * <p>
+ * Four implementations for four HTTP clients:
+ * <ul>
+ * <li> org.springframework.ws.transport.http.ClientHttpRequestMessageSender (ClientHttpRequestFactory from the Spring framework) </li>
+ * <li> org.springframework.ws.transport.http.CommonsHttpMessageSender  (Jakarta implementation) </li>
+ * <li> org.springframework.ws.transport.http.HttpComponentsMessageSender (Apache HttpClient) </li>
+ * <li> org.springframework.ws.transport.http.HttpUrlConnectionMessageSender 
+ * (org.springframework.ws.transport.http.HttpUrlConnection internal Spring framework implementation) </li>
+ * </ul>
+ * </p>
+ */
+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);
+       }
+       
+}
index a824d54..7f6f1f3 100644 (file)
         </property>
         
         <property name="faultMessageResolver" ref="customFaultMessageResolver" />
+        
+        <property name="messageSenders">
+               <list>
+                       <bean class="de.spring.webservices.client.transport.http.CustomHttpRequestMessageSender" />
+               </list>
+        </property>
     </bean>
     
     <!--
diff --git a/jaxb2/web-services-spring-jaxb2-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java b/jaxb2/web-services-spring-jaxb2-client/src/main/java/de/spring/webservices/client/transport/http/CustomHttpRequestMessageSender.java
new file mode 100644 (file)
index 0000000..cf44220
--- /dev/null
@@ -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.
+ * 
+ * <p>
+ * 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/}
+ * </p>
+ * 
+ * <p>
+ * Four implementations for four HTTP clients:
+ * <ul>
+ * <li> org.springframework.ws.transport.http.ClientHttpRequestMessageSender (ClientHttpRequestFactory from the Spring framework) </li>
+ * <li> org.springframework.ws.transport.http.CommonsHttpMessageSender  (Jakarta implementation) </li>
+ * <li> org.springframework.ws.transport.http.HttpComponentsMessageSender (Apache HttpClient) </li>
+ * <li> org.springframework.ws.transport.http.HttpUrlConnectionMessageSender 
+ * (org.springframework.ws.transport.http.HttpUrlConnection internal Spring framework implementation) </li>
+ * </ul>
+ * </p>
+ */
+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);
+       }
+       
+}
index 8027c40..9358079 100644 (file)
         </property>
         
         <property name="faultMessageResolver" ref="customFaultMessageResolver" />
+
+        <property name="messageSenders">
+            <list>
+                <bean class="de.spring.webservices.client.transport.http.CustomHttpRequestMessageSender" />
+            </list>
+        </property>
     </bean>
     
     <!--