1
2
3
4
5
6
7
8
9
10
11 package org.mule.transformers.wire;
12
13 import org.mule.MuleException;
14 import org.mule.MuleManager;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.umo.UMOException;
17 import org.mule.umo.transformer.TransformerException;
18 import org.mule.umo.transformer.UMOTransformer;
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 UMOTransformer inboundTransformer;
41 protected UMOTransformer outboundTransformer;
42
43 public Object read(InputStream in) throws UMOException
44 {
45 if (inboundTransformer == null)
46 {
47 throw new IllegalArgumentException(CoreMessages.objectIsNull("inboundTransformer").getMessage());
48 }
49 if (inboundTransformer.isSourceTypeSupported(InputStream.class))
50 {
51 return inboundTransformer.transform(in);
52 }
53 else
54 {
55 try
56 {
57 ByteArrayOutputStream baos = new ByteArrayOutputStream();
58 IOUtils.copy(in, baos);
59 return inboundTransformer.transform(baos.toByteArray());
60 }
61 catch (IOException e)
62 {
63 throw new MuleException(CoreMessages.failedToReadPayload(), e);
64 }
65 }
66 }
67
68 public void write(OutputStream out, Object o) throws UMOException
69 {
70 if (outboundTransformer == null)
71 {
72 throw new IllegalArgumentException(CoreMessages.objectIsNull("outboundTransformer").getMessage());
73 }
74 try
75 {
76 Class returnClass = outboundTransformer.getReturnClass();
77 if (returnClass == null)
78 {
79 logger.warn("No return class was set on transformer: " + outboundTransformer
80 + ". Attempting to work out how to treat the result transformation");
81
82 Object result = outboundTransformer.transform(o);
83
84 byte[] bytes;
85 if (result instanceof byte[])
86 {
87 bytes = (byte[]) result;
88 }
89 else
90 {
91 bytes = result.toString().getBytes(MuleManager.getConfiguration().getEncoding());
92 }
93
94 out.write(bytes);
95 }
96 else if (returnClass.equals(byte[].class))
97 {
98 byte[] b = (byte[]) outboundTransformer.transform(o);
99 out.write(b);
100 }
101 else if (returnClass.equals(String.class))
102 {
103 String s = (String) outboundTransformer.transform(o);
104 out.write(s.getBytes(MuleManager.getConfiguration().getEncoding()));
105 }
106 else
107 {
108 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
109 }
110 }
111 catch (IOException e)
112 {
113 throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
114 }
115 }
116
117 public UMOTransformer getInboundTransformer()
118 {
119 return inboundTransformer;
120 }
121
122 public void setInboundTransformer(UMOTransformer inboundTransformer)
123 {
124 this.inboundTransformer = inboundTransformer;
125 }
126
127 public UMOTransformer getOutboundTransformer()
128 {
129 return outboundTransformer;
130 }
131
132 public void setOutboundTransformer(UMOTransformer outboundTransformer)
133 {
134 this.outboundTransformer = outboundTransformer;
135 }
136
137 }