View Javadoc

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      private static final DataType<JsonData> JSON_TYPE = DataTypeFactory.create(JsonData.class);
44      
45      private Map<Class<?>, Class<?>> deserializationMixins = new HashMap<Class<?>, Class<?>>();
46      
47      public JsonToObject()
48      {
49          this.registerSourceType(DataTypeFactory.create(Reader.class));
50          this.registerSourceType(DataTypeFactory.create(URL.class));
51          this.registerSourceType(DataTypeFactory.create(File.class));
52          this.registerSourceType(DataTypeFactory.STRING);
53          this.registerSourceType(DataTypeFactory.INPUT_STREAM);
54          this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
55          setReturnDataType(JSON_TYPE);
56      }
57  
58      @Override
59      public void initialise() throws InitialisationException
60      {
61          super.initialise();
62          //Add shared mixins first
63          for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
64          {
65              getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
66          }
67  
68          for (Map.Entry<Class<?>, Class<?>> entry : deserializationMixins.entrySet())
69          {
70              getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
71          }
72      }
73  
74      @Override
75      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
76      {
77          Object src = message.getPayload();
78          Object returnValue;
79          InputStream is = null;
80          Reader reader = null;
81  
82          try
83          {
84              if (src instanceof InputStream)
85              {
86                  is = (InputStream) src;
87              }
88              else if (src instanceof File)
89              {
90                  is = new FileInputStream((File) src);
91              }
92              else if (src instanceof URL)
93              {
94                  is = ((URL) src).openStream();
95              }
96              else if (src instanceof byte[])
97              {
98                  is = new ByteArrayInputStream((byte[]) src);
99              }
100             
101             if (src instanceof Reader)
102             {
103                 if (getReturnDataType().equals(JSON_TYPE))
104                 {
105                     returnValue = new JsonData((Reader) src);
106                 }
107                 else
108                 {
109                     returnValue = getMapper().readValue((Reader) src, getReturnDataType().getType());
110                 }
111             }
112             else if (src instanceof String)
113             {
114                 if (getReturnDataType().equals(JSON_TYPE))
115                 {
116                     returnValue = new JsonData((String) src);
117                 }
118                 else
119                 {
120                     returnValue = getMapper().readValue((String) src, getReturnDataType().getType());
121                 }
122             }
123             else
124             {
125                 reader = new InputStreamReader(is, outputEncoding);
126                 if (getReturnDataType().equals(JSON_TYPE))
127                 {
128                     returnValue = new JsonData(reader);
129                 }
130                 else
131                 {
132                     returnValue = getMapper().readValue(reader, getReturnDataType().getType());
133                 }
134             }
135             return returnValue;
136         }
137         catch (Exception e)
138         {
139             throw new TransformerException(CoreMessages.transformFailed("json",
140                 getReturnDataType().getType().getName()), this, e);
141         }
142         finally
143         {
144             IOUtils.closeQuietly(reader);
145             IOUtils.closeQuietly(is);
146         }
147     }
148 
149     public Map<Class<?>, Class<?>> getDeserializationMixins()
150     {
151         return deserializationMixins;
152     }
153 
154     public void setDeserializationMixins(Map<Class<?>, Class<?>> deserializationMixins)
155     {
156         this.deserializationMixins = deserializationMixins;
157     }
158 }