View Javadoc

1   /*
2    * $Id: AjaxMessageReceiver.java 22098 2011-06-06 03:24:43Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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.AbstractMessageReceiver;
24  import org.mule.transport.ajax.embedded.AjaxConnector;
25  import org.mule.transport.ajax.i18n.AjaxMessages;
26  import org.mule.util.StringUtils;
27  
28  import org.cometd.Bayeux;
29  import org.cometd.Client;
30  import org.mortbay.cometd.AbstractBayeux;
31  import org.mortbay.cometd.BayeuxService;
32  
33  /**
34   * Registers a receiver service with Bayeux.
35   * The {@link AjaxMessageReceiver.ReceiverService#route(org.cometd.Client, Object)}
36   * is invoked when a message is received on the subscription channel
37   */
38  public class AjaxMessageReceiver extends AbstractMessageReceiver implements BayeuxAware
39  {
40      private AbstractBayeux bayeux;
41  
42      public AjaxMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint)
43              throws CreateException
44      {
45          super(connector, flowConstruct, endpoint);
46          String channel = endpoint.getEndpointURI().getPath();
47          if(StringUtils.isEmpty(channel) || channel.equals("/"))
48          {
49              //TODO i18n
50              throw new CreateException(AjaxMessages.createStaticMessage("The subscription path cannot be empty or equal '/'"), this);
51          }
52      }
53  
54      public class ReceiverService extends BayeuxService
55      {
56          private final ImmutableEndpoint endpoint;
57  
58          public ReceiverService(String channel, Bayeux bayeux, ImmutableEndpoint endpoint)
59          {
60              super(bayeux, channel);
61              this.endpoint = endpoint;
62              subscribe(channel, "route");
63          }
64  
65          public Object route(Client client, Object data) throws Exception
66          {
67              MuleMessage messageToRoute = createMuleMessage(data, endpoint.getEncoding());
68              messageToRoute.setInvocationProperty(AjaxConnector.COMETD_CLIENT, client);
69  
70              Object replyTo = messageToRoute.getReplyTo();
71              if (replyTo != null)
72              {
73                  messageToRoute.setProperty(MuleProperties.MULE_FORCE_SYNC_PROPERTY, Boolean.TRUE, PropertyScope.INBOUND);
74              }
75  
76              MuleEvent event = AjaxMessageReceiver.this.routeMessage(messageToRoute);
77              MuleMessage message = event == null ? null : event.getMessage();
78  
79              // only the AjaxConnector (as opposed to the AjaxServletConnector) has the
80              // isDisableReplyTo() method and both inherit from different superclasses
81              if (getConnector() instanceof AjaxConnector)
82              {
83                  // If a replyTo channel is set the client is expecting a response.
84                  // Mule does not invoke the replyTo handler if an error occurs, but in this case we
85                  // want it to.
86                  AjaxConnector ajaxConnector = (AjaxConnector) getConnector();
87                  if (!ajaxConnector.isDisableReplyTo() && message != null && message.getExceptionPayload() == null && replyTo != null)
88                  {
89                      ajaxConnector.getReplyToHandler(endpoint).processReplyTo(RequestContext.getEvent(), message, replyTo);
90                  }
91              }
92              return null;
93          }
94      }
95  
96      public AbstractBayeux getBayeux()
97      {
98          return bayeux;
99      }
100 
101     public void setBayeux(AbstractBayeux bayeux)
102     {
103         this.bayeux = bayeux;
104     }
105 
106     @Override
107     protected void doStart() throws MuleException
108     {
109         //Register our listener service with Bayeux
110         String channel = endpoint.getEndpointURI().getPath();
111         new ReceiverService(channel, getBayeux(), getEndpoint());
112     }
113 }