1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transformer.simple; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.transformer.DiscoverableTransformer; |
11 | |
import org.mule.api.transformer.TransformerException; |
12 | |
import org.mule.api.transport.OutputHandler; |
13 | |
import org.mule.config.i18n.MessageFactory; |
14 | |
import org.mule.transformer.AbstractTransformer; |
15 | |
import org.mule.transformer.types.DataTypeFactory; |
16 | |
import org.mule.util.IOUtils; |
17 | |
import org.mule.util.SerializationUtils; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.OutputStream; |
22 | |
import java.io.Serializable; |
23 | |
|
24 | |
|
25 | |
public class ObjectToOutputHandler extends AbstractTransformer implements DiscoverableTransformer |
26 | |
{ |
27 | |
|
28 | |
|
29 | 0 | private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1; |
30 | |
|
31 | |
public ObjectToOutputHandler() |
32 | 0 | { |
33 | 0 | registerSourceType(DataTypeFactory.BYTE_ARRAY); |
34 | 0 | registerSourceType(DataTypeFactory.STRING); |
35 | 0 | registerSourceType(DataTypeFactory.INPUT_STREAM); |
36 | 0 | registerSourceType(DataTypeFactory.create(Serializable.class)); |
37 | 0 | setReturnDataType(DataTypeFactory.create(OutputHandler.class)); |
38 | 0 | } |
39 | |
|
40 | |
@Override |
41 | |
public Object doTransform(final Object src, final String encoding) throws TransformerException |
42 | |
{ |
43 | 0 | if (src instanceof String) |
44 | |
{ |
45 | 0 | return new OutputHandler() |
46 | 0 | { |
47 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
48 | |
{ |
49 | 0 | out.write(((String) src).getBytes(encoding)); |
50 | 0 | } |
51 | |
}; |
52 | |
} |
53 | 0 | else if (src instanceof byte[]) |
54 | |
{ |
55 | 0 | return new OutputHandler() |
56 | 0 | { |
57 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
58 | |
{ |
59 | 0 | out.write((byte[]) src); |
60 | 0 | } |
61 | |
}; |
62 | |
} |
63 | 0 | else if (src instanceof InputStream) |
64 | |
{ |
65 | 0 | return new OutputHandler() |
66 | 0 | { |
67 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
68 | |
{ |
69 | 0 | InputStream is = (InputStream) src; |
70 | |
try |
71 | |
{ |
72 | 0 | IOUtils.copyLarge(is, out); |
73 | |
} |
74 | |
finally |
75 | |
{ |
76 | 0 | is.close(); |
77 | 0 | } |
78 | 0 | } |
79 | |
}; |
80 | |
} |
81 | 0 | else if (src instanceof Serializable) |
82 | |
{ |
83 | 0 | return new OutputHandler() |
84 | 0 | { |
85 | |
public void write(MuleEvent event, OutputStream out) throws IOException |
86 | |
{ |
87 | 0 | SerializationUtils.serialize((Serializable) src, out); |
88 | 0 | } |
89 | |
}; |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | throw new TransformerException(MessageFactory |
94 | |
.createStaticMessage("Unable to convert " + src.getClass() + " to OutputHandler.")); |
95 | |
} |
96 | |
} |
97 | |
|
98 | |
public int getPriorityWeighting() |
99 | |
{ |
100 | 0 | return priorityWeighting; |
101 | |
} |
102 | |
|
103 | |
public void setPriorityWeighting(int priorityWeighting) |
104 | |
{ |
105 | 0 | this.priorityWeighting = priorityWeighting; |
106 | 0 | } |
107 | |
} |