Example usage for javax.xml.soap SOAPPart createElementNS

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

Introduction

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

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

From source file:com.googlecode.ddom.saaj.SOAPPartTest.java

@Validated
@Test//from  ww w. j a va 2  s.c o  m
public void testCreateEnvelopeWithCreateElementNS() throws Exception {
    SOAPPart soapPart = factory.createMessage().getSOAPPart();
    Element element = soapPart.createElementNS(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE, "SOAP-ENV:Envelope");
    assertTrue(element instanceof SOAPEnvelope);
    soapPart.appendChild(element);
    SOAPEnvelope envelope = soapPart.getEnvelope();
    assertNotNull(envelope);
}

From source file:com.googlecode.ddom.saaj.SOAPPartTest.java

@Validated
@Test(expected = SOAPException.class)
public void testSetParentElement() throws Exception {
    SOAPPart soapPart = factory.createMessage().getSOAPPart();
    SOAPElement element = (SOAPElement) soapPart.createElementNS("urn:ns", "p:root");
    soapPart.setParentElement(element);//  w ww .  j  a  va2  s  .c  om
}

From source file:com.googlecode.ddom.saaj.SOAPMessageTest.java

/**
 * Tests that {@link SOAPMessage#writeTo(java.io.OutputStream)} performs namespace repairing.
 * /*from  www . ja v a 2 s.c  o m*/
 * @throws Exception
 */
@Validated
@Test
public void testWriteToNamespaceRepairing() throws Exception {
    SOAPMessage message = getFactory().createMessage();
    SOAPPart part = message.getSOAPPart();
    SOAPBody body = part.getEnvelope().getBody();
    body.appendChild(part.createElementNS("urn:ns", "p:test"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    message.writeTo(baos);
    String content = baos.toString("UTF-8");
    assertTrue(content.contains("<p:test xmlns:p=\"urn:ns\"/>"));
}

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

public void sign(final SOAPMessage message) {
    try {/*w ww .  j  a  v a 2 s  .  c o  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);
    }
}

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);
}