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.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.util.Arrays;
024: import java.util.Iterator;
025:
026: import org.apache.commons.httpclient.Header;
027: import org.apache.commons.httpclient.HttpClient;
028: import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
029: import org.apache.commons.httpclient.methods.PostMethod;
030: import org.springframework.util.Assert;
031: import org.springframework.ws.WebServiceMessage;
032: import org.springframework.ws.transport.WebServiceConnection;
033:
034: /**
035: * Implementation of {@link WebServiceConnection} that is based on Jakarta Commons HttpClient. Exposes a {@link
036: * PostMethod}.
037: *
038: * @author Arjen Poutsma
039: * @since 1.0.0
040: */
041: public class CommonsHttpConnection extends AbstractHttpSenderConnection {
042:
043: private final HttpClient httpClient;
044:
045: private final PostMethod postMethod;
046:
047: private ByteArrayOutputStream requestBuffer;
048:
049: protected CommonsHttpConnection(HttpClient httpClient,
050: PostMethod postMethod) {
051: Assert.notNull(httpClient, "httpClient must not be null");
052: Assert.notNull(postMethod, "postMethod must not be null");
053: this .httpClient = httpClient;
054: this .postMethod = postMethod;
055: }
056:
057: public PostMethod getPostMethod() {
058: return postMethod;
059: }
060:
061: public void close() throws IOException {
062: postMethod.releaseConnection();
063: }
064:
065: /*
066: * Sending request
067: */
068:
069: protected void onSendBeforeWrite(WebServiceMessage message)
070: throws IOException {
071: requestBuffer = new ByteArrayOutputStream();
072: }
073:
074: protected void addRequestHeader(String name, String value)
075: throws IOException {
076: postMethod.addRequestHeader(name, value);
077: }
078:
079: protected OutputStream getRequestOutputStream() throws IOException {
080: return requestBuffer;
081: }
082:
083: protected void onSendAfterWrite(WebServiceMessage message)
084: throws IOException {
085: postMethod.setRequestEntity(new ByteArrayRequestEntity(
086: requestBuffer.toByteArray()));
087: requestBuffer = null;
088: httpClient.executeMethod(postMethod);
089: }
090:
091: /*
092: * Receiving response
093: */
094:
095: protected int getResponseCode() throws IOException {
096: return postMethod.getStatusCode();
097: }
098:
099: protected String getResponseMessage() throws IOException {
100: return postMethod.getStatusText();
101: }
102:
103: protected long getResponseContentLength() throws IOException {
104: return postMethod.getResponseContentLength();
105: }
106:
107: protected InputStream getRawResponseInputStream()
108: throws IOException {
109: return postMethod.getResponseBodyAsStream();
110: }
111:
112: protected Iterator getResponseHeaderNames() throws IOException {
113: Header[] headers = postMethod.getResponseHeaders();
114: String[] names = new String[headers.length];
115: for (int i = 0; i < headers.length; i++) {
116: names[i] = headers[i].getName();
117: }
118: return Arrays.asList(names).iterator();
119: }
120:
121: protected Iterator getResponseHeaders(String name)
122: throws IOException {
123: Header[] headers = postMethod.getResponseHeaders(name);
124: String[] values = new String[headers.length];
125: for (int i = 0; i < headers.length; i++) {
126: values[i] = headers[i].getValue();
127: }
128: return Arrays.asList(values).iterator();
129: }
130:
131: }
|