View Javadoc
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.annotations.ContainsTransformerMethods;
10  import org.mule.api.annotations.Transformer;
11  import org.mule.api.annotations.param.InboundHeaders;
12  import org.mule.api.annotations.param.Payload;
13  import org.mule.json.model.EmailAddress;
14  import org.mule.json.model.Item;
15  import org.mule.json.model.Person;
16  
17  import org.codehaus.jackson.JsonNode;
18  import org.codehaus.jackson.map.ObjectMapper;
19  import org.codehaus.jackson.node.ArrayNode;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  @ContainsTransformerMethods
29  public class JsonCustomTransformer
30  {
31      //This is used to test other source types and injecting an ObjectMapper instance
32      @Transformer(sourceTypes = String.class)
33      public Person toCar(byte[] doc, ObjectMapper context) throws IOException
34      {
35          return context.readValue(doc, 0, doc.length, Person.class);
36      }
37  
38  
39      //NOTE the @MessagePayload annotation is ignored for transformer but we're just testing that that it doesn't break things
40      @Transformer
41      public EmailAddress toEmail(@Payload InputStream in, @InboundHeaders("*") Map headers, ObjectMapper mapper) throws IOException
42      {
43          Object foo = headers.get("foo");
44          if(foo==null || !"fooValue".equals(foo))
45          {
46              throw new IllegalArgumentException("Header foo not set to 'fooValue'");
47          }
48          return mapper.readValue(in, EmailAddress.class);
49      }
50  
51  
52      @Transformer(sourceTypes = {InputStream.class})
53      public List<Item> toItemList(@Payload String in, ObjectMapper mapper) throws IOException
54      {
55          List<Item> items = new ArrayList<Item>();
56          ArrayNode nodes = (ArrayNode) mapper.readTree(in);
57          for (Iterator<JsonNode> iterator = nodes.getElements(); iterator.hasNext();)
58          {
59              //TODO, we're reparsing content here
60              items.add(mapper.readValue(iterator.next().toString(), Item.class));
61          }
62  
63          return items;
64      }
65  
66      @Transformer(sourceTypes = {InputStream.class})
67      public List<Person> toPeople(@Payload String in, ObjectMapper mapper) throws IOException
68      {
69          List<Person> people = new ArrayList<Person>();
70          ArrayNode nodes = (ArrayNode) mapper.readTree(in);
71          for (Iterator<JsonNode> iterator = nodes.getElements(); iterator.hasNext();)
72          {
73              //TODO, we're reparsing content here
74               people.add(mapper.readValue(iterator.next().toString(), Person.class));
75          }
76  
77          return people;
78      }
79  }