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.ws.transport.AbstractSenderConnection;
27 import org.springframework.ws.transport.FaultAwareWebServiceConnection;
28 import org.springframework.ws.transport.WebServiceConnection;
29
30
31
32
33
34
35
36 public abstract class AbstractHttpSenderConnection extends AbstractSenderConnection
37 implements FaultAwareWebServiceConnection {
38
39
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
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
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
93 protected abstract int getResponseCode() throws IOException;
94
95
96 protected abstract String getResponseMessage() throws IOException;
97
98
99 protected abstract long getResponseContentLength() throws IOException;
100
101
102 protected abstract InputStream getRawResponseInputStream() throws IOException;
103
104
105
106
107
108 public final boolean hasFault() throws IOException {
109 return HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR == getResponseCode() && isXmlResponse();
110 }
111
112
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 }