1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.ajax; |
8 | |
|
9 | |
import org.mule.RequestContext; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.config.MuleProperties; |
14 | |
import org.mule.api.construct.FlowConstruct; |
15 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.lifecycle.CreateException; |
18 | |
import org.mule.api.transport.Connector; |
19 | |
import org.mule.api.transport.PropertyScope; |
20 | |
import org.mule.transport.AbstractMessageReceiver; |
21 | |
import org.mule.transport.ajax.embedded.AjaxConnector; |
22 | |
import org.mule.transport.ajax.i18n.AjaxMessages; |
23 | |
import org.mule.util.StringUtils; |
24 | |
|
25 | |
import org.cometd.Bayeux; |
26 | |
import org.cometd.Client; |
27 | |
import org.mortbay.cometd.AbstractBayeux; |
28 | |
import org.mortbay.cometd.BayeuxService; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class AjaxMessageReceiver extends AbstractMessageReceiver implements BayeuxAware |
36 | |
{ |
37 | |
private AbstractBayeux bayeux; |
38 | |
|
39 | |
public AjaxMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
40 | |
throws CreateException |
41 | |
{ |
42 | 0 | super(connector, flowConstruct, endpoint); |
43 | 0 | String channel = endpoint.getEndpointURI().getPath(); |
44 | 0 | if(StringUtils.isEmpty(channel) || channel.equals("/")) |
45 | |
{ |
46 | |
|
47 | 0 | throw new CreateException(AjaxMessages.createStaticMessage("The subscription path cannot be empty or equal '/'"), this); |
48 | |
} |
49 | 0 | } |
50 | |
|
51 | |
public class ReceiverService extends BayeuxService |
52 | |
{ |
53 | |
private final ImmutableEndpoint endpoint; |
54 | |
|
55 | |
public ReceiverService(String channel, Bayeux bayeux, ImmutableEndpoint endpoint) |
56 | 0 | { |
57 | 0 | super(bayeux, channel); |
58 | 0 | this.endpoint = endpoint; |
59 | 0 | subscribe(channel, "route"); |
60 | 0 | } |
61 | |
|
62 | |
public Object route(Client client, Object data) throws Exception |
63 | |
{ |
64 | 0 | MuleMessage messageToRoute = createMuleMessage(data, endpoint.getEncoding()); |
65 | 0 | messageToRoute.setInvocationProperty(AjaxConnector.COMETD_CLIENT, client); |
66 | |
|
67 | 0 | Object replyTo = messageToRoute.getReplyTo(); |
68 | 0 | if (replyTo != null) |
69 | |
{ |
70 | 0 | messageToRoute.setProperty(MuleProperties.MULE_FORCE_SYNC_PROPERTY, Boolean.TRUE, PropertyScope.INBOUND); |
71 | |
} |
72 | |
|
73 | 0 | MuleEvent event = AjaxMessageReceiver.this.routeMessage(messageToRoute); |
74 | 0 | MuleMessage message = event == null ? null : event.getMessage(); |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | if (getConnector() instanceof AjaxConnector) |
79 | |
{ |
80 | |
|
81 | |
|
82 | |
|
83 | 0 | AjaxConnector ajaxConnector = (AjaxConnector) getConnector(); |
84 | 0 | if (!ajaxConnector.isDisableReplyTo() && message != null && message.getExceptionPayload() == null && replyTo != null) |
85 | |
{ |
86 | 0 | ajaxConnector.getReplyToHandler(endpoint).processReplyTo(RequestContext.getEvent(), message, replyTo); |
87 | |
} |
88 | |
} |
89 | 0 | return null; |
90 | |
} |
91 | |
} |
92 | |
|
93 | |
public AbstractBayeux getBayeux() |
94 | |
{ |
95 | 0 | return bayeux; |
96 | |
} |
97 | |
|
98 | |
public void setBayeux(AbstractBayeux bayeux) |
99 | |
{ |
100 | 0 | this.bayeux = bayeux; |
101 | 0 | } |
102 | |
|
103 | |
@Override |
104 | |
protected void doStart() throws MuleException |
105 | |
{ |
106 | |
|
107 | 0 | String channel = endpoint.getEndpointURI().getPath(); |
108 | 0 | new ReceiverService(channel, getBayeux(), getEndpoint()); |
109 | 0 | } |
110 | |
} |