// Decompiled by DJ v3.8.8.85 Copyright 2005 Atanas Neshkov Date: 03/02/2010 13.04.41 // Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version! // Decompiler options: packimports(3) // Source File Name: JbiInWsdl1Interceptor.java package org.apache.servicemix.cxfbc.interceptors; import java.util.*; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import org.apache.cxf.binding.soap.*; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.binding.soap.model.SoapBindingInfo; import org.apache.cxf.binding.soap.model.SoapHeaderInfo; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.Message; import org.apache.cxf.service.model.*; import org.apache.cxf.staxutils.*; import org.apache.servicemix.soap.util.DomUtil; import org.w3c.dom.*; // Referenced classes of package org.apache.servicemix.cxfbc.interceptors: // JbiOperationInterceptor, CxfJbiConstants public class JbiInWsdl1Interceptor extends AbstractSoapInterceptor { private boolean useJBIWrapper; private boolean useSOAPEnvelope; public JbiInWsdl1Interceptor(boolean useJBIWrapper, boolean useSOAPEnvelope) { super("pre-invoke"); this.useJBIWrapper = true; this.useSOAPEnvelope = true; addAfter(JbiOperationInterceptor.class.getName()); this.useJBIWrapper = useJBIWrapper; this.useSOAPEnvelope = useSOAPEnvelope; } public void handleMessage(SoapMessage message) { if(message.getContent(Exception.class) != null) return; Document document = DomUtil.createDocument(); if(!useJBIWrapper) { SoapVersion soapVersion = message.getVersion(); if(useSOAPEnvelope) { Element soapEnv = DomUtil.createElement(document, new QName(soapVersion.getEnvelope().getNamespaceURI(), soapVersion.getEnvelope().getLocalPart(), soapVersion.getPrefix())); Element soapBody = DomUtil.createElement(soapEnv, new QName(soapVersion.getBody().getNamespaceURI(), soapVersion.getBody().getLocalPart(), soapVersion.getPrefix())); soapEnv.appendChild(soapBody); Element body = getBodyElement(message); if(body != null) soapBody.appendChild(soapBody.getOwnerDocument().importNode(body, true)); } else { Element body = getBodyElement(message); if(body != null) document.appendChild(document.importNode(body, true)); } } else { BindingOperationInfo wsdlOperation = getOperation(message); BindingMessageInfo wsdlMessage = isRequestor(message) ? wsdlOperation.getOutput() : wsdlOperation.getInput(); document = DomUtil.createDocument(); Element root = DomUtil.createElement(document, CxfJbiConstants.WSDL11_WRAPPER_MESSAGE); String typeNamespace = wsdlMessage.getMessageInfo().getName().getNamespaceURI(); if(typeNamespace == null || typeNamespace.length() == 0) throw new IllegalArgumentException("messageType namespace is null or empty"); root.setAttribute("xmlns:msg", typeNamespace); root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); String typeLocalName = wsdlMessage.getMessageInfo().getName().getLocalPart(); if(typeLocalName == null || typeLocalName.length() == 0) throw new IllegalArgumentException("messageType local name is null or empty"); root.setAttribute("type", (new StringBuilder()).append("msg:").append(typeLocalName).toString()); String messageName = wsdlMessage.getMessageInfo().getName().getLocalPart(); root.setAttribute("name", messageName); root.setAttribute("version", "1.0"); SoapBindingInfo binding = (SoapBindingInfo)((Endpoint)message.getExchange().get(Endpoint.class)).getEndpointInfo().getBinding(); String style = binding.getStyle(wsdlOperation.getOperationInfo()); if(style == null) style = binding.getStyle(); Element body = getBodyElement(message); if(body == null) return; if(body.getLocalName().equals("Fault")) { handleJBIFault(message, body); return; } List headers = wsdlMessage.getExtensors(SoapHeaderInfo.class); List headerElement = message.getHeaders(); List parts = new ArrayList(); Iterator i$; for(i$ = wsdlMessage.getMessageParts().iterator(); i$.hasNext();) { MessagePartInfo part = (MessagePartInfo)i$.next(); if("document".equals(style)) { parts.add(body); } else { Element param = DomUtil.getFirstChildElement(body); boolean found = false; do { if(param == null) break; if(part.getName().getLocalPart().equals(param.getLocalName())) { found = true; parts.add(wrapNodeList(param.getChildNodes())); break; } param = DomUtil.getNextSiblingElement(param); } while(true); if(!found) throw new Fault(new Exception((new StringBuilder()).append("Missing part '").append(part.getName()).append("'").toString())); } } processHeader(message, headers, headerElement, parts); i$ = parts.iterator(); do { if(!i$.hasNext()) break; Object part = i$.next(); if(part instanceof Node) addPart(root, (Node)part); else if(part instanceof NodeList) addPart(root, (NodeList)part); else if(part instanceof SoapHeader) addPart(root, (Node)((SoapHeader)part).getObject()); } while(true); } message.setContent(Source.class, new DOMSource(document)); } private void processHeader(SoapMessage message, List headers, List headerElement, List parts) { if(headers != null) { for(Iterator i$ = headers.iterator(); i$.hasNext();) { SoapHeaderInfo header = (SoapHeaderInfo)i$.next(); MessagePartInfo part = header.getPart(); Header param = findHeader(headerElement, part); int idx = part.getIndex(); if(idx > parts.size()) parts.add(param); else if(idx == -1) parts.add(0, param); else parts.add(idx, param); } } } private void handleJBIFault(SoapMessage message, Element soapFault) { try { Document doc = DomUtil.createDocument(); Element jbiFault = DomUtil.createElement(doc, new QName("http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper", "JBIFault")); Node jbiFaultDetail = null; if(message.getVersion() instanceof Soap11) { NodeList nodeList = soapFault.getElementsByTagName("faultcode"); String faultCode = nodeList.item(0).getFirstChild().getTextContent(); String prefix = faultCode.substring(0, faultCode.indexOf(":")); String localName = faultCode.substring(faultCode.indexOf(":") + 1); message.put("faultcode", new QName(prefix, localName)); nodeList = soapFault.getElementsByTagName("faultstring"); message.put("faultstring", nodeList.item(0).getFirstChild().getTextContent()); nodeList = soapFault.getElementsByTagName("detail"); if(nodeList != null && nodeList.getLength() > 0 && DomUtil.getFirstChildElement(nodeList.item(0)) != null) { jbiFaultDetail = doc.importNode(DomUtil.getFirstChildElement(nodeList.item(0)), true); } else { message.put("hasdetail", Boolean.valueOf(false)); nodeList = soapFault.getElementsByTagName("faultstring"); jbiFaultDetail = doc.importNode(nodeList.item(0).getFirstChild(), true); } } else { NodeList nodeList = soapFault.getElementsByTagName("soap:Code"); String faultCode = DomUtil.getFirstChildElement(nodeList.item(0)).getTextContent(); String prefix = faultCode.substring(0, faultCode.indexOf(":")); String localName = faultCode.substring(faultCode.indexOf(":") + 1); message.put("faultcode", new QName(prefix, localName)); nodeList = soapFault.getElementsByTagName("soap:Reason"); message.put("faultstring", DomUtil.getFirstChildElement(nodeList.item(0)).getTextContent()); nodeList = soapFault.getElementsByTagName("soap:Detail"); if(nodeList != null && nodeList.getLength() > 0 && DomUtil.getFirstChildElement(nodeList.item(0)) != null) { jbiFaultDetail = doc.importNode(DomUtil.getFirstChildElement(nodeList.item(0)), true); } else { message.put("hasdetail", Boolean.valueOf(false)); nodeList = soapFault.getElementsByTagName("faultstring"); jbiFaultDetail = doc.importNode(DomUtil.getFirstChildElement(nodeList.item(0)), true); } } SchemaInfo schemaInfo = getOperation(message).getBinding().getService().getSchema(jbiFaultDetail.getNamespaceURI()); if(schemaInfo != null && !schemaInfo.isElementFormQualified() && (jbiFaultDetail instanceof Element)) jbiFaultDetail = addEmptyDefaultTns((Element)jbiFaultDetail); jbiFault.appendChild(jbiFaultDetail); message.setContent(Source.class, new DOMSource(doc)); message.put("jbiFault", Boolean.valueOf(true)); } catch(Exception e) { throw new RuntimeException((new StringBuilder()).append("the fault message can't be parsed:").append(e.getMessage()).toString()); } } private Element addEmptyDefaultTns(Element ret) { if(!ret.hasAttribute("xmlns")) ret.setAttribute("xmlns", ""); NodeList nodes = ret.getChildNodes(); for(int i = 0; i < nodes.getLength(); i++) if(nodes.item(i) instanceof Element) { Element ele = (Element)nodes.item(i); ele = addEmptyDefaultTns(ele); } return ret; } private NodeList wrapNodeList(final NodeList childNodes) { return new NodeList() { public int getLength() { return childNodes.getLength(); } public Node item(int index) { return childNodes.item(index); } }; } protected BindingOperationInfo getOperation(Message message) { System.out.println("MICDIC:getOperation()"); if(message!=null) { System.out.println("MICDIC: type:"+message.toString()); System.out.println("MICDIC EXCHANGE:"); System.out.println(message.getExchange()); System.out.println(""); System.out.println("MICDIC EXCHANGE VALUES:"); System.out.println(message.getExchange().entrySet()); System.out.println(""); } else System.out.println("MICDIC: message null"); BindingOperationInfo operation = (BindingOperationInfo)message.getExchange().get(BindingOperationInfo.class); if(operation == null) throw new Fault(new Exception("Operation not bound on this message")); else return operation; } protected Element getBodyElement(SoapMessage message) { try { Document doc; XMLStreamReader xmlReader = (XMLStreamReader)message.getContent(XMLStreamReader.class); XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, message.getVersion().getBody()); StaxUtils.toNextElement((DepthXMLStreamReader)filteredReader); doc = DOMUtils.createDocument(); StaxUtils.readDocElements(doc, filteredReader, false); return doc.getDocumentElement(); } catch (XMLStreamException e) { throw new Fault(e); } } protected Header getHeaderElement(SoapMessage message, QName name) { Exchange exchange = message.getExchange(); BindingOperationInfo bop = (BindingOperationInfo)exchange.get(BindingOperationInfo.class); if(bop.isUnwrapped()) bop = bop.getWrappedOperation(); boolean client = isRequestor(message); BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput(); if(bmi == null) return null; List headers = bmi.getExtensors(SoapHeaderInfo.class); if(headers == null || headers.size() == 0) return null; List headerElement = message.getHeaders(); for(Iterator i$ = headers.iterator(); i$.hasNext();) { SoapHeaderInfo header = (SoapHeaderInfo)i$.next(); QName qname = header.getPart().isElement() ? header.getPart().getElementQName() : header.getPart().getTypeQName(); if(qname.equals(name)) { MessagePartInfo mpi = header.getPart(); return findHeader(headerElement, mpi); } } return null; } private static void addPart(Element parent, Node partValue) { Element element = DomUtil.createElement(parent, CxfJbiConstants.WSDL11_WRAPPER_PART); element.appendChild(element.getOwnerDocument().importNode(partValue, true)); } private static void addPart(Element parent, NodeList nodes) { Element element = DomUtil.createElement(parent, CxfJbiConstants.WSDL11_WRAPPER_PART); for(int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); element.appendChild(element.getOwnerDocument().importNode(node, true)); } } private static Header findHeader(List headerElement, MessagePartInfo mpi) { Header param = null; if(headerElement != null) { QName name = mpi.getConcreteName(); Iterator i$ = headerElement.iterator(); do { if(!i$.hasNext()) break; Header header = (Header)i$.next(); if(mpi.isElement()) { if(header.getName().getNamespaceURI() != null && header.getName().getNamespaceURI().equals(name.getNamespaceURI()) && header.getName().getLocalPart() != null && header.getName().getLocalPart().equals(name.getLocalPart())) param = header; } else if(header.getName().getLocalPart() != null && header.getName().getLocalPart().equals(name.getLocalPart())) param = header; } while(true); } return param; } }