Example usage for javax.xml.soap SOAPPart getContent

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

Introduction

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

Prototype

public abstract Source getContent() throws SOAPException;

Source Link

Document

Returns the content of the SOAPEnvelope as a JAXP Source object.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
            "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(// w w w. j  a  v  a  2s.  c  o  m
            soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
            "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

    Source source = soapPart.getContent();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
            "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(/*from ww w. jav  a  2s.  c  o  m*/
            soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
            "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

    Source source = soapPart.getContent();

    Node root = null;
    if (source instanceof DOMSource) {
        root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
        InputSource inSource = ((SAXSource) source).getInputSource();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = null;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(inSource);
        root = (Node) doc.getDocumentElement();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
            "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(/*from   w  ww. ja va2 s.  c o  m*/
            soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
            "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

    Source source = soapPart.getContent();

    Node root = null;
    if (source instanceof DOMSource) {
        root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
        InputSource inSource = ((SAXSource) source).getInputSource();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = null;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(inSource);
        root = (Node) doc.getDocumentElement();
    }

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(new DOMSource(root), new StreamResult(System.out));
}

From source file:Signing.java

public static void main(String[] args) throws Exception {
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

        SOAPHeader soapHeader = soapEnvelope.getHeader();
        SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
                "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

        SOAPBody soapBody = soapEnvelope.getBody();
        soapBody.addAttribute(//from   w  w w . j a v a2s.co  m
                soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
                "Body");
        Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
        SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

        Source source = soapPart.getContent();
        Node root = null;
        if (source instanceof DOMSource) {
            root = ((DOMSource) source).getNode();
        } else if (source instanceof SAXSource) {
            InputSource inSource = ((SAXSource) source).getInputSource();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = null;

            db = dbf.newDocumentBuilder();

            Document doc = db.parse(inSource);
            root = (Node) doc.getDocumentElement();
        }

        dumpDocument(root);

        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(1024, new SecureRandom());
        KeyPair keypair = kpg.generateKeyPair();

        XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance();
        Reference ref = sigFactory.newReference("#Body", sigFactory.newDigestMethod(DigestMethod.SHA1, null));
        SignedInfo signedInfo = sigFactory.newSignedInfo(
                sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
                        (C14NMethodParameterSpec) null),
                sigFactory.newSignatureMethod(SignatureMethod.DSA_SHA1, null), Collections.singletonList(ref));
        KeyInfoFactory kif = sigFactory.getKeyInfoFactory();
        KeyValue kv = kif.newKeyValue(keypair.getPublic());
        KeyInfo keyInfo = kif.newKeyInfo(Collections.singletonList(kv));

        XMLSignature sig = sigFactory.newXMLSignature(signedInfo, keyInfo);

        System.out.println("Signing the message...");
        PrivateKey privateKey = keypair.getPrivate();
        Element envelope = getFirstChildElement(root);
        Element header = getFirstChildElement(envelope);
        DOMSignContext sigContext = new DOMSignContext(privateKey, header);
        sigContext.putNamespacePrefix(XMLSignature.XMLNS, "ds");
        sigContext.setIdAttributeNS(getNextSiblingElement(header),
                "http://schemas.xmlsoap.org/soap/security/2000-12", "id");
        sig.sign(sigContext);

        dumpDocument(root);

        System.out.println("Validate the signature...");
        Element sigElement = getFirstChildElement(header);
        DOMValidateContext valContext = new DOMValidateContext(keypair.getPublic(), sigElement);
        valContext.setIdAttributeNS(getNextSiblingElement(header),
                "http://schemas.xmlsoap.org/soap/security/2000-12", "id");
        boolean valid = sig.validate(valContext);

        System.out.println("Signature valid? " + valid);
    }

From source file:Main.java

private static DOMResult transform(SOAPPart part) throws Exception {
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    DOMResult rs = new DOMResult();
    trans.transform(part.getContent(), rs);

    return rs;/*  www .ja va2  s .  co m*/
}

From source file:it.cnr.icar.eric.common.SOAPMessenger.java

Reader processResponseBody(SOAPMessage response, String lookFor)
        throws JAXRException, SOAPException, TransformerConfigurationException, TransformerException {
    // grab info out of reply
    SOAPPart replyPart = response.getSOAPPart();
    Source replySource = replyPart.getContent();

    // transform//  w  w w. ja  v a2 s .  c  om
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer xFormer = tFactory.newTransformer();
    DOMResult domResult = new DOMResult();
    xFormer.transform(replySource, domResult);

    org.w3c.dom.Node node = domResult.getNode();

    while (node != null) {
        String nodeLocalName = node.getLocalName();

        if ((nodeLocalName != null) && (nodeLocalName.endsWith(lookFor))) {
            break;
        }

        node = nextNode(node);
    }

    if (node == null) {
        node = domResult.getNode();
        while (node != null) {
            String nodeLocalName = node.getLocalName();
            if ((nodeLocalName != null) && (nodeLocalName.endsWith(lookFor))) {
                break;
            }

            node = nextNode(node);
        }

        throw new JAXRException(resourceBundle.getString("message.elementNotFound", new String[] { lookFor }));
    }

    return domNode2StringReader(node);
}

From source file:it.cnr.icar.eric.server.common.Utility.java

public org.w3c.dom.Element getSignatureElement(SOAPMessage msg)
        throws javax.xml.soap.SOAPException, javax.xml.transform.TransformerException {
    org.w3c.dom.Element sigElement = null;

    javax.xml.transform.Transformer xFormer = javax.xml.transform.TransformerFactory.newInstance()
            .newTransformer();/*  w w  w. j  a  v  a2s.  co  m*/

    // grab info out of msg
    javax.xml.soap.SOAPPart msgPart = msg.getSOAPPart();
    javax.xml.transform.Source msgSource = msgPart.getContent();

    // transform
    javax.xml.transform.dom.DOMResult domResult = new javax.xml.transform.dom.DOMResult();
    xFormer.transform(msgSource, domResult);

    //root node is the soap:Envelope
    Node envelopeNode = domResult.getNode();

    //now you have the node. the following code strips off the envelope of
    //the soap message to get to the actual content
    // Advance to envelope node in case of text nodes preceding it
    while ((envelopeNode.getLocalName() == null)
            || (!envelopeNode.getLocalName().equalsIgnoreCase("envelope"))) {
        envelopeNode = envelopeNode.getFirstChild();
    }

    // Advance to header within envelope node
    Node headerNode = envelopeNode.getFirstChild();

    while ((headerNode.getLocalName() == null) || (!headerNode.getLocalName().equalsIgnoreCase("header"))) {
        headerNode = headerNode.getNextSibling();
    }

    //System.err.println("headerNode name is: " + headerNode.getLocalName());
    // Advance to signature node within header
    Node sigNode = headerNode.getFirstChild();

    if (sigNode == null) {
        return null;
    }

    //System.err.println("sigNode: " + sigNode);
    while ((sigNode.getLocalName() == null) || (!sigNode.getLocalName().equalsIgnoreCase("signature"))) {
        sigNode = sigNode.getNextSibling();

        if (sigNode == null) {
            return null;
        }
    }

    //Desired Signature element may be inside a SOAP-SEC signature element
    if (!sigNode.getNamespaceURI().equals("http://www.w3.org/2000/09/xmldsig#")) {
        sigNode = sigNode.getFirstChild();

        while ((sigNode.getLocalName() == null) || (!sigNode.getLocalName().equalsIgnoreCase("signature"))) {
            sigNode = sigNode.getNextSibling();
        }
    }

    if (sigNode.getNamespaceURI().equals("http://www.w3.org/2000/09/xmldsig#")) {
        if (sigNode instanceof org.w3c.dom.Element) {
            sigElement = (org.w3c.dom.Element) sigNode;
        }
    }

    return sigElement;
}