1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.ajax; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.DefaultMuleException; |
15 | |
import org.mule.api.MuleContext; |
16 | |
import org.mule.api.transport.MuleMessageFactory; |
17 | |
import org.mule.api.transport.PropertyScope; |
18 | |
import org.mule.module.json.JsonData; |
19 | |
import org.mule.module.json.filters.IsJsonFilter; |
20 | |
import org.mule.transport.AbstractMuleMessageFactory; |
21 | |
import org.mule.transport.ajax.embedded.AjaxConnector; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.cometd.Bayeux; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class AjaxMuleMessageFactory extends AbstractMuleMessageFactory |
39 | |
{ |
40 | 0 | protected transient IsJsonFilter filter = new IsJsonFilter(); |
41 | |
|
42 | |
public AjaxMuleMessageFactory(MuleContext context) |
43 | |
{ |
44 | 0 | super(context); |
45 | 0 | } |
46 | |
|
47 | |
@Override |
48 | |
protected Class<?>[] getSupportedTransportMessageTypes() |
49 | |
{ |
50 | 0 | return new Class[] { Object.class }; |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
protected Object extractPayload(Object transportMessage, String encoding) throws Exception |
55 | |
{ |
56 | 0 | if (transportMessage instanceof Map<?, ?>) |
57 | |
{ |
58 | 0 | return extractPayloadFromMap((Map<?, ?>) transportMessage); |
59 | |
} |
60 | 0 | else if (filter.accept(transportMessage)) |
61 | |
{ |
62 | 0 | return extractJsonPayload(transportMessage); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | return transportMessage; |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
protected void addProperties(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception |
72 | |
{ |
73 | 0 | if (transportMessage instanceof Map<?, ?>) |
74 | |
{ |
75 | 0 | addPropertiesFromMap(muleMessage, (Map<?, ?>) transportMessage); |
76 | |
} |
77 | 0 | else if (filter.accept(transportMessage)) |
78 | |
{ |
79 | 0 | addPropertiesToFromJsonData(muleMessage, transportMessage); |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
private Object extractPayloadFromMap(Map<?, ?> map) |
84 | |
{ |
85 | 0 | Object data = map.remove(Bayeux.DATA_FIELD); |
86 | 0 | if (data == null) |
87 | |
{ |
88 | 0 | throw new IllegalArgumentException(Bayeux.DATA_FIELD + " parameter not set in payload map"); |
89 | |
} |
90 | 0 | return data; |
91 | |
} |
92 | |
|
93 | |
private Object extractJsonPayload(Object transportMessage) throws DefaultMuleException |
94 | |
{ |
95 | 0 | String transportMessageString = transportMessage.toString(); |
96 | 0 | if (transportMessageString.indexOf(Bayeux.DATA_FIELD) > -1) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | JsonData data = new JsonData(transportMessageString); |
101 | 0 | return data.get(Bayeux.DATA_FIELD).toString(); |
102 | |
} |
103 | 0 | catch (IOException e) |
104 | |
{ |
105 | 0 | throw new DefaultMuleException(e); |
106 | |
} |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 0 | return transportMessage; |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
@SuppressWarnings("unchecked") |
115 | |
private void addPropertiesFromMap(DefaultMuleMessage muleMessage, Map<?, ?> map) |
116 | |
{ |
117 | 0 | Object replyTo = map.remove(AjaxConnector.REPLYTO_PARAM); |
118 | 0 | muleMessage.setReplyTo(replyTo); |
119 | |
|
120 | |
|
121 | 0 | map.remove(Bayeux.DATA_FIELD); |
122 | |
|
123 | |
|
124 | 0 | Map<String, Object> messageProperties = (Map<String, Object>) map; |
125 | 0 | muleMessage.addProperties(messageProperties, PropertyScope.INVOCATION); |
126 | 0 | } |
127 | |
|
128 | |
private void addPropertiesToFromJsonData(DefaultMuleMessage muleMessage, Object transportMessage) throws IOException |
129 | |
{ |
130 | 0 | JsonData data = new JsonData(transportMessage.toString()); |
131 | 0 | if (data.hasNode(AjaxConnector.REPLYTO_PARAM)) |
132 | |
{ |
133 | 0 | muleMessage.setReplyTo(data.getAsString(AjaxConnector.REPLYTO_PARAM)); |
134 | |
} |
135 | 0 | } |
136 | |
} |