The earlier submission had a possible NullPointerException waiting to
happen; I've cleaned it up. The same is true of the current
SaajMarshaller.toNMS() method - a check should be performed on the result of
parent.getAttributes() before any further action is taken, or a
NullPointerException may be raised.

/jonathan

Code Follows

    public void toNMS(NormalizedMessage normalizedMessage, HttpMethod
method) throws Exception {
        addNmsProperties(normalizedMessage, method);
        String response = method.getResponseBodyAsString();
        Node node = sourceTransformer.toDOMNode(new StringSource(response));
        CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
        NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
                "/*/*[local-name()='Body']/*");
        Node root = iterator.nextNode();

        /*
         * Copy embedded namespaces from the envelope into the body,
similarly
         * to the SaajMarshaller.toNMS() method.
         */
        for (Node parent = root.getParentNode(); parent != null; parent =
parent.getParentNode()) {
            NamedNodeMap attributes = parent.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr att = (Attr) attributes.item(i);
                    if (att.getName().startsWith("xmlns:")
                            &&
root.getAttributes().getNamedItemNS(att.getNamespaceURI(),
                                    att.getLocalName()) == null) {
                        if (root.getNodeType() == Node.ELEMENT_NODE) {
                            ((Element)
root).setAttributeNS(att.getNamespaceURI(),
                                    att.getName(),
                                    att.getValue());
                        }
                    }
                }
            }
        }

        normalizedMessage.setContent(new DOMSource(root));
    }  

Reply via email to