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.transformers.jaxb;
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.jaxb.model.EmailAddress;
14  import org.mule.jaxb.model.Person;
15  import org.mule.module.xml.util.XMLUtils;
16  
17  import java.io.InputStream;
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.xml.bind.JAXBContext;
23  import javax.xml.bind.JAXBException;
24  import javax.xml.xpath.XPathExpressionException;
25  
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Node;
28  
29  
30  
31  /**
32   * Explicit JAXB transformers used to test that JAXB transforms can be intercepted
33   */
34  @ContainsTransformerMethods
35  public class JAXBTestTransformers
36  {
37  
38      @Transformer(sourceTypes = {String.class})
39      public Person toPerson(Document doc, JAXBContext context) throws JAXBException
40      {
41          return (Person) context.createUnmarshaller().unmarshal(doc);
42      }
43  
44  
45      //NOTE the @MessagePayload annotation is ignored for transformer but we're just testing that that it doesn't break things
46      @Transformer(sourceTypes = {String.class, InputStream.class})
47      public List<EmailAddress> toEmailAddresses(@Payload Document doc, @InboundHeaders("*") Map headers, JAXBContext context) throws JAXBException, XPathExpressionException
48      {
49          //Test that we receive headers
50  
51          if(!headers.get("foo").equals("fooValue"))
52          {
53              throw new IllegalArgumentException("Header foo was not set to the correct value 'fooValue'");
54          }
55          
56          List<Node> nodes = XMLUtils.select("/person/emailAddresses/emailAddress", doc);
57          List<EmailAddress> addrs = new ArrayList<EmailAddress>(nodes.size());
58          for (Node node : nodes)
59          {
60              addrs.add(context.createUnmarshaller().unmarshal(node, EmailAddress.class).getValue());
61          }
62          return addrs;
63      }
64  
65  }