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);
}
}
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;
}
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>"));
}
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/>"));
}
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>"));
}
Aggregations