1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.wire; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.DefaultMuleException; |
15 | |
import org.mule.api.transformer.Transformer; |
16 | |
import org.mule.api.transformer.TransformerException; |
17 | |
import org.mule.api.transformer.wire.WireFormat; |
18 | |
import org.mule.config.i18n.CoreMessages; |
19 | |
import org.mule.util.IOUtils; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.OutputStream; |
24 | |
|
25 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 24 | public class TransformerPairWireFormat implements WireFormat |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 24 | protected transient Log logger = LogFactory.getLog(getClass()); |
39 | |
|
40 | |
protected Transformer inboundTransformer; |
41 | |
protected Transformer outboundTransformer; |
42 | |
protected Class transferObjectClass; |
43 | |
|
44 | |
public Object read(InputStream in) throws MuleException |
45 | |
{ |
46 | 6 | if (inboundTransformer == null) |
47 | |
{ |
48 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage()); |
49 | |
} |
50 | 6 | if (inboundTransformer.isSourceTypeSupported(InputStream.class)) |
51 | |
{ |
52 | 6 | return inboundTransformer.transform(in); |
53 | |
} |
54 | |
else |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
59 | 0 | IOUtils.copy(in, baos); |
60 | 0 | return inboundTransformer.transform(baos.toByteArray()); |
61 | |
} |
62 | 0 | catch (IOException e) |
63 | |
{ |
64 | 0 | throw new DefaultMuleException(CoreMessages.failedToReadPayload(), e); |
65 | |
} |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
public void write(OutputStream out, Object o, String encoding) throws MuleException |
70 | |
{ |
71 | |
|
72 | 8 | if (outboundTransformer == null) |
73 | |
{ |
74 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage()); |
75 | |
} |
76 | |
try |
77 | |
{ |
78 | 8 | Class returnClass = outboundTransformer.getReturnClass(); |
79 | 8 | if (returnClass == null) |
80 | |
{ |
81 | 0 | logger.warn("No return class was set on transformer: " + outboundTransformer |
82 | |
+ ". Attempting to work out how to treat the result transformation"); |
83 | |
|
84 | 0 | Object result = outboundTransformer.transform(o); |
85 | |
|
86 | |
byte[] bytes; |
87 | 0 | if (result instanceof byte[]) |
88 | |
{ |
89 | 0 | bytes = (byte[]) result; |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | bytes = result.toString().getBytes(encoding); |
94 | |
} |
95 | |
|
96 | 0 | out.write(bytes); |
97 | 0 | } |
98 | 8 | else if (returnClass.equals(byte[].class)) |
99 | |
{ |
100 | 8 | byte[] b = (byte[]) outboundTransformer.transform(o); |
101 | 6 | out.write(b); |
102 | 6 | } |
103 | 0 | else if (returnClass.equals(String.class)) |
104 | |
{ |
105 | 0 | String s = (String) outboundTransformer.transform(o); |
106 | 0 | out.write(s.getBytes(encoding)); |
107 | 0 | } |
108 | |
else |
109 | |
{ |
110 | 0 | throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass())); |
111 | |
} |
112 | |
} |
113 | 0 | catch (IOException e) |
114 | |
{ |
115 | 0 | throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e); |
116 | 6 | } |
117 | 6 | } |
118 | |
|
119 | |
public Transformer getInboundTransformer() |
120 | |
{ |
121 | 12 | return inboundTransformer; |
122 | |
} |
123 | |
|
124 | |
public void setInboundTransformer(Transformer inboundTransformer) |
125 | |
{ |
126 | 32 | this.inboundTransformer = inboundTransformer; |
127 | 32 | } |
128 | |
|
129 | |
public Transformer getOutboundTransformer() |
130 | |
{ |
131 | 4 | return outboundTransformer; |
132 | |
} |
133 | |
|
134 | |
public void setOutboundTransformer(Transformer outboundTransformer) |
135 | |
{ |
136 | 24 | this.outboundTransformer = outboundTransformer; |
137 | 24 | } |
138 | |
|
139 | |
public void setTransferObjectClass(Class clazz) |
140 | |
{ |
141 | 0 | transferObjectClass = clazz; |
142 | 0 | } |
143 | |
} |