1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.json.transformers; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.module.json.filters.IsJsonFilter; |
16 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.StringWriter; |
20 | |
import java.io.UnsupportedEncodingException; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class ObjectToJson extends AbstractJsonTransformer |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | 0 | protected transient final Log logger = LogFactory.getLog(ObjectToJson.class); |
41 | |
|
42 | 0 | private Map<Class<?>, Class<?>> serializationMixins = new HashMap<Class<?>, Class<?>>(); |
43 | |
|
44 | |
protected Class<?> sourceClass; |
45 | |
|
46 | 0 | private boolean handleException = false; |
47 | |
|
48 | 0 | private IsJsonFilter isJsonFilter = new IsJsonFilter(); |
49 | |
|
50 | |
public ObjectToJson() |
51 | 0 | { |
52 | 0 | this.setReturnDataType(DataTypeFactory.JSON_STRING); |
53 | 0 | } |
54 | |
|
55 | |
@Override |
56 | |
public void initialise() throws InitialisationException |
57 | |
{ |
58 | 0 | super.initialise(); |
59 | |
|
60 | |
|
61 | 0 | if (getSourceClass() != null) |
62 | |
{ |
63 | 0 | sourceTypes.clear(); |
64 | 0 | registerSourceType(DataTypeFactory.create(getSourceClass())); |
65 | |
} |
66 | |
|
67 | |
|
68 | 0 | for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet()) |
69 | |
{ |
70 | 0 | getMapper().getSerializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue()); |
71 | |
} |
72 | |
|
73 | 0 | for (Map.Entry<Class<?>, Class<?>> entry : serializationMixins.entrySet()) |
74 | |
{ |
75 | 0 | getMapper().getSerializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue()); |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
@Override |
80 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
81 | |
{ |
82 | 0 | Object src = message.getPayload(); |
83 | 0 | if (src instanceof String && isJsonFilter.accept(src)) |
84 | |
{ |
85 | |
|
86 | 0 | return src; |
87 | |
} |
88 | |
|
89 | |
|
90 | 0 | if (message.getExceptionPayload() != null && this.isHandleException()) |
91 | |
{ |
92 | 0 | if (logger.isDebugEnabled()) |
93 | |
{ |
94 | 0 | logger.debug("Found exception with null payload"); |
95 | |
} |
96 | 0 | src = this.getException(message.getExceptionPayload().getException()); |
97 | |
} |
98 | |
|
99 | 0 | StringWriter writer = new StringWriter(); |
100 | |
try |
101 | |
{ |
102 | 0 | getMapper().writeValue(writer, src); |
103 | |
} |
104 | 0 | catch (IOException e) |
105 | |
{ |
106 | 0 | throw new TransformerException(this, e); |
107 | 0 | } |
108 | |
|
109 | 0 | if (returnType.getType().equals(byte[].class)) |
110 | |
{ |
111 | |
try |
112 | |
{ |
113 | 0 | return writer.toString().getBytes(outputEncoding); |
114 | |
} |
115 | 0 | catch (UnsupportedEncodingException uee) |
116 | |
{ |
117 | 0 | throw new TransformerException(this, uee); |
118 | |
} |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | return writer.toString(); |
123 | |
} |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private Exception getException(Throwable t) |
131 | |
{ |
132 | 0 | Exception returnValue = null; |
133 | 0 | List<Throwable> causeStack = new ArrayList<Throwable>(); |
134 | |
|
135 | 0 | for (Throwable tempCause = t; tempCause != null; tempCause = tempCause.getCause()) |
136 | |
{ |
137 | 0 | causeStack.add(tempCause); |
138 | |
} |
139 | |
|
140 | 0 | for (int i = causeStack.size() - 1; i >= 0; i--) |
141 | |
{ |
142 | 0 | Throwable tempCause = causeStack.get(i); |
143 | |
|
144 | |
|
145 | 0 | if (i == causeStack.size()) |
146 | |
{ |
147 | 0 | returnValue = new Exception(tempCause.getMessage()); |
148 | 0 | returnValue.setStackTrace(tempCause.getStackTrace()); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 0 | returnValue = new Exception(tempCause.getMessage(), returnValue); |
153 | 0 | returnValue.setStackTrace(tempCause.getStackTrace()); |
154 | |
} |
155 | |
} |
156 | |
|
157 | 0 | return returnValue; |
158 | |
} |
159 | |
|
160 | |
public boolean isHandleException() |
161 | |
{ |
162 | 0 | return this.handleException; |
163 | |
} |
164 | |
|
165 | |
public void setHandleException(boolean handleException) |
166 | |
{ |
167 | 0 | this.handleException = handleException; |
168 | 0 | } |
169 | |
|
170 | |
public Class<?> getSourceClass() |
171 | |
{ |
172 | 0 | return sourceClass; |
173 | |
} |
174 | |
|
175 | |
public void setSourceClass(Class<?> sourceClass) |
176 | |
{ |
177 | 0 | this.sourceClass = sourceClass; |
178 | 0 | } |
179 | |
|
180 | |
public Map<Class<?>, Class<?>> getSerializationMixins() |
181 | |
{ |
182 | 0 | return serializationMixins; |
183 | |
} |
184 | |
|
185 | |
public void setSerializationMixins(Map<Class<?>, Class<?>> serializationMixins) |
186 | |
{ |
187 | 0 | this.serializationMixins = serializationMixins; |
188 | 0 | } |
189 | |
} |
190 | |
|