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 public class TransformerPairWireFormat implements WireFormat
33 {
34
35
36
37
38 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 if (inboundTransformer == null)
47 {
48 throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage());
49 }
50 if (inboundTransformer.isSourceTypeSupported(InputStream.class))
51 {
52 return inboundTransformer.transform(in);
53 }
54 else
55 {
56 try
57 {
58 ByteArrayOutputStream baos = new ByteArrayOutputStream();
59 IOUtils.copy(in, baos);
60 return inboundTransformer.transform(baos.toByteArray());
61 }
62 catch (IOException e)
63 {
64 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 if (outboundTransformer == null)
73 {
74 throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage());
75 }
76 try
77 {
78 Class returnClass = outboundTransformer.getReturnClass();
79 if (returnClass == null)
80 {
81 logger.warn("No return class was set on transformer: " + outboundTransformer
82 + ". Attempting to work out how to treat the result transformation");
83
84 Object result = outboundTransformer.transform(o);
85
86 byte[] bytes;
87 if (result instanceof byte[])
88 {
89 bytes = (byte[]) result;
90 }
91 else
92 {
93 bytes = result.toString().getBytes(encoding);
94 }
95
96 out.write(bytes);
97 }
98 else if (returnClass.equals(byte[].class))
99 {
100 byte[] b = (byte[]) outboundTransformer.transform(o);
101 out.write(b);
102 }
103 else if (returnClass.equals(String.class))
104 {
105 String s = (String) outboundTransformer.transform(o);
106 out.write(s.getBytes(encoding));
107 }
108 else
109 {
110 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
111 }
112 }
113 catch (IOException e)
114 {
115 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
116 }
117 }
118
119 public Transformer getInboundTransformer()
120 {
121 return inboundTransformer;
122 }
123
124 public void setInboundTransformer(Transformer inboundTransformer)
125 {
126 this.inboundTransformer = inboundTransformer;
127 }
128
129 public Transformer getOutboundTransformer()
130 {
131 return outboundTransformer;
132 }
133
134 public void setOutboundTransformer(Transformer outboundTransformer)
135 {
136 this.outboundTransformer = outboundTransformer;
137 }
138
139 public void setTransferObjectClass(Class clazz)
140 {
141 transferObjectClass = clazz;
142 }
143 }