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.cxf.support;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.module.cxf.CxfConstants;
13  
14  import java.util.Set;
15  
16  import javax.xml.namespace.QName;
17  
18  import org.apache.cxf.binding.soap.SoapMessage;
19  import org.apache.cxf.headers.Header;
20  import org.apache.cxf.interceptor.Fault;
21  import org.apache.cxf.message.Message;
22  import org.apache.cxf.phase.Phase;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.NodeList;
26  import org.w3c.dom.Text;
27  
28  /**
29   * Reads the Mule Soap Header and sets the various header properties on the message.
30   */
31  public class MuleHeadersInInterceptor extends AbstractMuleHeaderInterceptor
32  {
33  
34      public MuleHeadersInInterceptor()
35      {
36          super(Phase.PRE_PROTOCOL);
37      }
38  
39      public void handleMessage(Message m) throws Fault
40      {
41          if (!(m instanceof SoapMessage))
42          {
43              return;
44          }
45  
46          SoapMessage message = (SoapMessage) m;
47          if (!message.hasHeaders())
48          {
49              return;
50          }
51          Header mule_header = message.getHeader(MULE_HEADER_Q);
52          if (mule_header == null)
53          {
54              return;
55          }
56          Object obj = mule_header.getObject();
57          if (!(obj instanceof Element))
58          {
59              // Error? We can't work with it at any rate.
60              return;
61          }
62  
63          Element header_element = (Element) obj;
64          NodeList mule_headers = header_element.getChildNodes();
65          int idx = 0;
66          Node child;
67          while ((child = mule_headers.item(idx++)) != null)
68          {
69              if (child.getNodeType() != Node.ELEMENT_NODE)
70              {
71                  continue;
72              }
73              Element child_el = (Element) child;
74              if (child_el.getNamespaceURI() == null || !child_el.getNamespaceURI().equals(MULE_NS_URI))
75              {
76                  continue;
77              }
78              
79              if (SUPPORTED_HEADERS.contains(child_el.getLocalName()))
80              {
81                  message.put(child_el.getLocalName(), collectTextFrom(child_el));
82              }
83          }
84          
85  
86          MuleMessage reqMsg = ((MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT)).getMessage();
87          
88          // Copy correlation headers nto message
89          String replyTo = (String) message.get(MuleProperties.MULE_REPLY_TO_PROPERTY);
90          if (replyTo != null)
91          {
92              reqMsg.setReplyTo(replyTo);
93          }
94          
95          String corId = (String) message.get(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
96          if (corId != null)
97          {
98              reqMsg.setCorrelationId(corId);
99          }
100 
101         String corGroupSize = (String) message.get(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY);
102         if (corGroupSize != null)
103         {
104             reqMsg.setCorrelationGroupSize(Integer.valueOf(corGroupSize));
105         }
106 
107         String corSeq = (String) message.get(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY);
108         if (corSeq != null)
109         {
110             reqMsg.setCorrelationSequence(Integer.valueOf(corSeq));
111         }
112     }
113 
114     public Set<QName> getUnderstoodHeaders()
115     {
116         return UNDERSTOOD_HEADERS;
117     }
118 
119     private String collectTextFrom(Element e)
120     {
121         NodeList children = e.getChildNodes();
122         StringBuilder sb = new StringBuilder();
123         int idx = 0;
124         Node n;
125         while ((n = children.item(idx++)) != null)
126         {
127             if (n.getNodeType() == Node.TEXT_NODE)
128             {
129                 sb.append(((Text) n).getTextContent());
130             }
131         }
132         return sb.toString();
133     }
134 
135 }