Search in sources :

Example 76 with CachedOutputStream

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

the class XSLTInInterceptor method transformXReader.

protected void transformXReader(Message message, XMLStreamReader xReader) {
    CachedOutputStream cachedOS = new CachedOutputStream();
    try {
        StaxUtils.copy(xReader, cachedOS);
        InputStream transformedIS = XSLTUtils.transform(getXSLTTemplate(), cachedOS.getInputStream());
        XMLStreamReader transformedReader = StaxUtils.createXMLStreamReader(transformedIS);
        message.setContent(XMLStreamReader.class, transformedReader);
    } catch (XMLStreamException e) {
        throw new Fault("STAX_COPY", LOG, e, e.getMessage());
    } catch (IOException e) {
        throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage());
    } finally {
        try {
            StaxUtils.close(xReader);
        } catch (XMLStreamException ex) {
            throw new Fault(ex);
        }
        try {
            cachedOS.close();
        } catch (IOException e) {
            LOG.warning("Cannot close stream after transformation: " + e.getMessage());
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 77 with CachedOutputStream

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

the class XSLTUtils method transform.

public static InputStream transform(Templates xsltTemplate, InputStream in) {
    try (InputStream inputStream = in;
        CachedOutputStream out = new CachedOutputStream()) {
        Source beforeSource = new StaxSource(StaxUtils.createXMLStreamReader(inputStream));
        Transformer trans = xsltTemplate.newTransformer();
        trans.transform(beforeSource, new StreamResult(out));
        return out.getInputStream();
    } 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());
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StaxSource(org.apache.cxf.staxutils.StaxSource) 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) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 78 with CachedOutputStream

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

the class AttachmentUtilTest method numberStringAsAttachmentMaxSize.

@Test
public void numberStringAsAttachmentMaxSize() throws IOException {
    CachedOutputStream cos = createMock(CachedOutputStream.class);
    cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, "12345", cos);
    replay(cos);
    cos.setMaxSize(12345);
    cos.setThreshold(102400L);
    verify(cos);
}
Also used : CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 79 with CachedOutputStream

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

the class AttachmentUtilTest method stringAsAttachmentDirectory.

@Test
public void stringAsAttachmentDirectory() throws IOException {
    String attachmentDirectory = "/dev/null";
    CachedOutputStream cos = createMock(CachedOutputStream.class);
    cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_DIRECTORY, attachmentDirectory, cos);
    replay(cos);
    cos.setOutputDir(new File(attachmentDirectory));
    cos.setThreshold(102400L);
    verify(cos);
}
Also used : File(java.io.File) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 80 with CachedOutputStream

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

the class AttachmentUtilTest method shortAsAttachmentMaxSize.

@Test
public void shortAsAttachmentMaxSize() throws IOException {
    CachedOutputStream cos = createMock(CachedOutputStream.class);
    cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, Short.MAX_VALUE, cos);
    replay(cos);
    cos.setMaxSize(Short.MAX_VALUE);
    cos.setThreshold(102400L);
    verify(cos);
}
Also used : CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Aggregations

CachedOutputStream (org.apache.cxf.io.CachedOutputStream)105 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 Test (org.junit.Test)24 Message (org.apache.cxf.message.Message)22 OutputStream (java.io.OutputStream)18 Fault (org.apache.cxf.interceptor.Fault)18 MessageImpl (org.apache.cxf.message.MessageImpl)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)10 PrintWriter (java.io.PrintWriter)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 StreamSource (javax.xml.transform.stream.StreamSource)6 Endpoint (org.apache.cxf.endpoint.Endpoint)6 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)6 RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Source (javax.xml.transform.Source)5 ArrayList (java.util.ArrayList)4