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  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.json.transformers;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.lifecycle.InitialisationException;
 11  
 import org.mule.api.transformer.DataType;
 12  
 import org.mule.api.transformer.TransformerException;
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.module.json.JsonData;
 15  
 import org.mule.transformer.types.DataTypeFactory;
 16  
 import org.mule.util.IOUtils;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.InputStream;
 22  
 import java.io.InputStreamReader;
 23  
 import java.io.Reader;
 24  
 import java.net.URL;
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 
 28  
 /**
 29  
  * A transformer that will convert a JSON encoded object graph to a java object. The
 30  
  * object type is determined by the 'returnType' attribute. Note that this
 31  
  * transformers supports Arrays and Lists. For example, to convert a JSON string to
 32  
  * an array of org.foo.Person, set the the returnClass=[Lorg.foo.Person;.
 33  
  */
 34  
 public class JsonToObject extends AbstractJsonTransformer
 35  
 {
 36  0
     private static final DataType<JsonData> JSON_TYPE = DataTypeFactory.create(JsonData.class);
 37  
 
 38  0
     private Map<Class<?>, Class<?>> deserializationMixins = new HashMap<Class<?>, Class<?>>();
 39  
 
 40  
     public JsonToObject()
 41  0
     {
 42  0
         this.registerSourceType(DataTypeFactory.create(Reader.class));
 43  0
         this.registerSourceType(DataTypeFactory.create(URL.class));
 44  0
         this.registerSourceType(DataTypeFactory.create(File.class));
 45  0
         this.registerSourceType(DataTypeFactory.STRING);
 46  0
         this.registerSourceType(DataTypeFactory.INPUT_STREAM);
 47  0
         this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
 48  0
         setReturnDataType(JSON_TYPE);
 49  0
     }
 50  
 
 51  
     @Override
 52  
     public void initialise() throws InitialisationException
 53  
     {
 54  0
         super.initialise();
 55  
         //Add shared mixins first
 56  0
         for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
 57  
         {
 58  0
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
 59  
         }
 60  
 
 61  0
         for (Map.Entry<Class<?>, Class<?>> entry : deserializationMixins.entrySet())
 62  
         {
 63  0
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
 64  
         }
 65  0
     }
 66  
 
 67  
     @Override
 68  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 69  
     {
 70  0
         Object src = message.getPayload();
 71  
         Object returnValue;
 72  0
         InputStream is = null;
 73  0
         Reader reader = null;
 74  
 
 75  
         try
 76  
         {
 77  0
             if (src instanceof InputStream)
 78  
             {
 79  0
                 is = (InputStream) src;
 80  
             }
 81  0
             else if (src instanceof File)
 82  
             {
 83  0
                 is = new FileInputStream((File) src);
 84  
             }
 85  0
             else if (src instanceof URL)
 86  
             {
 87  0
                 is = ((URL) src).openStream();
 88  
             }
 89  0
             else if (src instanceof byte[])
 90  
             {
 91  0
                 is = new ByteArrayInputStream((byte[]) src);
 92  
             }
 93  
 
 94  0
             if (src instanceof Reader)
 95  
             {
 96  0
                 if (getReturnDataType().equals(JSON_TYPE))
 97  
                 {
 98  0
                     returnValue = new JsonData((Reader) src);
 99  
                 }
 100  
                 else
 101  
                 {
 102  0
                     returnValue = getMapper().readValue((Reader) src, getReturnDataType().getType());
 103  
                 }
 104  
             }
 105  0
             else if (src instanceof String)
 106  
             {
 107  0
                 if (getReturnDataType().equals(JSON_TYPE))
 108  
                 {
 109  0
                     returnValue = new JsonData((String) src);
 110  
                 }
 111  
                 else
 112  
                 {
 113  0
                     returnValue = getMapper().readValue((String) src, getReturnDataType().getType());
 114  
                 }
 115  
             }
 116  
             else
 117  
             {
 118  0
                 reader = new InputStreamReader(is, outputEncoding);
 119  0
                 if (getReturnDataType().equals(JSON_TYPE))
 120  
                 {
 121  0
                     returnValue = new JsonData(reader);
 122  
                 }
 123  
                 else
 124  
                 {
 125  0
                     returnValue = getMapper().readValue(reader, getReturnDataType().getType());
 126  
                 }
 127  
             }
 128  0
             return returnValue;
 129  
         }
 130  0
         catch (Exception e)
 131  
         {
 132  0
             throw new TransformerException(CoreMessages.transformFailed("json",
 133  
                 getReturnDataType().getType().getName()), this, e);
 134  
         }
 135  
         finally
 136  
         {
 137  0
             IOUtils.closeQuietly(reader);
 138  0
             IOUtils.closeQuietly(is);
 139  
         }
 140  
     }
 141  
 
 142  
     public Map<Class<?>, Class<?>> getDeserializationMixins()
 143  
     {
 144  0
         return deserializationMixins;
 145  
     }
 146  
 
 147  
     public void setDeserializationMixins(Map<Class<?>, Class<?>> deserializationMixins)
 148  
     {
 149  0
         this.deserializationMixins = deserializationMixins;
 150  0
     }
 151  
 }