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