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.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.Iterator;
023: import java.util.zip.GZIPInputStream;
024:
025: import org.springframework.util.FileCopyUtils;
026: import org.springframework.ws.transport.AbstractSenderConnection;
027: import org.springframework.ws.transport.FaultAwareWebServiceConnection;
028: import org.springframework.ws.transport.WebServiceConnection;
029:
030: /**
031: * Abstract base class for {@link WebServiceConnection} implementations that send request over HTTP.
032: *
033: * @author Arjen Poutsma
034: * @since 1.0.0
035: */
036: public abstract class AbstractHttpSenderConnection extends
037: AbstractSenderConnection implements
038: FaultAwareWebServiceConnection {
039:
040: /** Buffer used for reading the response, when the content length is invalid. */
041: private byte[] responseBuffer;
042:
043: public final boolean hasError() throws IOException {
044: return getResponseCode() / 100 != 2;
045: }
046:
047: public final String getErrorMessage() throws IOException {
048: StringBuffer buffer = new StringBuffer(getResponseMessage());
049: buffer.append(" [");
050: buffer.append(getResponseCode());
051: buffer.append(']');
052: return buffer.toString();
053: }
054:
055: /*
056: * Receiving response
057: */
058: protected final boolean hasResponse() throws IOException {
059: if (getResponseCode() == HttpTransportConstants.STATUS_ACCEPTED) {
060: return false;
061: }
062: long contentLength = getResponseContentLength();
063: if (contentLength < 0) {
064: if (responseBuffer == null) {
065: responseBuffer = FileCopyUtils
066: .copyToByteArray(getResponseInputStream());
067: }
068: contentLength = responseBuffer.length;
069: }
070: return contentLength > 0;
071: }
072:
073: protected final InputStream getResponseInputStream()
074: throws IOException {
075: InputStream inputStream;
076: if (responseBuffer != null) {
077: inputStream = new ByteArrayInputStream(responseBuffer);
078: } else {
079: inputStream = getRawResponseInputStream();
080: }
081: return isGzipResponse() ? new GZIPInputStream(inputStream)
082: : inputStream;
083: }
084:
085: /** Determine whether the given response is a GZIP response. */
086: private boolean isGzipResponse() throws IOException {
087: for (Iterator iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_ENCODING); iterator
088: .hasNext();) {
089: String encodingHeader = (String) iterator.next();
090: return encodingHeader.toLowerCase().indexOf(
091: HttpTransportConstants.CONTENT_ENCODING_GZIP) != -1;
092: }
093: return false;
094: }
095:
096: /** Returns the HTTP status code of the response. */
097: protected abstract int getResponseCode() throws IOException;
098:
099: /** Returns the HTTP status message of the response. */
100: protected abstract String getResponseMessage() throws IOException;
101:
102: /** Returns the length of the response. */
103: protected abstract long getResponseContentLength()
104: throws IOException;
105:
106: protected abstract InputStream getRawResponseInputStream()
107: throws IOException;
108:
109: /*
110: * Faults
111: */
112:
113: public final boolean hasFault() throws IOException {
114: return getResponseCode() == HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR;
115: }
116:
117: public final void setFault(boolean fault) {
118: }
119:
120: }
|