1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.simple; |
12 | |
|
13 | |
import org.mule.RequestContext; |
14 | |
import org.mule.api.transformer.DiscoverableTransformer; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.api.transport.OutputHandler; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.transformer.AbstractTransformer; |
19 | |
import org.mule.transformer.types.DataTypeFactory; |
20 | |
import org.mule.util.IOUtils; |
21 | |
import org.mule.util.StringMessageUtils; |
22 | |
|
23 | |
import java.io.ByteArrayOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.UnsupportedEncodingException; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class ObjectToString extends AbstractTransformer implements DiscoverableTransformer |
34 | |
{ |
35 | |
protected static final int DEFAULT_BUFFER_SIZE = 80; |
36 | |
|
37 | |
|
38 | 0 | private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1; |
39 | |
|
40 | |
public ObjectToString() |
41 | 0 | { |
42 | 0 | registerSourceType(DataTypeFactory.OBJECT); |
43 | 0 | registerSourceType(DataTypeFactory.BYTE_ARRAY); |
44 | 0 | registerSourceType(DataTypeFactory.INPUT_STREAM); |
45 | 0 | registerSourceType(DataTypeFactory.create(OutputHandler.class)); |
46 | |
|
47 | |
|
48 | 0 | setReturnDataType(DataTypeFactory.TEXT_STRING); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
53 | |
{ |
54 | 0 | String output = ""; |
55 | |
|
56 | 0 | if (src instanceof InputStream) |
57 | |
{ |
58 | 0 | InputStream is = (InputStream) src; |
59 | |
try |
60 | |
{ |
61 | 0 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
62 | 0 | IOUtils.copy(is, byteOut); |
63 | 0 | output = new String(byteOut.toByteArray(), encoding); |
64 | |
} |
65 | 0 | catch (IOException e) |
66 | |
{ |
67 | 0 | throw new TransformerException(CoreMessages.errorReadingStream(), e); |
68 | |
} |
69 | |
finally |
70 | |
{ |
71 | 0 | try |
72 | |
{ |
73 | 0 | is.close(); |
74 | |
} |
75 | 0 | catch (IOException e) |
76 | |
{ |
77 | 0 | logger.warn("Could not close stream", e); |
78 | 0 | } |
79 | 0 | } |
80 | 0 | } |
81 | 0 | else if (src instanceof OutputHandler) |
82 | |
{ |
83 | 0 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
84 | |
|
85 | |
try |
86 | |
{ |
87 | 0 | ((OutputHandler) src).write(RequestContext.getEvent(), bytes); |
88 | |
|
89 | 0 | output = new String(bytes.toByteArray(), encoding); |
90 | |
} |
91 | 0 | catch (IOException e) |
92 | |
{ |
93 | 0 | throw new TransformerException(this, e); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | 0 | } |
98 | 0 | else if (src instanceof byte[]) |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | output = new String((byte[]) src, encoding); |
103 | |
} |
104 | 0 | catch (UnsupportedEncodingException e) |
105 | |
{ |
106 | 0 | throw new TransformerException(this, e); |
107 | 0 | } |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | output = StringMessageUtils.toString(src); |
112 | |
} |
113 | |
|
114 | 0 | return output; |
115 | |
} |
116 | |
|
117 | |
public int getPriorityWeighting() |
118 | |
{ |
119 | 0 | return priorityWeighting; |
120 | |
} |
121 | |
|
122 | |
public void setPriorityWeighting(int priorityWeighting) |
123 | |
{ |
124 | 0 | this.priorityWeighting = priorityWeighting; |
125 | 0 | } |
126 | |
} |