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