cf44220c9caca70287c014c7917ac40e8cb3b83d
[SpringWebServicesForFun/.git] /
1 package de.spring.webservices.client.transport.http;
2
3 import java.io.IOException;
4 import java.net.URI;
5
6 import org.springframework.http.HttpMethod;
7 import org.springframework.http.client.ClientHttpRequest;
8 import org.springframework.http.client.ClientHttpRequestFactory;
9 import org.springframework.http.client.SimpleClientHttpRequestFactory;
10 import org.springframework.ws.transport.WebServiceConnection;
11 import org.springframework.ws.transport.http.AbstractHttpWebServiceMessageSender;
12 import org.springframework.ws.transport.http.ClientHttpRequestConnection;
13 import org.springframework.ws.transport.http.HttpTransportConstants;
14
15
16 /**
17  * Based on ClientHttpRequestMessageSender from the Spring WS framework.
18  * 
19  * <p>
20  * Spring WS framework also provides implementations based on the HTTP clients by Jakarta and Apache HttpClient: 
21  * {@link https://hc.apache.org/httpcomponents-client-ga/} and {@link http://hc.apache.org/httpclient-3.x/}
22  * </p>
23  * 
24  * <p>
25  * Four implementations for four HTTP clients:
26  * <ul>
27  * <li> org.springframework.ws.transport.http.ClientHttpRequestMessageSender (ClientHttpRequestFactory from the Spring framework) </li>
28  * <li> org.springframework.ws.transport.http.CommonsHttpMessageSender  (Jakarta implementation) </li>
29  * <li> org.springframework.ws.transport.http.HttpComponentsMessageSender (Apache HttpClient) </li>
30  * <li> org.springframework.ws.transport.http.HttpUrlConnectionMessageSender 
31  * (org.springframework.ws.transport.http.HttpUrlConnection internal Spring framework implementation) </li>
32  * </ul>
33  * </p>
34  */
35 public class CustomHttpRequestMessageSender extends AbstractHttpWebServiceMessageSender {
36         private static final String MY_CUSTOM_HEADER = "MY_CUSTOM_HEADER";
37
38         private final ClientHttpRequestFactory requestFactory;
39
40         public CustomHttpRequestMessageSender() {
41                 requestFactory = new SimpleClientHttpRequestFactory();
42         }
43
44         @Override
45         public WebServiceConnection createConnection(URI uri) throws IOException {
46                 ClientHttpRequest request = requestFactory.createRequest(uri, HttpMethod.POST);
47                 if (isAcceptGzipEncoding()) {
48                         request.getHeaders().add(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
49                                         HttpTransportConstants.CONTENT_ENCODING_GZIP);
50                 }
51                 
52                 request.getHeaders().add(MY_CUSTOM_HEADER, "gumartinm.name");
53                 
54                 return new ClientHttpRequestConnection(request);
55         }
56         
57 }