View Javadoc

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