1 /*
2 * Copyright 2006 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.URI;
22 import java.net.URL;
23 import java.net.URLConnection;
24
25 import org.springframework.ws.transport.WebServiceConnection;
26
27 /**
28 * <code>WebServiceMessageSender</code> implementation that uses standard J2SE facilities to execute POST requests,
29 * without support for HTTP authentication or advanced configuration options.
30 * <p/>
31 * Designed for easy subclassing, customizing specific template methods. However, consider {@link
32 * CommonsHttpMessageSender} for more sophisticated needs: the J2SE <code>HttpURLConnection</code> is rather limited in
33 * its capabilities.
34 *
35 * @author Arjen Poutsma
36 * @see java.net.HttpURLConnection
37 * @since 1.0.0
38 */
39 public class HttpUrlConnectionMessageSender extends AbstractHttpWebServiceMessageSender {
40
41 public WebServiceConnection createConnection(URI uri) throws IOException {
42 URL url = uri.toURL();
43 URLConnection connection = url.openConnection();
44 if (!(connection instanceof HttpURLConnection)) {
45 throw new HttpTransportException("URI [" + uri + "] is not an HTTP URL");
46 }
47 else {
48 HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
49 prepareConnection(httpURLConnection);
50 return new HttpUrlConnection(httpURLConnection);
51 }
52 }
53
54 /**
55 * Template method for preparing the given {@link java.net.HttpURLConnection}.
56 * <p/>
57 * The default implementation prepares the connection for input and output, sets the HTTP method to POST, disables
58 * caching, and sets the {@code Accept-Encoding} header to gzip, if {@linkplain #setAcceptGzipEncoding(boolean)
59 * applicable}.
60 *
61 * @param connection the connection to prepare
62 * @throws IOException in case of I/O errors
63 */
64 protected void prepareConnection(HttpURLConnection connection) throws IOException {
65 connection.setRequestMethod(HttpTransportConstants.METHOD_POST);
66 connection.setUseCaches(false);
67 connection.setDoInput(true);
68 connection.setDoOutput(true);
69 if (isAcceptGzipEncoding()) {
70 connection.setRequestProperty(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
71 HttpTransportConstants.CONTENT_ENCODING_GZIP);
72 }
73 }
74
75
76 }