View Javadoc

1   /*
2    * Copyright 2007 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.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Iterator;
23  import java.util.zip.GZIPInputStream;
24  
25  import org.springframework.util.FileCopyUtils;
26  import org.springframework.ws.transport.AbstractSenderConnection;
27  import org.springframework.ws.transport.FaultAwareWebServiceConnection;
28  import org.springframework.ws.transport.WebServiceConnection;
29  
30  /**
31   * Abstract base class for {@link WebServiceConnection} implementations that send request over HTTP.
32   *
33   * @author Arjen Poutsma
34   * @since 1.0.0
35   */
36  public abstract class AbstractHttpSenderConnection extends AbstractSenderConnection
37          implements FaultAwareWebServiceConnection {
38  
39      /** Buffer used for reading the response, when the content length is invalid. */
40      private byte[] responseBuffer;
41  
42      public final boolean hasError() throws IOException {
43          return getResponseCode() / 100 != 2;
44      }
45  
46      public final String getErrorMessage() throws IOException {
47          StringBuffer buffer = new StringBuffer(getResponseMessage());
48          buffer.append(" [");
49          buffer.append(getResponseCode());
50          buffer.append(']');
51          return buffer.toString();
52      }
53  
54      /*
55       * Receiving response
56       */
57      protected final boolean hasResponse() throws IOException {
58          if (HttpTransportConstants.STATUS_ACCEPTED == getResponseCode()) {
59              return false;
60          }
61          long contentLength = getResponseContentLength();
62          if (contentLength < 0) {
63              if (responseBuffer == null) {
64                  responseBuffer = FileCopyUtils.copyToByteArray(getRawResponseInputStream());
65              }
66              contentLength = responseBuffer.length;
67          }
68          return contentLength > 0;
69      }
70  
71      protected final InputStream getResponseInputStream() throws IOException {
72          InputStream inputStream;
73          if (responseBuffer != null) {
74              inputStream = new ByteArrayInputStream(responseBuffer);
75          }
76          else {
77              inputStream = getRawResponseInputStream();
78          }
79          return isGzipResponse() ? new GZIPInputStream(inputStream) : inputStream;
80      }
81  
82      /** Determine whether the response is a GZIP response. */
83      private boolean isGzipResponse() throws IOException {
84          Iterator iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_ENCODING);
85          if (iterator.hasNext()) {
86              String encodingHeader = (String) iterator.next();
87              return encodingHeader.toLowerCase().indexOf(HttpTransportConstants.CONTENT_ENCODING_GZIP) != -1;
88          }
89          return false;
90      }
91  
92      /** Returns the HTTP status code of the response. */
93      protected abstract int getResponseCode() throws IOException;
94  
95      /** Returns the HTTP status message of the response. */
96      protected abstract String getResponseMessage() throws IOException;
97  
98      /** Returns the length of the response. */
99      protected abstract long getResponseContentLength() throws IOException;
100 
101     /** Returns the raw, possibly compressed input stream to read the response from. */
102     protected abstract InputStream getRawResponseInputStream() throws IOException;
103 
104     /*
105      * Faults
106      */
107 
108     public final boolean hasFault() throws IOException {
109         return HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR == getResponseCode() && isXmlResponse();
110     }
111 
112     /** Determine whether the response is a XML message. */
113     private boolean isXmlResponse() throws IOException {
114         Iterator iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_TYPE);
115         if (iterator.hasNext()) {
116             String contentType = ((String) iterator.next()).toLowerCase();
117             return contentType.indexOf("xml") != -1;
118         }
119         return false;
120     }
121 
122     public final void setFault(boolean fault) {
123     }
124 
125 }