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.module.cxf.CxfConstants;
11  import org.mule.module.cxf.MuleSoapHeaders;
12  
13  import javax.xml.namespace.QName;
14  
15  import org.apache.cxf.binding.soap.SoapHeader;
16  import org.apache.cxf.binding.soap.SoapMessage;
17  import org.apache.cxf.helpers.DOMUtils;
18  import org.apache.cxf.interceptor.Fault;
19  import org.apache.cxf.message.Message;
20  import org.apache.cxf.phase.Phase;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Text;
24  
25  import static org.mule.module.cxf.MuleSoapHeaders.MULE_HEADER;
26  
27  import static org.mule.api.config.MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY;
28  import static org.mule.api.config.MuleProperties.MULE_CORRELATION_ID_PROPERTY;
29  import static org.mule.api.config.MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY;
30  import static org.mule.api.config.MuleProperties.MULE_EVENT_PROPERTY;
31  import static org.mule.api.config.MuleProperties.MULE_REPLY_TO_PROPERTY;
32  
33  /**
34   * Writes the Mule Soap Header to the outgoing request.
35   */
36  public class MuleHeadersOutInterceptor extends AbstractMuleHeaderInterceptor
37  {
38  
39      public MuleHeadersOutInterceptor()
40      {
41          super(Phase.PRE_PROTOCOL);
42      }
43  
44      public void handleMessage(Message m) throws Fault
45      {
46          if (!(m instanceof SoapMessage))
47          {
48              return;
49          }
50  
51          SoapMessage message = (SoapMessage) m;
52          MuleEvent event = (MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT);
53  
54          if (event == null)
55          {
56              return;
57          }
58  
59          MuleSoapHeaders muleHeaders = new MuleSoapHeaders(event);
60  
61          if (muleHeaders.getCorrelationId() == null && muleHeaders.getReplyTo() == null)
62          {
63              return;
64          }
65  
66          Document owner_doc = DOMUtils.createDocument();
67  
68          Element mule_header = owner_doc.createElementNS(MULE_NS_URI, QUALIFIED_MULE_HEADER);
69          // setup mule: namespace prefix declaration so that we can use it.
70          mule_header.setAttribute("xmlns:mule", MULE_NS_URI);
71  
72          if (muleHeaders.getCorrelationId() != null)
73          {
74              mule_header.appendChild(buildMuleHeader(owner_doc, MULE_CORRELATION_ID_PROPERTY,
75                  muleHeaders.getCorrelationId()));
76              mule_header.appendChild(buildMuleHeader(owner_doc, MULE_CORRELATION_GROUP_SIZE_PROPERTY,
77                  muleHeaders.getCorrelationGroup()));
78              mule_header.appendChild(buildMuleHeader(owner_doc, MULE_CORRELATION_SEQUENCE_PROPERTY,
79                  muleHeaders.getCorrelationSequence()));
80          }
81          if (muleHeaders.getReplyTo() != null)
82          {
83              mule_header.appendChild(buildMuleHeader(owner_doc, MULE_REPLY_TO_PROPERTY,
84                  muleHeaders.getReplyTo()));
85          }
86  
87          SoapHeader sh = new SoapHeader(new QName(MULE_NS_URI, MULE_HEADER), mule_header);
88          message.getHeaders().add(sh);
89      }
90  
91      Element buildMuleHeader(Document owner_doc, String localName, String value)
92      {
93          Element out = owner_doc.createElementNS(MULE_NS_URI, "mule:" + localName);
94          if (value != null)
95          {
96              Text text = owner_doc.createTextNode(value);
97              out.appendChild(text);
98          }
99          return out;
100     }
101 
102 }