Каноникализатор XML с JAVA - PullRequest
0 голосов
/ 06 марта 2019

Мне нужно канонизировать часть XML ниже:

<test xmlns="http://www.test.org/H003" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Revision="1" Version="H003">
    <header authenticate="true">
        <static>
            <HostID>ABCDEF</HostID>
            <Nonce>123456</Nonce>
            <Timestamp>2017-02-20T14:41:38.965Z</Timestamp>
            <PartnerID>ED123</PartnerID>
            <UserID>0123</UserID>
            <Product InstituteID="XX" Language="fr">XYZ</Product>
            <OrderDetails>
                <OrderType>ABC</OrderType>
                <OrderAttribute>DDDD</OrderAttribute>
            </OrderDetails>
            <SecurityMedium>0000</SecurityMedium>
        </static>
        <mutable></mutable>
    </header>
    <AuthSignature>
        <ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
            <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:CanonicalizationMethod>
            <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"></ds:SignatureMethod>
            <ds:Reference URI="#xpointer(//*[@authenticate='true'])">
                <ds:Transforms>
                    <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:Transform>
                </ds:Transforms>
                <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"></ds:DigestMethod>
                <ds:DigestValue>abcdefghijklmnopqrstuvwxyz</ds:DigestValue>
            </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="X002">abcd123456</ds:SignatureValue>
    </AuthSignature>
    <body></body>
</test>

Для этого я использую Canonicalizer и XPathAPI:

private byte[] testCanon(Document document, String xpathString) throws XXXXException {
    byte[] retour;
    Node node;
    NodeIterator iter;
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {
      Canonicalizer canonicalizer = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);

      iter = XPathAPI.selectNodeIterator(document, xpathString);

      while ((node = iter.nextNode()) != null) {
        copierAttributs((Element) document.getFirstChild(), (Element) node);
        output.write(canonicalizer.canonicalizeSubtree(node));
      }

      retour = output.toByteArray();
    } catch (TransformerException | InvalidCanonicalizerException | CanonicalizationException | IOException e) {
      throw new XXXXException(e);
    }

return retour;
}

Я получил сообщение об ошибке «Префикс должен стать пространством имен: ds», когда я вызываю selectNodeIterator с моим значением xpathString, равным // ds: SignedInfo

если пространство имен xmlns: ds перемещается из SignedInfo в родительское, то все в порядке, тогда XML приходит от клиента, и я не могу попросить его изменить его ...

Спасибо

...