01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.ws.transport.http;
18:
19: import java.io.IOException;
20: import java.net.HttpURLConnection;
21: import java.net.URL;
22: import java.net.URLConnection;
23:
24: import org.springframework.ws.transport.WebServiceConnection;
25:
26: /**
27: * <code>WebServiceMessageSender</code> implementation that uses standard J2SE facilities to execute POST requests,
28: * without support for HTTP authentication or advanced configuration options.
29: * <p/>
30: * Designed for easy subclassing, customizing specific template methods. However, consider {@link
31: * CommonsHttpMessageSender} for more sophisticated needs: the J2SE <code>HttpURLConnection</code> is rather limited in
32: * its capabilities.
33: *
34: * @author Arjen Poutsma
35: * @see java.net.HttpURLConnection
36: * @since 1.0.0
37: */
38: public class HttpUrlConnectionMessageSender extends
39: AbstractHttpWebServiceMessageSender {
40:
41: private static final String HTTP_METHOD_POST = "POST";
42:
43: public WebServiceConnection createConnection(String uri)
44: throws IOException {
45: URL url = new URL(uri);
46: URLConnection connection = url.openConnection();
47: if (!(connection instanceof HttpURLConnection)) {
48: throw new HttpTransportException("URI [" + uri
49: + "] is not an HTTP URL");
50: } else {
51: HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
52: httpURLConnection.setRequestMethod(HTTP_METHOD_POST);
53: httpURLConnection.setUseCaches(false);
54: httpURLConnection.setDoInput(true);
55: httpURLConnection.setDoOutput(true);
56: if (isAcceptGzipEncoding()) {
57: httpURLConnection.setRequestProperty(
58: HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
59: }
60: return new HttpUrlConnection(httpURLConnection);
61: }
62: }
63:
64: }
|