1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport; |
12 | |
|
13 | |
import org.mule.api.ThreadSafeAccess; |
14 | |
import org.mule.api.transport.MessageTypeNotSupportedException; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.io.StringWriter; |
18 | |
import java.io.Writer; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class WriterMessageAdapter extends AbstractMessageAdapter |
25 | |
{ |
26 | |
|
27 | |
|
28 | |
|
29 | |
private static final long serialVersionUID = -1065602752454818625L; |
30 | |
|
31 | |
private final StringWriter writer; |
32 | |
|
33 | |
public WriterMessageAdapter(Object message) throws MessageTypeNotSupportedException |
34 | 0 | { |
35 | 0 | if (message instanceof String) |
36 | |
{ |
37 | 0 | writer = new StringWriter(); |
38 | 0 | writer.write((String) message); |
39 | |
} |
40 | 0 | else if (message instanceof StringWriter) |
41 | |
{ |
42 | 0 | this.writer = (StringWriter) message; |
43 | |
} |
44 | |
else |
45 | |
{ |
46 | 0 | throw new MessageTypeNotSupportedException(message, getClass()); |
47 | |
} |
48 | 0 | } |
49 | |
|
50 | |
protected WriterMessageAdapter(WriterMessageAdapter template) |
51 | |
{ |
52 | 0 | super(template); |
53 | 0 | writer = template.writer; |
54 | 0 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public String getPayloadAsString(String encoding) throws Exception |
65 | |
{ |
66 | 0 | return writer.toString(); |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public byte[] getPayloadAsBytes() throws Exception |
76 | |
{ |
77 | 0 | return writer.toString().getBytes(); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public Object getPayload() |
84 | |
{ |
85 | 0 | return writer.toString(); |
86 | |
} |
87 | |
|
88 | |
public void write(String string) |
89 | |
{ |
90 | 0 | writer.write(string); |
91 | 0 | } |
92 | |
|
93 | |
public void write(String string, int offset, int len) |
94 | |
{ |
95 | 0 | writer.write(string, offset, len); |
96 | 0 | } |
97 | |
|
98 | |
public Writer getWriter() |
99 | |
{ |
100 | 0 | return writer; |
101 | |
} |
102 | |
|
103 | |
public void flush() |
104 | |
{ |
105 | 0 | writer.flush(); |
106 | 0 | } |
107 | |
|
108 | |
public void close() throws IOException |
109 | |
{ |
110 | 0 | writer.close(); |
111 | 0 | } |
112 | |
|
113 | |
public ThreadSafeAccess newThreadCopy() |
114 | |
{ |
115 | 0 | return new WriterMessageAdapter(this); |
116 | |
} |
117 | |
|
118 | |
} |