Coverage Report - org.mule.module.json.transformers.JsonToObject
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonToObject
0%
0/48
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: JsonToObject.java 19715 2010-09-24 15:08:06Z 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.module.json.transformers;
 11  
 
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.lifecycle.InitialisationException;
 14  
 import org.mule.api.transformer.DataType;
 15  
 import org.mule.api.transformer.TransformerException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.module.json.JsonData;
 18  
 import org.mule.transformer.types.DataTypeFactory;
 19  
 import org.mule.util.IOUtils;
 20  
 
 21  
 import java.io.ByteArrayInputStream;
 22  
 import java.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.InputStream;
 25  
 import java.io.InputStreamReader;
 26  
 import java.io.Reader;
 27  
 import java.net.URL;
 28  
 import java.util.HashMap;
 29  
 import java.util.Map;
 30  
 
 31  
 
 32  
 /**
 33  
  * A transformer that will convert a JSON encoded object graph to a java object. The object type is
 34  
  * determined by the 'returnClass' attribute. Note that this transformers supports Arrays and Lists. For
 35  
  * example, to
 36  
  * convert a JSON string to an array of org.foo.Person, set the the returnClass=[Lorg.foo.Person;.
 37  
  * <p/>
 38  
  * The JSON engine can be configured using the jsonConfig attribute. This is an object reference to an
 39  
  * instance of: {@link net.sf.json.JsonConfig}. This can be created as a spring bean.
 40  
  */
 41  
 public class JsonToObject extends AbstractJsonTransformer
 42  
 {
 43  0
     private static final DataType<JsonData> JSON_TYPE = DataTypeFactory.create(JsonData.class);
 44  
     
 45  0
     private Map<Class<?>, Class<?>> deserializationMixins = new HashMap<Class<?>, Class<?>>();
 46  
     
 47  
     public JsonToObject()
 48  0
     {
 49  0
         this.registerSourceType(DataTypeFactory.create(Reader.class));
 50  0
         this.registerSourceType(DataTypeFactory.create(URL.class));
 51  0
         this.registerSourceType(DataTypeFactory.create(File.class));
 52  0
         this.registerSourceType(DataTypeFactory.STRING);
 53  0
         this.registerSourceType(DataTypeFactory.INPUT_STREAM);
 54  0
         this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
 55  0
         setReturnDataType(JSON_TYPE);
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public void initialise() throws InitialisationException
 60  
     {
 61  0
         super.initialise();
 62  
         //Add shared mixins first
 63  0
         for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
 64  
         {
 65  0
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
 66  
         }
 67  
 
 68  0
         for (Map.Entry<Class<?>, Class<?>> entry : deserializationMixins.entrySet())
 69  
         {
 70  0
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
 71  
         }
 72  0
     }
 73  
 
 74  
     @Override
 75  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 76  
     {
 77  0
         Object src = message.getPayload();
 78  
         Object returnValue;
 79  0
         InputStream is = null;
 80  0
         Reader reader = null;
 81  
 
 82  
         try
 83  
         {
 84  0
             if (src instanceof InputStream)
 85  
             {
 86  0
                 is = (InputStream) src;
 87  
             }
 88  0
             else if (src instanceof File)
 89  
             {
 90  0
                 is = new FileInputStream((File) src);
 91  
             }
 92  0
             else if (src instanceof URL)
 93  
             {
 94  0
                 is = ((URL) src).openStream();
 95  
             }
 96  0
             else if (src instanceof byte[])
 97  
             {
 98  0
                 is = new ByteArrayInputStream((byte[]) src);
 99  
             }
 100  
             
 101  0
             if (src instanceof Reader)
 102  
             {
 103  0
                 if (getReturnDataType().equals(JSON_TYPE))
 104  
                 {
 105  0
                     returnValue = new JsonData((Reader) src);
 106  
                 }
 107  
                 else
 108  
                 {
 109  0
                     returnValue = getMapper().readValue((Reader) src, getReturnDataType().getType());
 110  
                 }
 111  
             }
 112  0
             else if (src instanceof String)
 113  
             {
 114  0
                 if (getReturnDataType().equals(JSON_TYPE))
 115  
                 {
 116  0
                     returnValue = new JsonData((String) src);
 117  
                 }
 118  
                 else
 119  
                 {
 120  0
                     returnValue = getMapper().readValue((String) src, getReturnDataType().getType());
 121  
                 }
 122  
             }
 123  
             else
 124  
             {
 125  0
                 reader = new InputStreamReader(is, outputEncoding);
 126  0
                 if (getReturnDataType().equals(JSON_TYPE))
 127  
                 {
 128  0
                     returnValue = new JsonData(reader);
 129  
                 }
 130  
                 else
 131  
                 {
 132  0
                     returnValue = getMapper().readValue(reader, getReturnDataType().getType());
 133  
                 }
 134  
             }
 135  0
             return returnValue;
 136  
         }
 137  0
         catch (Exception e)
 138  
         {
 139  0
             throw new TransformerException(CoreMessages.transformFailed("json",
 140  
                 getReturnDataType().getType().getName()), this, e);
 141  
         }
 142  
         finally
 143  
         {
 144  0
             IOUtils.closeQuietly(reader);
 145  0
             IOUtils.closeQuietly(is);
 146  
         }
 147  
     }
 148  
 
 149  
     public Map<Class<?>, Class<?>> getDeserializationMixins()
 150  
     {
 151  0
         return deserializationMixins;
 152  
     }
 153  
 
 154  
     public void setDeserializationMixins(Map<Class<?>, Class<?>> deserializationMixins)
 155  
     {
 156  0
         this.deserializationMixins = deserializationMixins;
 157  0
     }
 158  
 }