1 17 package org.apache.servicemix.soap.handlers.addressing; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import javax.xml.namespace.QName ; 23 24 import org.apache.activemq.util.IdGenerator; 25 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 26 import org.apache.servicemix.jbi.util.DOMUtil; 27 import org.apache.servicemix.soap.Context; 28 import org.apache.servicemix.soap.SoapFault; 29 import org.apache.servicemix.soap.handlers.AbstractHandler; 30 import org.apache.servicemix.soap.marshalers.SoapMessage; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.DocumentFragment ; 33 import org.w3c.dom.Element ; 34 35 43 public class AddressingHandler extends AbstractHandler { 44 45 public static final String WSA_NAMESPACE_200303 = "http://schemas.xmlsoap.org/ws/2003/03/addressing"; 46 public static final String WSA_NAMESPACE_200403 = "http://schemas.xmlsoap.org/ws/2004/03/addressing"; 47 public static final String WSA_NAMESPACE_200408 = "http://schemas.xmlsoap.org/ws/2004/08/addressing"; 48 public static final String WSA_NAMESPACE_200508 = "http://www.w3.org/2005/08/addressing"; 49 50 public static final String WSA_PREFIX = "wsa"; 51 52 public static final String EL_ACTION = "Action"; 53 public static final String EL_ADDRESS = "Address"; 54 public static final String EL_FAULT_TO = "FaultTo"; 55 public static final String EL_FROM = "From"; 56 public static final String EL_MESSAGE_ID = "MessageID"; 57 public static final String EL_METADATA = "Metadata"; 58 public static final String EL_REFERENCE_PARAMETERS = "ReferenceParameters"; 59 public static final String EL_RELATES_TO = "RelatesTo"; 60 public static final String EL_REPLY_TO = "ReplyTo"; 61 public static final String EL_TO = "To"; 62 63 protected final SourceTransformer sourceTransformer = new SourceTransformer(); 64 protected final IdGenerator idGenerator = new IdGenerator(); 65 66 public void onReceive(Context context) throws Exception { 67 SoapMessage message = context.getInMessage(); 68 String action = null; 69 String to = null; 70 String nsUri = null; 71 Map headers = message.getHeaders(); 72 if (headers != null) { 73 for (Iterator it = headers.keySet().iterator(); it.hasNext();) { 74 QName qname = (QName ) it.next(); 75 Object value = headers.get(qname); 76 if (isWSANamespace(qname.getNamespaceURI())) { 77 if (nsUri == null) { 78 nsUri = qname.getNamespaceURI(); 79 } else if (!nsUri.equals(qname.getNamespaceURI())) { 80 throw new SoapFault(SoapFault.SENDER, "Inconsistent use of wsa namespaces"); 81 } 82 if (EL_ACTION.equals(qname.getLocalPart())) { 83 action = getHeaderText(value); 84 String [] parts = split(action); 85 context.setProperty(Context.INTERFACE, new QName (parts[0], parts[1])); 86 context.setProperty(Context.OPERATION, new QName (parts[0], parts[2])); 87 } else if (EL_TO.equals(qname.getLocalPart())) { 88 to = getHeaderText(value); 89 String [] parts = split(to); 90 context.setProperty(Context.SERVICE, new QName (parts[0], parts[1])); 91 context.setProperty(Context.ENDPOINT, parts[2]); 92 } else { 93 } 95 } 96 } 97 } 98 } 99 100 public void onReply(Context context) throws Exception { 101 SoapMessage in = context.getInMessage(); 102 SoapMessage out = context.getOutMessage(); 103 Map headers = in.getHeaders(); 104 if (headers != null) { 105 for (Iterator it = headers.keySet().iterator(); it.hasNext();) { 106 QName qname = (QName ) it.next(); 107 Object value = headers.get(qname); 108 if (isWSANamespace(qname.getNamespaceURI())) { 109 if (EL_MESSAGE_ID.equals(qname.getLocalPart())) { 110 QName name = new QName (qname.getNamespaceURI(), EL_MESSAGE_ID, qname.getPrefix() != null ? qname.getPrefix() : WSA_PREFIX); 111 DocumentFragment df = createHeader(name, idGenerator.generateSanitizedId()); 112 out.addHeader(name, df); 113 name = new QName (qname.getNamespaceURI(), EL_RELATES_TO, qname.getPrefix() != null ? qname.getPrefix() : WSA_PREFIX); 114 df = createHeader(name, getHeaderText(value)); 115 out.addHeader(name, df); 116 } 117 } 118 } 119 } 120 } 121 122 protected boolean isWSANamespace(String ns) { 123 return WSA_NAMESPACE_200303.equals(ns) || 124 WSA_NAMESPACE_200403.equals(ns) || 125 WSA_NAMESPACE_200408.equals(ns) || 126 WSA_NAMESPACE_200508.equals(ns); 127 } 128 129 protected String getHeaderText(Object header) { 130 Element el = (Element ) ((DocumentFragment ) header).getFirstChild(); 131 return DOMUtil.getElementText(el); 132 } 133 134 protected DocumentFragment createHeader(QName name, String value) throws Exception { 135 Document doc = new SourceTransformer().createDocument(); 136 DocumentFragment df = doc.createDocumentFragment(); 137 Element el = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart()); 138 el.appendChild(doc.createTextNode(value)); 139 df.appendChild(el); 140 return df; 141 } 142 143 protected String [] split(String uri) { 144 char sep; 145 uri = uri.trim(); 146 if (uri.indexOf('/') > 0) { 147 sep = '/'; 148 } else { 149 sep = ':'; 150 } 151 int idx1 = uri.lastIndexOf(sep); 152 int idx2 = uri.lastIndexOf(sep, idx1 - 1); 153 String epName = uri.substring(idx1 + 1); 154 String svcName = uri.substring(idx2 + 1, idx1); 155 String nsUri = uri.substring(0, idx2); 156 return new String [] { nsUri, svcName, epName }; 157 } 158 159 } 160 | Popular Tags |