Search in sources :

Example 1 with CachedWriter

use of org.apache.cxf.io.CachedWriter in project cxf by apache.

the class XSLTInterceptorsTest method outXMLStreamTest.

@Test
public void outXMLStreamTest() throws XMLStreamException, SAXException, IOException, ParserConfigurationException {
    CachedWriter cWriter = new CachedWriter();
    cWriter.holdTempFile();
    XMLStreamWriter xWriter = StaxUtils.createXMLStreamWriter(cWriter);
    message.setContent(XMLStreamWriter.class, xWriter);
    outInterceptor.handleMessage(message);
    XMLStreamWriter tXWriter = message.getContent(XMLStreamWriter.class);
    StaxUtils.copy(new StreamSource(messageIS), tXWriter);
    tXWriter.close();
    cWriter.releaseTempFileHold();
    Document doc = StaxUtils.read(cWriter.getReader());
    Assert.assertTrue("Message was not transformed", checkTransformedXML(doc));
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) CachedWriter(org.apache.cxf.io.CachedWriter) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with CachedWriter

use of org.apache.cxf.io.CachedWriter in project cxf by apache.

the class LoggingInInterceptor method logReader.

protected void logReader(Message message, Reader reader, LoggingMessage buffer) {
    try {
        CachedWriter writer = new CachedWriter();
        IOUtils.copyAndCloseInput(reader, writer);
        message.setContent(Reader.class, writer.getReader());
        if (writer.getTempFile() != null) {
            // large thing on disk...
            buffer.getMessage().append("\nMessage (saved to tmp file):\n");
            buffer.getMessage().append("Filename: " + writer.getTempFile().getAbsolutePath() + "\n");
        }
        if (writer.size() > limit && limit != -1) {
            buffer.getMessage().append("(message truncated to " + limit + " bytes)\n");
        }
        writer.writeCacheTo(buffer.getPayload(), limit);
    } catch (Exception e) {
        throw new Fault(e);
    }
}
Also used : CachedWriter(org.apache.cxf.io.CachedWriter)

Example 3 with CachedWriter

use of org.apache.cxf.io.CachedWriter in project cxf by apache.

the class XSLTInterceptorsTest method outWriterStreamTest.

@Test
public void outWriterStreamTest() throws Exception {
    CachedWriter cWriter = new CachedWriter();
    message.setContent(Writer.class, cWriter);
    outInterceptor.handleMessage(message);
    Writer tWriter = message.getContent(Writer.class);
    IOUtils.copy(new InputStreamReader(messageIS), tWriter, IOUtils.DEFAULT_BUFFER_SIZE);
    tWriter.close();
    Document doc = StaxUtils.read(cWriter.getReader());
    Assert.assertTrue("Message was not transformed", checkTransformedXML(doc));
}
Also used : InputStreamReader(java.io.InputStreamReader) CachedWriter(org.apache.cxf.io.CachedWriter) Document(org.w3c.dom.Document) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) CachedWriter(org.apache.cxf.io.CachedWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with CachedWriter

use of org.apache.cxf.io.CachedWriter in project cxf by apache.

the class XSLTOutInterceptor method transformXWriter.

protected void transformXWriter(Message message, XMLStreamWriter xWriter) {
    CachedWriter writer = new CachedWriter();
    XMLStreamWriter delegate = StaxUtils.createXMLStreamWriter(writer);
    XSLTStreamWriter wrapper = new XSLTStreamWriter(getXSLTTemplate(), writer, delegate, xWriter);
    message.setContent(XMLStreamWriter.class, wrapper);
    message.put(AbstractOutDatabindingInterceptor.DISABLE_OUTPUTSTREAM_OPTIMIZATION, Boolean.TRUE);
}
Also used : DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) CachedWriter(org.apache.cxf.io.CachedWriter)

Example 5 with CachedWriter

use of org.apache.cxf.io.CachedWriter in project cxf by apache.

the class XSLTUtils method transform.

public static Reader transform(Templates xsltTemplate, Reader inReader) {
    CachedWriter outWriter = new CachedWriter();
    try {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(inReader);
        Source beforeSource = new StaxSource(reader);
        Transformer trans = xsltTemplate.newTransformer();
        trans.transform(beforeSource, new StreamResult(outWriter));
        return outWriter.getReader();
    } catch (IOException e) {
        throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage());
    } catch (TransformerException e) {
        throw new Fault("XML_TRANSFORM", LOG, e, e.getMessage());
    } finally {
        try {
            inReader.close();
        } catch (Exception e) {
        // ignore
        }
        try {
            outWriter.close();
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StaxSource(org.apache.cxf.staxutils.StaxSource) CachedWriter(org.apache.cxf.io.CachedWriter) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException)

Aggregations

CachedWriter (org.apache.cxf.io.CachedWriter)6 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)3 Test (org.junit.Test)2 Document (org.w3c.dom.Document)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Writer (java.io.Writer)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Fault (org.apache.cxf.interceptor.Fault)1 DelegatingXMLStreamWriter (org.apache.cxf.staxutils.DelegatingXMLStreamWriter)1 StaxSource (org.apache.cxf.staxutils.StaxSource)1