View Javadoc

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