Search in sources :

Example 86 with CachedOutputStream

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

the class LoggingInInterceptor method logInputStream.

protected void logInputStream(Message message, InputStream is, LoggingMessage buffer, String encoding, String ct) {
    CachedOutputStream bos = new CachedOutputStream();
    if (threshold > 0) {
        bos.setThreshold(threshold);
    }
    try {
        // use the appropriate input stream and restore it later
        InputStream bis = is instanceof DelegatingInputStream ? ((DelegatingInputStream) is).getInputStream() : is;
        // only copy up to the limit since that's all we need to log
        // we can stream the rest
        IOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);
        bos.flush();
        bis = new SequenceInputStream(bos.getInputStream(), bis);
        // restore the delegating input stream or the input stream
        if (is instanceof DelegatingInputStream) {
            ((DelegatingInputStream) is).setInputStream(bis);
        } else {
            message.setContent(InputStream.class, bis);
        }
        if (bos.getTempFile() != null) {
            // large thing on disk...
            buffer.getMessage().append("\nMessage (saved to tmp file):\n");
            buffer.getMessage().append("Filename: " + bos.getTempFile().getAbsolutePath() + "\n");
        }
        boolean truncated = false;
        if (bos.size() > limit && limit != -1) {
            buffer.getMessage().append("(message truncated to " + limit + " bytes)\n");
            truncated = true;
        }
        writePayload(buffer.getPayload(), bos, encoding, ct, truncated);
        bos.close();
    } catch (Exception e) {
        throw new Fault(e);
    }
}
Also used : SequenceInputStream(java.io.SequenceInputStream) DelegatingInputStream(org.apache.cxf.io.DelegatingInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) DelegatingInputStream(org.apache.cxf.io.DelegatingInputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 87 with CachedOutputStream

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

the class LoggingOutInterceptorTest method handleAndGetCachedOutputStream.

private CachedOutputStream handleAndGetCachedOutputStream(LoggingOutInterceptor interceptor) {
    interceptor.setPrintWriter(new PrintWriter(new ByteArrayOutputStream()));
    Endpoint endpoint = control.createMock(Endpoint.class);
    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    BindingInfo bindingInfo = control.createMock(BindingInfo.class);
    EasyMock.expect(endpointInfo.getBinding()).andReturn(bindingInfo).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(bindingInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    control.replay();
    Message message = new MessageImpl();
    ExchangeImpl exchange = new ExchangeImpl();
    message.setExchange(exchange);
    exchange.put(Endpoint.class, endpoint);
    message.put(Message.CONTENT_TYPE, "application/xml");
    message.setContent(OutputStream.class, new ByteArrayOutputStream());
    interceptor.handleMessage(message);
    OutputStream os = message.getContent(OutputStream.class);
    assertTrue(os instanceof CachedOutputStream);
    return (CachedOutputStream) os;
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) BindingInfo(org.apache.cxf.service.model.BindingInfo) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) PrintWriter(java.io.PrintWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 88 with CachedOutputStream

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

the class LoggingOutInterceptorTest method testPrettyLoggingWithEncoding.

@Test
public void testPrettyLoggingWithEncoding() throws Exception {
    control.replay();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(baos);
    LoggingOutInterceptor p = new LoggingOutInterceptor(pw);
    p.setPrettyLogging(true);
    CachedOutputStream cos = new CachedOutputStream();
    String s = "<today><is><the><twenty> <second> <of> <january> <two> <thousand> <and> <nine></nine> " + "</and></thousand></two></january></of></second></twenty></the></is></today>";
    cos.write(s.getBytes());
    Message message = new MessageImpl();
    message.setExchange(new ExchangeImpl());
    message.put(Message.CONTENT_TYPE, "application/xml");
    message.put(Message.ENCODING, "UTF-8");
    Logger logger = LogUtils.getL7dLogger(this.getClass());
    LoggingOutInterceptor.LoggingCallback l = p.new LoggingCallback(logger, message, cos);
    l.onClose(cos);
    String str = baos.toString();
    // format has changed
    assertFalse(str.matches(s));
    assertTrue(str.contains("<today>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) PrintWriter(java.io.PrintWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 89 with CachedOutputStream

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

the class LoggingOutInterceptorTest method testFormattingOverride.

@Test
public void testFormattingOverride() throws Exception {
    control.replay();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // create a custom logging interceptor that overrides how formatting is done
    LoggingOutInterceptor p = new CustomFormatLoggingOutInterceptor(new PrintWriter(baos));
    CachedOutputStream cos = new CachedOutputStream();
    String s = "<today><is><the><twenty> <second> <of> <january> <two> <thousand> <and> <nine></nine> " + "</and></thousand></two></january></of></second></twenty></the></is></today>";
    cos.write(s.getBytes());
    Message message = new MessageImpl();
    message.setExchange(new ExchangeImpl());
    message.put(Message.CONTENT_TYPE, "application/xml");
    Logger logger = LogUtils.getL7dLogger(this.getClass());
    LoggingOutInterceptor.LoggingCallback l = p.new LoggingCallback(logger, message, cos);
    l.onClose(cos);
    String str = baos.toString();
    assertTrue(str.contains("<tomorrow/>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) PrintWriter(java.io.PrintWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 90 with CachedOutputStream

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

the class LoggingOutInterceptorTest method testPrettyLoggingWithoutEncoding.

@Test
public void testPrettyLoggingWithoutEncoding() throws Exception {
    control.replay();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(baos);
    LoggingOutInterceptor p = new LoggingOutInterceptor(pw);
    p.setPrettyLogging(true);
    CachedOutputStream cos = new CachedOutputStream();
    String s = "<today><is><the><twenty> <second> <of> <january> <two> <thousand> <and> <nine></nine> " + "</and></thousand></two></january></of></second></twenty></the></is></today>";
    cos.write(s.getBytes());
    Message message = new MessageImpl();
    message.setExchange(new ExchangeImpl());
    message.put(Message.CONTENT_TYPE, "application/xml");
    Logger logger = LogUtils.getL7dLogger(this.getClass());
    LoggingOutInterceptor.LoggingCallback l = p.new LoggingCallback(logger, message, cos);
    l.onClose(cos);
    String str = baos.toString();
    // format has changed
    assertFalse(str.matches(s));
    assertTrue(str.contains("<today>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) PrintWriter(java.io.PrintWriter) 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