001: /*
002: * $Id: MuleHeadersOutInterceptor.java 11405 2008-03-18 00:13:00Z dirk.olmes $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.cxf.support;
012:
013: import static javax.xml.XMLConstants.XML_NS_URI;
014: import static org.mule.api.config.MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY;
015: import static org.mule.api.config.MuleProperties.MULE_CORRELATION_ID_PROPERTY;
016: import static org.mule.api.config.MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY;
017: import static org.mule.api.config.MuleProperties.MULE_EVENT_PROPERTY;
018: import static org.mule.api.config.MuleProperties.MULE_REPLY_TO_PROPERTY;
019: import static org.mule.transport.soap.MuleSoapHeaders.MULE_HEADER;
020:
021: import org.mule.api.MuleEvent;
022: import org.mule.transport.soap.MuleSoapHeaders;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.apache.cxf.binding.soap.SoapHeader;
027: import org.apache.cxf.binding.soap.SoapMessage;
028: import org.apache.cxf.helpers.DOMUtils;
029: import org.apache.cxf.interceptor.Fault;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.phase.Phase;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Text;
035:
036: /**
037: * Writes the Mule Soap Header to the outgoing request.
038: */
039: public class MuleHeadersOutInterceptor extends
040: BaseMuleHeaderInterceptor {
041:
042: public MuleHeadersOutInterceptor() {
043: super (Phase.PRE_PROTOCOL);
044: }
045:
046: public void handleMessage(Message m) throws Fault {
047: if (!(m instanceof SoapMessage)) {
048: return;
049: }
050:
051: SoapMessage message = (SoapMessage) m;
052: MuleEvent event = (MuleEvent) message.getExchange().get(
053: MULE_EVENT_PROPERTY);
054:
055: if (event == null) {
056: return;
057: }
058:
059: MuleSoapHeaders muleHeaders = new MuleSoapHeaders(event);
060:
061: if (muleHeaders.getCorrelationId() == null
062: && muleHeaders.getReplyTo() == null) {
063: return;
064: }
065:
066: Document owner_doc = DOMUtils.createDocument();
067:
068: Element mule_header = owner_doc.createElementNS(MULE_NS_URI,
069: QUALIFIED_MULE_HEADER);
070: // setup mule: namespace prefix declaration so that we can use it.
071: mule_header.setAttribute("xmlns:mule", MULE_NS_URI);
072:
073: if (muleHeaders.getCorrelationId() != null) {
074: mule_header.appendChild(buildMuleHeader(owner_doc,
075: MULE_CORRELATION_ID_PROPERTY, muleHeaders
076: .getCorrelationId()));
077: mule_header.appendChild(buildMuleHeader(owner_doc,
078: MULE_CORRELATION_GROUP_SIZE_PROPERTY, muleHeaders
079: .getCorrelationGroup()));
080: mule_header.appendChild(buildMuleHeader(owner_doc,
081: MULE_CORRELATION_SEQUENCE_PROPERTY, muleHeaders
082: .getCorrelationSequence()));
083: }
084: if (muleHeaders.getReplyTo() != null) {
085: mule_header.appendChild(buildMuleHeader(owner_doc,
086: MULE_REPLY_TO_PROPERTY, muleHeaders.getReplyTo()));
087: }
088:
089: SoapHeader sh = new SoapHeader(new QName(MULE_NS_URI,
090: MULE_HEADER), mule_header);
091: message.getHeaders().add(sh);
092: }
093:
094: Element buildMuleHeader(Document owner_doc, String localName,
095: String value) {
096: Element out = owner_doc.createElementNS(MULE_NS_URI, "mule:"
097: + localName);
098: if (value != null) {
099: Text text = owner_doc.createTextNode(value);
100: out.appendChild(text);
101: }
102: return out;
103: }
104:
105: }
|