View Javadoc

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