001: /*
002: * Copyright 2006 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;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.OutputStream;
023: import java.io.PrintWriter;
024: import java.io.Reader;
025: import java.io.Writer;
026: import javax.xml.transform.Result;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.TransformerFactory;
031: import javax.xml.transform.sax.SAXSource;
032: import javax.xml.transform.stream.StreamResult;
033:
034: import org.springframework.core.io.InputStreamSource;
035: import org.springframework.core.io.Resource;
036: import org.springframework.util.FileCopyUtils;
037: import org.springframework.xml.sax.SaxUtils;
038: import org.springframework.xml.transform.StringSource;
039:
040: /**
041: * Mock implementation of the <code>WebServiceMessage</code> interface.
042: *
043: * @author Arjen Poutsma
044: * @since 1.0.0
045: */
046: public class MockWebServiceMessage implements
047: FaultAwareWebServiceMessage {
048:
049: private final StringBuffer content;
050:
051: private boolean fault = false;
052:
053: private String faultReason;
054:
055: public MockWebServiceMessage() {
056: content = new StringBuffer();
057: }
058:
059: public MockWebServiceMessage(Source source)
060: throws TransformerException {
061: TransformerFactory transformerFactory = TransformerFactory
062: .newInstance();
063: Transformer transformer = transformerFactory.newTransformer();
064: content = new StringBuffer();
065: transformer.transform(source, getPayloadResult());
066: }
067:
068: public MockWebServiceMessage(Resource resource) throws IOException,
069: TransformerException {
070: this (new SAXSource(SaxUtils.createInputSource(resource)));
071: }
072:
073: public MockWebServiceMessage(StringBuffer content) {
074: this .content = content;
075: }
076:
077: public MockWebServiceMessage(String content) {
078: this .content = new StringBuffer(content);
079: }
080:
081: public String getPayloadAsString() {
082: return content.toString();
083: }
084:
085: public void setPayload(InputStreamSource inputStreamSource)
086: throws IOException {
087: InputStream is = null;
088: try {
089: is = inputStreamSource.getInputStream();
090: Reader reader = new InputStreamReader(is, "UTF-8");
091: content.replace(0, content.length(), FileCopyUtils
092: .copyToString(reader));
093: } finally {
094: if (is != null) {
095: is.close();
096: }
097: }
098: }
099:
100: public void setPayload(String content) {
101: this .content.replace(0, this .content.length(), content);
102: }
103:
104: public Result getPayloadResult() {
105: content.setLength(0);
106: return new StreamResult(new StringBufferWriter());
107: }
108:
109: public Source getPayloadSource() {
110: return new StringSource(content.toString());
111: }
112:
113: public boolean hasFault() {
114: return fault;
115: }
116:
117: public void setFault(boolean fault) {
118: this .fault = fault;
119: }
120:
121: public String getFaultReason() {
122: return faultReason;
123: }
124:
125: public void setFaultReason(String faultReason) {
126: this .faultReason = faultReason;
127: }
128:
129: public void writeTo(OutputStream outputStream) throws IOException {
130: PrintWriter writer = new PrintWriter(outputStream);
131: writer.write(content.toString());
132: }
133:
134: public String toString() {
135: StringBuffer buffer = new StringBuffer(
136: "MockWebServiceMessage {");
137: buffer.append(content);
138: buffer.append('}');
139: return buffer.toString();
140: }
141:
142: private class StringBufferWriter extends Writer {
143:
144: private StringBufferWriter() {
145: super (content);
146: }
147:
148: public void write(String str) {
149: content.append(str);
150: }
151:
152: public void write(int c) {
153: content.append((char) c);
154: }
155:
156: public void write(String str, int off, int len) {
157: content.append(str.substring(off, off + len));
158: }
159:
160: public void close() throws IOException {
161: }
162:
163: public void flush() {
164: }
165:
166: public void write(char cbuf[], int off, int len) {
167: if (off < 0 || off > cbuf.length || len < 0
168: || off + len > cbuf.length || off + len < 0) {
169: throw new IndexOutOfBoundsException();
170: } else if (len == 0) {
171: return;
172: }
173: content.append(cbuf, off, len);
174: }
175: }
176: }
|