View Javadoc

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