View Javadoc

1   /*
2    * $Id: AjaxMuleMessageFactory.java 20384 2010-11-29 19:46:26Z dfeist $
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  
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   * A {@link MuleMessageFactory} implementation for JSON messages. The payload can either be a 
30   * {@link Map} or a JSON encoded String.
31   * <p/>
32   * If the payload is a {@link Map}, this message factory will recognise the following keys:
33   * <ul>
34   * <li>data - the object to use a the payload, this can be a JSON encoded string. See {@link Bayeux#DATA_FIELD}</li>
35   * <li>replyTo - the return Ajax channel for this message. {@link AjaxConnector#REPLYTO_PARAM}</li>
36   * </ul>
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         // remove the part of the map we process as payload
121         map.remove(Bayeux.DATA_FIELD);
122         
123         // the remainder of the map is used as message properties
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.getAsString(AjaxConnector.REPLYTO_PARAM));
134         }
135     }
136 }