View Javadoc

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