public void toNMS()

in common/servicemix-components/src/main/java/org/apache/servicemix/components/saaj/SaajMarshaler.java [58:117]


    public void toNMS(NormalizedMessage normalizedMessage, SOAPMessage soapMessage) throws MessagingException, SOAPException {

        if (logger.isDebugEnabled()) {
        	try {
	            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
	            soapMessage.writeTo(buffer);
	            logger.debug(new String(buffer.toByteArray()));
        	} catch (Exception e) { }
        }
        
    	addNmsProperties(normalizedMessage, soapMessage);

        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPBody soapBody = soapPart.getEnvelope().getBody();
        SOAPElement elem = null;
        for (Iterator it = soapBody.getChildElements(); it.hasNext();) {
        	Object child =  it.next();
        	if (child instanceof SOAPElement) {
        		elem = (SOAPElement) child;
        		break;
        	}
        }
        if (elem == null) {
        	throw new IllegalStateException("Could not find any element in soap body");
        }
        
        for (SOAPElement parent = elem.getParentElement(); parent != null; parent = parent.getParentElement()) {
        	// The following code works with sun saaj implementation
		    NamedNodeMap attributes = parent.getAttributes();
		    if (attributes != null) {
		        for (int i = 0; i < attributes.getLength(); i++) {
		            Attr att = (Attr) parent.getAttributes().item(i);
		            if (att.getName().startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")
		                    && elem.getAttributeNodeNS(att.getNamespaceURI(), att.getLocalName()) == null) {
		        		elem.addNamespaceDeclaration(att.getName().substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1), att.getValue());
		                elem.setAttributeNS(att.getNamespaceURI(), att.getName(), att.getValue());
		            }
		        }
		    }
		    // The following code works with axis saaj implementation
        	for (Iterator itNs = parent.getNamespacePrefixes(); itNs.hasNext();) {
        		String prefix = (String) itNs.next();
        		String nsuri = parent.getNamespaceURI(prefix);
        		if (elem.getAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix) == null) {
	        		elem.addNamespaceDeclaration(prefix, nsuri);
	        		elem.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, nsuri);
        		}
        	}
		}
        
        if (logger.isDebugEnabled()) {
        	try {
        		logger.debug(transformer.toString(elem));
        	} catch (Exception e) { }
        }
        
        normalizedMessage.setContent(new DOMSource(elem));

        addNmsAttachments(normalizedMessage, soapMessage);
    }