Example usage for javax.xml.soap SOAPHeader getPrefix

List of usage examples for javax.xml.soap SOAPHeader getPrefix

Introduction

In this page you can find the example usage for javax.xml.soap SOAPHeader getPrefix.

Prototype

public String getPrefix();

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java

public void sign(final SOAPMessage message) {
    try {/*from  w w w  . j a v  a 2s. co  m*/
        loadCertificate();

        final long startMs = System.currentTimeMillis();

        final SOAPPart doc = message.getSOAPPart();
        final QName wsuId = doc.getEnvelope().createQName("Id", "wsu");
        final SOAPHeader header = message.getSOAPHeader();
        final QName actor = header.createQName("actor", header.getPrefix());

        final SOAPElement security = header.addChildElement("Security", "wsse", WSSE);
        security.addAttribute(actor, ACTOR_SMEV);
        SOAPElement binarySecurityToken = security.addChildElement("BinarySecurityToken", "wsse");
        binarySecurityToken.setAttribute("EncodingType", WSS_BASE64_BINARY);
        binarySecurityToken.setAttribute("ValueType", WSS_X509V3);
        binarySecurityToken.setValue(DatatypeConverter.printBase64Binary(cert.getEncoded()));
        binarySecurityToken.addAttribute(wsuId, "CertId");

        XMLSignature signature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_GOST_GOST3410_3411,
                Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
        {
            Element element = signature.getElement();
            Element keyInfo = doc.createElementNS(Constants.SignatureSpecNS, "KeyInfo");
            Element securityTokenReference = doc.createElementNS(WSSE, "SecurityTokenReference");
            Element reference = doc.createElementNS(WSSE, "Reference");
            reference.setAttribute("URI", "#CertId");
            reference.setAttribute("ValueType", WSS_X509V3);
            securityTokenReference.appendChild(reference);
            keyInfo.appendChild(securityTokenReference);
            element.appendChild(keyInfo);
            security.appendChild(element);
        }
        Transforms transforms = new Transforms(doc);
        transforms.addTransform(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
        signature.addDocument("#body", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_GOST3411);
        signature.sign(privateKey);
        if (log.isDebugEnabled()) {
            log.debug("SIGN: " + (System.currentTimeMillis() - startMs) + "ms");
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}