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 protected transient IsJsonFilter filter = new IsJsonFilter();
41
42 public AjaxMuleMessageFactory(MuleContext context)
43 {
44 super(context);
45 }
46
47 @Override
48 protected Class<?>[] getSupportedTransportMessageTypes()
49 {
50 return new Class[] { Object.class };
51 }
52
53 @Override
54 protected Object extractPayload(Object transportMessage, String encoding) throws Exception
55 {
56 if (transportMessage instanceof Map<?, ?>)
57 {
58 return extractPayloadFromMap((Map<?, ?>) transportMessage);
59 }
60 else if (filter.accept(transportMessage))
61 {
62 return extractJsonPayload(transportMessage);
63 }
64 else
65 {
66 return transportMessage;
67 }
68 }
69
70 @Override
71 protected void addProperties(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception
72 {
73 if (transportMessage instanceof Map<?, ?>)
74 {
75 addPropertiesFromMap(muleMessage, (Map<?, ?>) transportMessage);
76 }
77 else if (filter.accept(transportMessage))
78 {
79 addPropertiesToFromJsonData(muleMessage, transportMessage);
80 }
81 }
82
83 private Object extractPayloadFromMap(Map<?, ?> map)
84 {
85 Object data = map.remove(Bayeux.DATA_FIELD);
86 if (data == null)
87 {
88 throw new IllegalArgumentException(Bayeux.DATA_FIELD + " parameter not set in payload map");
89 }
90 return data;
91 }
92
93 private Object extractJsonPayload(Object transportMessage) throws DefaultMuleException
94 {
95 String transportMessageString = transportMessage.toString();
96 if (transportMessageString.indexOf(Bayeux.DATA_FIELD) > -1)
97 {
98 try
99 {
100 JsonData data = new JsonData(transportMessageString);
101 return data.get(Bayeux.DATA_FIELD).toString();
102 }
103 catch (IOException e)
104 {
105 throw new DefaultMuleException(e);
106 }
107 }
108 else
109 {
110 return transportMessage;
111 }
112 }
113
114 @SuppressWarnings("unchecked")
115 private void addPropertiesFromMap(DefaultMuleMessage muleMessage, Map<?, ?> map)
116 {
117 Object replyTo = map.remove(AjaxConnector.REPLYTO_PARAM);
118 muleMessage.setReplyTo(replyTo);
119
120
121 map.remove(Bayeux.DATA_FIELD);
122
123
124 Map<String, Object> messageProperties = (Map<String, Object>) map;
125 muleMessage.addProperties(messageProperties, PropertyScope.INVOCATION);
126 }
127
128 private void addPropertiesToFromJsonData(DefaultMuleMessage muleMessage, Object transportMessage) throws IOException
129 {
130 JsonData data = new JsonData(transportMessage.toString());
131 if (data.hasNode(AjaxConnector.REPLYTO_PARAM))
132 {
133 muleMessage.setReplyTo(data.get(AjaxConnector.REPLYTO_PARAM));
134 }
135 }
136 }