View Javadoc

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