Search in sources :

Example 26 with DOMResult

use of javax.xml.transform.dom.DOMResult in project webservices-axiom by apache.

the class SoapActionTest method runTest.

@Override
protected void runTest() throws Throwable {
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document requestDocument = documentBuilder.newDocument();
    Element request = requestDocument.createElementNS("urn:test", "p:Echo");
    request.setTextContent("Hello");
    Document responseDocument = documentBuilder.newDocument();
    context.getBean(WebServiceTemplate.class).sendSourceAndReceiveToResult(new DOMSource(request), new SoapActionCallback("http://www.example.com/echo"), new DOMResult(responseDocument));
    Element response = responseDocument.getDocumentElement();
    assertEquals("urn:test", response.getNamespaceURI());
    assertEquals("Echo", response.getLocalName());
    assertEquals("Hello", response.getTextContent());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SoapActionCallback(org.springframework.ws.soap.client.core.SoapActionCallback) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate)

Example 27 with DOMResult

use of javax.xml.transform.dom.DOMResult in project webservices-axiom by apache.

the class WSAddressingDOMTest method runTest.

@Override
protected void runTest() throws Throwable {
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document requestDocument = documentBuilder.newDocument();
    Element request = requestDocument.createElementNS("urn:test", "p:testRequest");
    request.setTextContent("test");
    Document responseDocument = documentBuilder.newDocument();
    context.getBean(WebServiceTemplate.class).sendSourceAndReceiveToResult(new DOMSource(request), new ActionCallback(EchoEndpoint.ACTION), new DOMResult(responseDocument));
    Element response = responseDocument.getDocumentElement();
    assertEquals("urn:test", response.getNamespaceURI());
    assertEquals("testRequest", response.getLocalName());
    assertEquals("test", response.getTextContent());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ActionCallback(org.springframework.ws.soap.addressing.client.ActionCallback) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate)

Example 28 with DOMResult

use of javax.xml.transform.dom.DOMResult in project webservices-axiom by apache.

the class TestTransformerWithIdentityStylesheet method runTest.

protected void runTest() throws Throwable {
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.newDocument();
    Element root = document.createElementNS("", "root");
    Element element = document.createElementNS("urn:mynamespace", "element1");
    element.setAttribute("att", "testValue");
    element.appendChild(document.createTextNode("test"));
    root.appendChild(element);
    document.appendChild(root);
    Document stylesheet = builder.parse(TestTransformerWithIdentityStylesheet.class.getResourceAsStream("identity.xslt"));
    Document output = builder.newDocument();
    Transformer transformer = xsltImplementation.newTransformerFactory().newTransformer(new DOMSource(stylesheet));
    transformer.transform(new DOMSource(document), new DOMResult(output));
    assertAbout(xml()).that(xml(Document.class, output)).ignoringNamespaceDeclarations().hasSameContentAs(xml(Document.class, document));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 29 with DOMResult

use of javax.xml.transform.dom.DOMResult in project ddf by codice.

the class XacmlClient method addNamespaceAndPrefixes.

/**
     * Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
     * XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
     * response.
     *
     * @param xacmlResponse The XACML response as a string.
     * @return DOM representation of the XACML response with namespaces and namespace prefixes.
     * @throws PdpException
     */
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
    XMLReader xmlReader = null;
    try {
        XMLReader xmlParser = XMLReaderFactory.createXMLReader();
        xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader = new XMLFilterImpl(xmlParser) {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
            }
        };
    } catch (SAXException e) {
        String message = "Unable to read XACML response:\n" + xacmlResponse;
        LOGGER.info(message);
        throw new PdpException(message, e);
    }
    DOMResult domResult;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        domResult = new DOMResult();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))), domResult);
    } catch (TransformerException e) {
        String message = "Unable to transform XACML response:\n" + xacmlResponse;
        LOGGER.info(message);
        throw new PdpException(message, e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    return domResult;
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 30 with DOMResult

use of javax.xml.transform.dom.DOMResult in project ddf by codice.

the class XacmlClient method evaluate.

/**
     * Evaluates the XACML request and returns a XACML response.
     *
     * @param xacmlRequestType XACML request
     * @return XACML response
     * @throws PdpException
     */
public ResponseType evaluate(RequestType xacmlRequestType) throws PdpException {
    String xacmlRequest = this.marshal(xacmlRequestType);
    String xacmlResponse = this.callPdp(xacmlRequest);
    LOGGER.debug("\nXACML 3.0 Response from XACML PDP:\n {}", xacmlResponse);
    DOMResult domResult = addNamespaceAndPrefixes(xacmlResponse);
    return unmarshal(domResult);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult)

Aggregations

DOMResult (javax.xml.transform.dom.DOMResult)61 Document (org.w3c.dom.Document)33 DOMSource (javax.xml.transform.dom.DOMSource)24 Transformer (javax.xml.transform.Transformer)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 InputSource (org.xml.sax.InputSource)13 StreamSource (javax.xml.transform.stream.StreamSource)12 StringReader (java.io.StringReader)11 SAXResult (javax.xml.transform.sax.SAXResult)10 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)10 SAXSource (javax.xml.transform.sax.SAXSource)9 StreamResult (javax.xml.transform.stream.StreamResult)9 Test (org.junit.Test)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Source (javax.xml.transform.Source)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 InputStream (java.io.InputStream)6