Example usage for javax.xml.soap SOAPPart getDocumentElement

List of usage examples for javax.xml.soap SOAPPart getDocumentElement

Introduction

In this page you can find the example usage for javax.xml.soap SOAPPart getDocumentElement.

Prototype

public Element getDocumentElement();

Source Link

Document

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

Usage

From source file:be.e_contract.mycarenet.sts.WSSecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws WSSecurityException {
    LOG.debug("adding WS-Security header");
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    WSSecHeader wsSecHeader = new WSSecHeader();
    wsSecHeader.insertSecurityHeader(soapPart);

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.setTimeToLive(60);/*from   w w w.j  av  a  2 s  .  co m*/
    wsSecTimeStamp.build(soapPart, wsSecHeader);

    WSSecurityCrypto crypto = new WSSecurityCrypto(this.privateKey, this.certificate);
    WSSConfig wssConfig = new WSSConfig();
    wssConfig.setWsiBSPCompliant(false);
    WSSecSignature sign = new WSSecSignature(wssConfig);
    sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
    sign.prepare(soapPart, crypto, wsSecHeader);
    String bstId = sign.getBSTTokenId();
    sign.appendBSTElementToHeader(wsSecHeader);
    Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
    SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
    signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
            soapConstants.getEnvelopeURI(), "Content"));
    signParts.add(new WSEncryptionPart(bstId));
    signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
    List<Reference> referenceList = sign.addReferencesToSign(signParts, wsSecHeader);
    sign.computeSignature(referenceList, false, null);
}

From source file:be.e_contract.mycarenet.async.SecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws SOAPException, WSSecurityException {
    LOG.debug("adding WS-Security header");
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    WSSecHeader wsSecHeader = new WSSecHeader();
    wsSecHeader.insertSecurityHeader(soapPart);

    WSSecUsernameToken usernameToken = new WSSecUsernameToken();
    usernameToken.setUserInfo(this.packageLicenseKey.getUsername(), this.packageLicenseKey.getPassword());
    usernameToken.setPasswordType(WSConstants.PASSWORD_TEXT);
    usernameToken.prepare(soapPart);/*from ww w. ja  v  a 2 s.c  om*/
    usernameToken.prependToHeader(wsSecHeader);

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.build(soapPart, wsSecHeader);

    WSSecurityCrypto crypto = new WSSecurityCrypto(this.sessionKey);
    WSSConfig wssConfig = new WSSConfig();
    wssConfig.setWsiBSPCompliant(false);
    WSSecSignature sign = new WSSecSignature(wssConfig);
    sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
    sign.prepare(soapPart, crypto, wsSecHeader);
    sign.appendBSTElementToHeader(wsSecHeader);
    Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
    signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
    signParts.add(new WSEncryptionPart(usernameToken.getId()));
    SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
    signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
            soapConstants.getEnvelopeURI(), "Content"));
    sign.addReferencesToSign(signParts, wsSecHeader);
    List<Reference> referenceList = sign.addReferencesToSign(signParts, wsSecHeader);
    sign.computeSignature(referenceList, false, null);
}

From source file:be.e_contract.mycarenet.ehealth.common.WSSecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context)
        throws WSSecurityException, SAXException, IOException {
    LOG.debug("adding WS-Security header");
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    WSSecHeader wsSecHeader = new WSSecHeader();
    wsSecHeader.insertSecurityHeader(soapPart);

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.setTimeToLive(60);//from  w  ww .jav  a 2s .  c  om
    wsSecTimeStamp.build(soapPart, wsSecHeader);

    Document assertionDocument = this.documentBuilder
            .parse(new InputSource(new StringReader(this.samlAssertion)));
    Element assertionElement = assertionDocument.getDocumentElement();
    String assertionId = assertionElement.getAttribute("AssertionID");
    Element importedAssertionElement = (Element) soapPart.importNode(assertionElement, true);
    Element securityHeaderElement = wsSecHeader.getSecurityHeader();
    securityHeaderElement.appendChild(importedAssertionElement);

    WSSecSignature wsSecSignature = new WSSecSignature();
    wsSecSignature.setSignatureAlgorithm(WSConstants.RSA);
    wsSecSignature.setKeyIdentifierType(WSConstants.CUSTOM_KEY_IDENTIFIER);
    wsSecSignature.setCustomTokenValueType(WSConstants.WSS_SAML_KI_VALUE_TYPE);
    wsSecSignature.setCustomTokenId(assertionId);
    Crypto crypto = new WSSecurityCrypto(this.privateKey, null);
    wsSecSignature.prepare(soapPart, crypto, wsSecHeader);
    Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
    SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
    signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
            soapConstants.getEnvelopeURI(), "Content"));
    signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
    List<Reference> referenceList = wsSecSignature.addReferencesToSign(signParts, wsSecHeader);
    wsSecSignature.computeSignature(referenceList, false, null);
}

From source file:be.fedict.trust.xkms2.WSSecurityServerHandler.java

/**
 * Handles the outbound SOAP message. Adds the WS Security Header containing
 * a signed timestamp, and signed SOAP body.
 *//*from  www.j a v  a  2 s.  com*/
private void handleOutboundDocument(SOAPPart soapPart, SOAPMessageContext soapMessageContext) {

    LOG.debug("handle outbound document");
    ServletContext servletContext = (ServletContext) soapMessageContext.get(MessageContext.SERVLET_CONTEXT);
    TrustService trustService = ServiceConsumerServletContextListener.getTrustService(servletContext);
    WSSecurityConfigEntity wsSecurityConfig = trustService.getWsSecurityConfig();

    if (wsSecurityConfig.isSigning()) {
        LOG.debug("adding WS-Security SOAP header");

        try {
            PrivateKeyEntry privateKeyEntry = KeyStoreUtils.loadPrivateKeyEntry(wsSecurityConfig);
            X509Certificate certificate = (X509Certificate) privateKeyEntry.getCertificate();
            PrivateKey privateKey = privateKeyEntry.getPrivateKey();

            WSSecHeader wsSecHeader = new WSSecHeader();
            wsSecHeader.insertSecurityHeader(soapPart);

            WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
            wsSecTimeStamp.setTimeToLive(0);
            wsSecTimeStamp.build(soapPart, wsSecHeader);

            ClientCrypto crypto = new ClientCrypto(certificate, privateKey);
            WSSConfig wssConfig = new WSSConfig();
            wssConfig.setWsiBSPCompliant(false);
            WSSecSignature sign = new WSSecSignature(wssConfig);
            sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
            sign.prepare(soapPart, crypto, wsSecHeader);
            sign.appendBSTElementToHeader(wsSecHeader);
            Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
            signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
            SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
            signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
                    soapConstants.getEnvelopeURI(), "Content"));
            sign.addReferencesToSign(signParts, wsSecHeader);
            List<Reference> referenceList = sign.addReferencesToSign(signParts, wsSecHeader);
            sign.computeSignature(referenceList, false, null);

        } catch (WSSecurityException e) {
            trustService.logAudit("WS-Security error: " + e.getMessage());
            throw new RuntimeException("WSS4J error: " + e.getMessage(), e);
        } catch (KeyStoreLoadException e) {
            trustService.logAudit("Load keystore error: " + e.getMessage());
            throw new RuntimeException("Failed to laod keystore: " + e.getMessage(), e);
        }
    }
}

From source file:be.e_contract.dssp.client.WSSecuritySOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws WSSecurityException, SOAPException {
    if (null == this.session && null == this.username) {
        return;/*from w  w w  .  j a v a  2s. c o m*/
    }
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    SOAPHeader soapHeader;
    try {
        soapHeader = soapMessage.getSOAPHeader();
    } catch (SOAPException e) {
        // WebSphere 8.5.5.1 work-around.
        soapHeader = null;
    }
    if (null == soapHeader) {
        /*
         * Work-around for Axis2.
         */
        SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
        soapHeader = soapEnvelope.addHeader();
    }

    WSSecHeader wsSecHeader = new WSSecHeader();
    Element securityElement = wsSecHeader.insertSecurityHeader(soapPart);

    if (null != this.session) {
        securityElement.appendChild(
                securityElement.getOwnerDocument().importNode(this.session.getSecurityTokenElement(), true));
    }

    WSSecTimestamp wsSecTimeStamp = new WSSecTimestamp();
    wsSecTimeStamp.setTimeToLive(60);
    wsSecTimeStamp.build(soapPart, wsSecHeader);

    if (null != this.username) {
        WSSecUsernameToken usernameToken = new WSSecUsernameToken();
        usernameToken.setUserInfo(this.username, this.password);
        usernameToken.setPasswordType(WSConstants.PASSWORD_TEXT);
        usernameToken.prepare(soapPart);
        usernameToken.prependToHeader(wsSecHeader);
    }

    if (null != this.session) {
        // work-around for WebSphere
        WSSConfig wssConfig = new WSSConfig();
        wssConfig.setWsiBSPCompliant(false);

        WSSecSignature wsSecSignature = new WSSecSignature(wssConfig);
        wsSecSignature.setSignatureAlgorithm(WSConstants.HMAC_SHA1);
        wsSecSignature.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING);
        wsSecSignature.setCustomTokenId(this.session.getSecurityTokenElement().getAttributeNS(
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id"));
        wsSecSignature.setSecretKey(this.session.getKey());
        wsSecSignature.prepare(soapPart, null, wsSecHeader);
        Vector<WSEncryptionPart> signParts = new Vector<WSEncryptionPart>();
        SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(soapPart.getDocumentElement());
        signParts.add(new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
                soapConstants.getEnvelopeURI(), "Content"));
        signParts.add(new WSEncryptionPart(wsSecTimeStamp.getId()));
        List<Reference> referenceList = wsSecSignature.addReferencesToSign(signParts, wsSecHeader);
        wsSecSignature.computeSignature(referenceList, false, null);
    }

    /*
     * Really needs to be at the end for Axis2 to work. Axiom bug?
     */
    appendSecurityHeader(soapHeader, securityElement);
}

From source file:test.integ.be.fedict.hsm.ws.WSSecurityTestSOAPHandler.java

private void handleOutboundMessage(SOAPMessageContext context) throws SOAPException,
        DatatypeConfigurationException, CertificateEncodingException, DOMException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, MarshalException, XMLSignatureException, NoSuchProviderException {
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    Element soapEnvelopeElement = soapPart.getDocumentElement();
    String soapPrefix = soapEnvelopeElement.getPrefix();
    LOG.debug("SOAP prefix: " + soapPrefix);
    Element soapHeaderElement = soapPart.createElementNS(SOAP_NAMESPACE, soapPrefix + ":Header");
    Element soapBodyElement = (Element) soapEnvelopeElement.getFirstChild();
    soapBodyElement.setAttributeNS(XMLNS_NS, "xmlns:wsu", WSU_NAMESPACE);
    soapBodyElement.setAttributeNS(WSU_NAMESPACE, "wsu:Id", "Body");
    soapEnvelopeElement.insertBefore(soapHeaderElement, soapBodyElement);

    LOG.debug("adding WS-Security SOAP header");
    Element wsSecurityHeaderElement = soapPart.createElementNS(WSSE_NAMESPACE, "wsse:Security");
    soapHeaderElement.appendChild(wsSecurityHeaderElement);
    wsSecurityHeaderElement.setAttributeNS(XMLNS_NS, "xmlns:wsse", WSSE_NAMESPACE);
    wsSecurityHeaderElement.setAttributeNS(XMLNS_NS, "xmlns:wsu", WSU_NAMESPACE);
    wsSecurityHeaderElement.setAttributeNS(SOAP_NAMESPACE, soapPrefix + ":mustUnderstand", "true");

    Element tsElement = addTimestamp(wsSecurityHeaderElement);
    addBinarySecurityToken(wsSecurityHeaderElement);
    addSignature(wsSecurityHeaderElement, tsElement, soapBodyElement);
}