1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.OutputStream;
23 import java.io.PrintWriter;
24 import java.io.Reader;
25 import java.io.Writer;
26 import javax.xml.transform.Result;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.sax.SAXSource;
32 import javax.xml.transform.stream.StreamResult;
33
34 import org.springframework.core.io.InputStreamSource;
35 import org.springframework.core.io.Resource;
36 import org.springframework.util.FileCopyUtils;
37 import org.springframework.xml.sax.SaxUtils;
38 import org.springframework.xml.transform.StringSource;
39
40
41
42
43
44
45
46 public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
47
48 private StringBuffer content;
49
50 private boolean fault = false;
51
52 private String faultReason;
53
54 public MockWebServiceMessage() {
55 }
56
57 public MockWebServiceMessage(Source source) throws TransformerException {
58 TransformerFactory transformerFactory = TransformerFactory.newInstance();
59 Transformer transformer = transformerFactory.newTransformer();
60 content = new StringBuffer();
61 transformer.transform(source, getPayloadResult());
62 }
63
64 public MockWebServiceMessage(Resource resource) throws IOException, TransformerException {
65 this(new SAXSource(SaxUtils.createInputSource(resource)));
66 }
67
68 public MockWebServiceMessage(StringBuffer content) {
69 this.content = content;
70 }
71
72 public MockWebServiceMessage(String content) {
73 if (content != null) {
74 this.content = new StringBuffer(content);
75 }
76 }
77
78 public String getPayloadAsString() {
79 return content != null ? content.toString() : null;
80 }
81
82 public void setPayload(InputStreamSource inputStreamSource) throws IOException {
83 checkContent();
84 InputStream is = null;
85 try {
86 is = inputStreamSource.getInputStream();
87 Reader reader = new InputStreamReader(is, "UTF-8");
88 content.replace(0, content.length(), FileCopyUtils.copyToString(reader));
89 }
90 finally {
91 if (is != null) {
92 is.close();
93 }
94 }
95 }
96
97 public void setPayload(String content) {
98 checkContent();
99 this.content.replace(0, this.content.length(), content);
100 }
101
102 private void checkContent() {
103 if (content == null) {
104 content = new StringBuffer();
105 }
106 }
107
108 public Result getPayloadResult() {
109 checkContent();
110 content.setLength(0);
111 return new StreamResult(new StringBufferWriter());
112 }
113
114 public Source getPayloadSource() {
115 return content != null ? new StringSource(content.toString()) : null;
116 }
117
118 public boolean hasFault() {
119 return fault;
120 }
121
122 public void setFault(boolean fault) {
123 this.fault = fault;
124 }
125
126 public String getFaultReason() {
127 return faultReason;
128 }
129
130 public void setFaultReason(String faultReason) {
131 this.faultReason = faultReason;
132 }
133
134 public void writeTo(OutputStream outputStream) throws IOException {
135 if (content != null) {
136 PrintWriter writer = new PrintWriter(outputStream);
137 writer.write(content.toString());
138 }
139 }
140
141 public String toString() {
142 StringBuffer buffer = new StringBuffer("MockWebServiceMessage {");
143 if (content != null) {
144 buffer.append(content);
145 }
146 buffer.append('}');
147 return buffer.toString();
148 }
149
150 private class StringBufferWriter extends Writer {
151
152 private StringBufferWriter() {
153 super(content);
154 }
155
156 public void write(String str) {
157 content.append(str);
158 }
159
160 public void write(int c) {
161 content.append((char) c);
162 }
163
164 public void write(String str, int off, int len) {
165 content.append(str.substring(off, off + len));
166 }
167
168 public void close() throws IOException {
169 }
170
171 public void flush() {
172 }
173
174 public void write(char cbuf[], int off, int len) {
175 if (off < 0 || off > cbuf.length || len < 0 || off + len > cbuf.length || off + len < 0) {
176 throw new IndexOutOfBoundsException();
177 }
178 else if (len == 0) {
179 return;
180 }
181 content.append(cbuf, off, len);
182 }
183 }
184 }