1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.util.StringUtils;
27 import org.springframework.ws.transport.AbstractSenderConnection;
28 import org.springframework.ws.transport.FaultAwareWebServiceConnection;
29 import org.springframework.ws.transport.WebServiceConnection;
30
31
32
33
34
35
36
37 public abstract class AbstractHttpSenderConnection extends AbstractSenderConnection
38 implements FaultAwareWebServiceConnection {
39
40
41 private byte[] responseBuffer;
42
43 public final boolean hasError() throws IOException {
44 return getResponseCode() / 100 != 2;
45 }
46
47 public final String getErrorMessage() throws IOException {
48 StringBuffer buffer = new StringBuffer();
49 String responseMessage = getResponseMessage();
50 if (StringUtils.hasLength(responseMessage)) {
51 buffer.append(responseMessage);
52 }
53 buffer.append(" [");
54 buffer.append(getResponseCode());
55 buffer.append(']');
56 return buffer.toString();
57 }
58
59
60
61
62 protected final boolean hasResponse() throws IOException {
63 int responseCode = getResponseCode();
64 if (HttpTransportConstants.STATUS_ACCEPTED == responseCode ||
65 HttpTransportConstants.STATUS_NO_CONTENT == responseCode) {
66 return false;
67 }
68 long contentLength = getResponseContentLength();
69 if (contentLength < 0) {
70 if (responseBuffer == null) {
71 responseBuffer = FileCopyUtils.copyToByteArray(getRawResponseInputStream());
72 }
73 contentLength = responseBuffer.length;
74 }
75 return contentLength > 0;
76 }
77
78 protected final InputStream getResponseInputStream() throws IOException {
79 InputStream inputStream;
80 if (responseBuffer != null) {
81 inputStream = new ByteArrayInputStream(responseBuffer);
82 }
83 else {
84 inputStream = getRawResponseInputStream();
85 }
86 return isGzipResponse() ? new GZIPInputStream(inputStream) : inputStream;
87 }
88
89
90 private boolean isGzipResponse() throws IOException {
91 Iterator iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_ENCODING);
92 if (iterator.hasNext()) {
93 String encodingHeader = (String) iterator.next();
94 return encodingHeader.toLowerCase().indexOf(HttpTransportConstants.CONTENT_ENCODING_GZIP) != -1;
95 }
96 return false;
97 }
98
99
100 protected abstract int getResponseCode() throws IOException;
101
102
103 protected abstract String getResponseMessage() throws IOException;
104
105
106 protected abstract long getResponseContentLength() throws IOException;
107
108
109 protected abstract InputStream getRawResponseInputStream() throws IOException;
110
111
112
113
114
115 public final boolean hasFault() throws IOException {
116 return HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR == getResponseCode() && isXmlResponse();
117 }
118
119
120 private boolean isXmlResponse() throws IOException {
121 Iterator iterator = getResponseHeaders(HttpTransportConstants.HEADER_CONTENT_TYPE);
122 if (iterator.hasNext()) {
123 String contentType = ((String) iterator.next()).toLowerCase();
124 return contentType.indexOf("xml") != -1;
125 }
126 return false;
127 }
128
129 public final void setFault(boolean fault) {
130 }
131
132 }