001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.transport.http;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.net.HttpURLConnection;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.springframework.util.Assert;
030: import org.springframework.util.StringUtils;
031: import org.springframework.ws.WebServiceMessage;
032: import org.springframework.ws.transport.WebServiceConnection;
033:
034: /**
035: * Implementation of the {@link WebServiceConnection} interface that uses a {@link HttpURLConnection}.
036: *
037: * @author Arjen Poutsma
038: * @since 1.0.0
039: */
040: public class HttpUrlConnection extends AbstractHttpSenderConnection {
041:
042: private final HttpURLConnection connection;
043:
044: /**
045: * Creates a new instance of the <code>HttpUrlConnection</code> with the given <code>HttpURLConnection</code>.
046: *
047: * @param connection the <code>HttpURLConnection</code>
048: */
049: protected HttpUrlConnection(HttpURLConnection connection) {
050: Assert.notNull(connection, "connection must not be null");
051: this .connection = connection;
052: }
053:
054: public HttpURLConnection getConnection() {
055: return connection;
056: }
057:
058: public void close() {
059: connection.disconnect();
060: }
061:
062: /*
063: * Sending request
064: */
065:
066: protected void addRequestHeader(String name, String value)
067: throws IOException {
068: connection.addRequestProperty(name, value);
069: }
070:
071: protected OutputStream getRequestOutputStream() throws IOException {
072: return connection.getOutputStream();
073: }
074:
075: protected void onSendAfterWrite(WebServiceMessage message)
076: throws IOException {
077: connection.connect();
078: }
079:
080: /*
081: * Receiving response
082: */
083:
084: protected long getResponseContentLength() throws IOException {
085: return connection.getContentLength();
086: }
087:
088: protected Iterator getResponseHeaderNames() throws IOException {
089: List headerNames = new ArrayList();
090: // Header field 0 is the status line, so we start at 1
091: int i = 1;
092: while (true) {
093: String headerName = connection.getHeaderFieldKey(i);
094: if (!StringUtils.hasLength(headerName)) {
095: break;
096: }
097: headerNames.add(headerName);
098: i++;
099: }
100: return headerNames.iterator();
101: }
102:
103: protected Iterator getResponseHeaders(String name)
104: throws IOException {
105: String headerField = connection.getHeaderField(name);
106: if (headerField == null) {
107: return Collections.EMPTY_LIST.iterator();
108: } else {
109: Set tokens = StringUtils
110: .commaDelimitedListToSet(headerField);
111: return tokens.iterator();
112: }
113: }
114:
115: protected int getResponseCode() throws IOException {
116: return connection.getResponseCode();
117: }
118:
119: protected String getResponseMessage() throws IOException {
120: return connection.getResponseMessage();
121: }
122:
123: protected InputStream getRawResponseInputStream()
124: throws IOException {
125: if (connection.getResponseCode() / 100 != 2) {
126: return connection.getErrorStream();
127: } else {
128: return connection.getInputStream();
129: }
130: }
131: }
|