View Javadoc

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