01: /*
02: * $Id: MuleHeadersInInterceptor.java 11405 2008-03-18 00:13:00Z dirk.olmes $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.cxf.support;
12:
13: import java.util.Set;
14:
15: import javax.xml.namespace.QName;
16:
17: import org.apache.cxf.binding.soap.SoapMessage;
18: import org.apache.cxf.headers.Header;
19: import org.apache.cxf.interceptor.Fault;
20: import org.apache.cxf.message.Message;
21: import org.apache.cxf.phase.Phase;
22: import org.w3c.dom.Element;
23: import org.w3c.dom.Node;
24: import org.w3c.dom.NodeList;
25: import org.w3c.dom.Text;
26:
27: /**
28: * Reads the Mule Soap Header and sets the various header properties on the message.
29: */
30: public class MuleHeadersInInterceptor extends BaseMuleHeaderInterceptor {
31:
32: public MuleHeadersInInterceptor() {
33: super (Phase.PRE_PROTOCOL);
34: }
35:
36: public void handleMessage(Message m) throws Fault {
37: if (!(m instanceof SoapMessage)) {
38: return;
39: }
40:
41: SoapMessage message = (SoapMessage) m;
42: if (!message.hasHeaders()) {
43: return;
44: }
45: Header mule_header = message.getHeader(MULE_HEADER_Q);
46: if (mule_header == null) {
47: return;
48: }
49: Object obj = mule_header.getObject();
50: if (!(obj instanceof Element)) {
51: // Error? We can't work with it at any rate.
52: return;
53: }
54:
55: Element header_element = (Element) obj;
56: NodeList mule_headers = header_element.getChildNodes();
57: int idx = 0;
58: Node child;
59: while ((child = mule_headers.item(idx++)) != null) {
60: if (child.getNodeType() != Node.ELEMENT_NODE) {
61: continue;
62: }
63: Element child_el = (Element) child;
64: if (child_el.getNamespaceURI() == null
65: || !child_el.getNamespaceURI().equals(MULE_NS_URI)) {
66: continue;
67: }
68:
69: if (SUPPORTED_HEADERS.contains(child_el.getLocalName())) {
70: message.put(child_el.getLocalName(),
71: collectTextFrom(child_el));
72: }
73: }
74: }
75:
76: public Set<QName> getUnderstoodHeaders() {
77: return UNDERSTOOD_HEADERS;
78: }
79:
80: private String collectTextFrom(Element e) {
81: NodeList children = e.getChildNodes();
82: StringBuilder sb = new StringBuilder();
83: int idx = 0;
84: Node n;
85: while ((n = children.item(idx++)) != null) {
86: if (n.getNodeType() == Node.TEXT_NODE) {
87: sb.append(((Text) n).getTextContent());
88: }
89: }
90: return sb.toString();
91: }
92:
93: }
|