Search in sources :

Example 21 with CachedOutputStream

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

the class AttachmentUtilTest method bigIntAsAttachmentMemoryThreshold.

@Test
public void bigIntAsAttachmentMemoryThreshold() throws IOException {
    BigInteger bigInteger = new BigInteger(String.valueOf(Long.MAX_VALUE));
    try (CachedOutputStream cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, bigInteger)) {
        assertEquals(bigInteger.longValue(), cos.getThreshold());
    }
    // Overflow long value
    bigInteger = bigInteger.add(BigInteger.ONE);
    try (CachedOutputStream cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, bigInteger)) {
        assertEquals(AttachmentDeserializer.THRESHOLD, cos.getThreshold());
    }
}
Also used : BigInteger(java.math.BigInteger) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 22 with CachedOutputStream

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

the class AttachmentUtilTest method bigIntAsAttachmentMaxSize.

@Test
public void bigIntAsAttachmentMaxSize() throws IOException {
    CachedOutputStream cos = createMock(CachedOutputStream.class);
    BigInteger bigInteger = new BigInteger(String.valueOf(Long.MAX_VALUE));
    cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, bigInteger, cos);
    replay(cos);
    cos.setMaxSize(bigInteger.longValue());
    cos.setThreshold(102400L);
    verify(cos);
    // Overflow long value
    bigInteger = bigInteger.add(BigInteger.ONE);
    cos = createMock(CachedOutputStream.class);
    cos = testSetStreamedAttachmentProperties(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, bigInteger, cos);
    replay(cos);
    cos.setThreshold(102400L);
    verify(cos);
}
Also used : BigInteger(java.math.BigInteger) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 23 with CachedOutputStream

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

the class JAXRSOutInterceptor method checkCachedStream.

private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled) throws Exception {
    final XMLStreamWriter writer;
    if (enabled) {
        writer = m.getContent(XMLStreamWriter.class);
    } else {
        writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
    }
    if (writer instanceof CachingXmlEventWriter) {
        CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
        if (!cache.getEvents().isEmpty()) {
            XMLStreamWriter origWriter = null;
            try {
                origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
                for (XMLEvent event : cache.getEvents()) {
                    StaxUtils.writeEvent(event, origWriter);
                }
            } finally {
                StaxUtils.close(origWriter);
            }
        }
        m.setContent(XMLStreamWriter.class, null);
        return;
    }
    if (enabled) {
        OutputStream os = m.getContent(OutputStream.class);
        if (os != osOriginal && os instanceof CachedOutputStream) {
            CachedOutputStream cos = (CachedOutputStream) os;
            if (cos.size() != 0) {
                cos.writeCacheTo(osOriginal);
            }
        }
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) XMLEvent(javax.xml.stream.events.XMLEvent) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 24 with CachedOutputStream

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

the class JAXRSOutInterceptor method checkBufferingMode.

private boolean checkBufferingMode(Message m, List<WriterInterceptor> writers, boolean firstTry) {
    if (!firstTry) {
        return false;
    }
    WriterInterceptor last = writers.get(writers.size() - 1);
    MessageBodyWriter<Object> w = ((WriterInterceptorMBW) last).getMBW();
    Object outBuf = m.getContextualProperty(OUT_BUFFERING);
    boolean enabled = PropertyUtils.isTrue(outBuf);
    boolean configurableProvider = w instanceof AbstractConfigurableProvider;
    if (!enabled && outBuf == null && configurableProvider) {
        enabled = ((AbstractConfigurableProvider) w).getEnableBuffering();
    }
    if (enabled) {
        boolean streamingOn = configurableProvider && ((AbstractConfigurableProvider) w).getEnableStreaming();
        if (streamingOn) {
            m.setContent(XMLStreamWriter.class, new CachingXmlEventWriter());
        } else {
            m.setContent(OutputStream.class, new CachedOutputStream());
        }
    }
    return enabled;
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) AbstractConfigurableProvider(org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 25 with CachedOutputStream

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

the class PersistInInterceptor method getSoapRequest.

/**
 * Copied from LoggingInInterceptor
 *
 * @param soapMessage
 */
private void getSoapRequest(Message soapMessage, ExchangeData exchange) {
    InputStream is = soapMessage.getContent(InputStream.class);
    if (is != null) {
        CachedOutputStream bos = new CachedOutputStream();
        try {
            IOUtils.copy(is, bos);
            bos.flush();
            is.close();
            soapMessage.setContent(InputStream.class, bos.getInputStream());
            StringBuilder builder = new StringBuilder();
            bos.writeCacheTo(builder, bos.size());
            bos.close();
            exchange.setRequest(builder.toString());
            exchange.setRequestSize((int) bos.size());
        } catch (IOException e) {
            throw new Fault(e);
        }
    }
}
Also used : InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

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