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