Dustin Sun
Dustin Sun

Reputation: 5532

Java XML DOM - how to use Element.getAttributeNS

I have XML node:

    <soapenv:Body 
        xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
        wsu:Id="Id-223">
         ....
    </soapenv:Body>

What values should I pass to org.w3c.dom.Element.getAttributeNS(namespaceURI, localName) to get the value of attribute Id?

Note I can use Element.getAttribute("wsu:Id") to get the value but I have to use getAttributeNS method.

Additional Info: org.apache.wss4j.dom.message.token.Timestamp uses following code:

public  String getID() {
   //WSConstants.WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    return element.getAttributeNS(WSConstants.WSU_NS, "Id");
}

I can confirm this piece of code does not work.

[my code] was copied from Dmitry's answer in this post. You can easily reproduce my error using the XML in question and validateSignature method in Dmitry's answer

File signatureFile = new File("/home/user1/Downloads/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(signatureFile);

Node securityNode = doc.getElementsByTagName("wsse:Security").item(0);
Node bodyNode = doc.getElementsByTagName("soap:Body").item(0);
validateSignature(securityNode, bodyNode, key);

Then

public boolean validateSignature(Node signatureNode, Node bodyTag, PublicKey publicKey) {
        boolean signatureIsValid = false;
        try {
            // Create a DOM XMLSignatureFactory that will be used to unmarshal the
            // document containing the XMLSignature
            String providerName = System.getProperty
                    ("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
            XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",
                    (Provider) Class.forName(providerName).newInstance());

            // Create a DOMValidateContext and specify a KeyValue KeySelector
            // and document context
            DOMValidateContext valContext = new DOMValidateContext(new X509KeySelector(publicKey), signatureNode);

            String nameSpaceURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
            org.w3c.dom.Element bodyEle = (Element) bodyTag;
            System.out.println(Main.nodeToString(bodyEle));

            valContext.setIdAttributeNS((Element) bodyTag, org.apache.ws.security.WSConstants.WSU_NS, "Id");



            // Unmarshal the XMLSignature.
            XMLSignature signature = fac.unmarshalXMLSignature(valContext);
            // Validate the XMLSignature.
            signatureIsValid = signature.validate(valContext);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return signatureIsValid;
    }

Upvotes: 1

Views: 438

Answers (1)

Dustin Sun
Dustin Sun

Reputation: 5532

It turned out that I missed this piece of code

dbFactory.setNamespaceAware(true);

Upvotes: 0

Related Questions