View Javadoc

1   /*
2    * $Id: MuleHeadersInInterceptor.java 11405 2008-03-18 00:13:00Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.cxf.support;
12  
13  import java.util.Set;
14  
15  import javax.xml.namespace.QName;
16  
17  import org.apache.cxf.binding.soap.SoapMessage;
18  import org.apache.cxf.headers.Header;
19  import org.apache.cxf.interceptor.Fault;
20  import org.apache.cxf.message.Message;
21  import org.apache.cxf.phase.Phase;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Node;
24  import org.w3c.dom.NodeList;
25  import org.w3c.dom.Text;
26  
27  /**
28   * Reads the Mule Soap Header and sets the various header properties on the message.
29   */
30  public class MuleHeadersInInterceptor extends BaseMuleHeaderInterceptor
31  {
32  
33      public MuleHeadersInInterceptor()
34      {
35          super(Phase.PRE_PROTOCOL);
36      }
37  
38      public void handleMessage(Message m) throws Fault
39      {
40          if (!(m instanceof SoapMessage))
41          {
42              return;
43          }
44  
45          SoapMessage message = (SoapMessage) m;
46          if (!message.hasHeaders())
47          {
48              return;
49          }
50          Header mule_header = message.getHeader(MULE_HEADER_Q);
51          if (mule_header == null)
52          {
53              return;
54          }
55          Object obj = mule_header.getObject();
56          if (!(obj instanceof Element))
57          {
58              // Error? We can't work with it at any rate.
59              return;
60          }
61  
62          Element header_element = (Element) obj;
63          NodeList mule_headers = header_element.getChildNodes();
64          int idx = 0;
65          Node child;
66          while ((child = mule_headers.item(idx++)) != null)
67          {
68              if (child.getNodeType() != Node.ELEMENT_NODE)
69              {
70                  continue;
71              }
72              Element child_el = (Element) child;
73              if (child_el.getNamespaceURI() == null || !child_el.getNamespaceURI().equals(MULE_NS_URI))
74              {
75                  continue;
76              }
77              
78              if (SUPPORTED_HEADERS.contains(child_el.getLocalName()))
79              {
80                  message.put(child_el.getLocalName(), collectTextFrom(child_el));
81              }
82          }
83      }
84  
85      public Set<QName> getUnderstoodHeaders()
86      {
87          return UNDERSTOOD_HEADERS;
88      }
89  
90      private String collectTextFrom(Element e)
91      {
92          NodeList children = e.getChildNodes();
93          StringBuilder sb = new StringBuilder();
94          int idx = 0;
95          Node n;
96          while ((n = children.item(idx++)) != null)
97          {
98              if (n.getNodeType() == Node.TEXT_NODE)
99              {
100                 sb.append(((Text) n).getTextContent());
101             }
102         }
103         return sb.toString();
104     }
105 
106 }