Repository: cxf Updated Branches: refs/heads/master f8d941e6c -> 5c5f752d2
[CXF-6510] Updating LoggingOutInterceptor to use formatLoggingMessage, patch from CodeSmell(Mike) applied, This closes #135 Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/5c5f752d Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/5c5f752d Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/5c5f752d Branch: refs/heads/master Commit: 5c5f752d2be6aa3db507c52cfc623e856b921c9f Parents: f8d941e Author: Sergey Beryozkin <[email protected]> Authored: Mon May 16 10:08:08 2016 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Mon May 16 10:08:08 2016 +0100 ---------------------------------------------------------------------- .../cxf/interceptor/LoggingOutInterceptor.java | 9 +-- .../interceptor/LoggingOutInterceptorTest.java | 72 ++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/5c5f752d/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java index b838fe8..b356e55 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java @@ -19,7 +19,6 @@ package org.apache.cxf.interceptor; - import java.io.FilterWriter; import java.io.IOException; import java.io.OutputStream; @@ -36,7 +35,6 @@ import org.apache.cxf.io.CachedOutputStreamCallback; import org.apache.cxf.message.Message; import org.apache.cxf.phase.Phase; - /** * */ @@ -189,7 +187,7 @@ public class LoggingOutInterceptor extends AbstractLoggingInterceptor { } catch (Exception ex) { //ignore } - log(logger, buffer.toString()); + log(logger, formatLoggingMessage(buffer)); message.setContent(Writer.class, out); super.close(); } @@ -228,7 +226,7 @@ public class LoggingOutInterceptor extends AbstractLoggingInterceptor { } if (!isShowMultipartContent() && isMultipartContent(ct)) { buffer.getMessage().append(MULTIPART_CONTENT_MESSAGE).append('\n'); - log(logger, buffer.toString()); + log(logger, formatLoggingMessage(buffer)); return; } @@ -259,8 +257,7 @@ public class LoggingOutInterceptor extends AbstractLoggingInterceptor { } catch (Exception ex) { //ignore } - message.setContent(OutputStream.class, - origStream); + message.setContent(OutputStream.class, origStream); } } http://git-wip-us.apache.org/repos/asf/cxf/blob/5c5f752d/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java b/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java index 55c1aad..832f738 100644 --- a/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java +++ b/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java @@ -22,6 +22,8 @@ package org.apache.cxf.interceptor; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; import java.util.logging.Logger; import org.apache.cxf.common.logging.LogUtils; @@ -76,6 +78,60 @@ public class LoggingOutInterceptorTest extends Assert { assertTrue(str.contains("<today>")); } + + @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/>")); + + } + + @Test + public void testFormattingOverrideLogWriter() throws Exception { + // create a custom logging interceptor that overrides how formatting is done + LoggingOutInterceptor p = new CustomFormatLoggingOutInterceptor(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + p.setPrintWriter(new PrintWriter(baos)); + + StringWriter sw = new StringWriter(); + sw.append("<today/>"); + + Endpoint endpoint = control.createMock(Endpoint.class); + EndpointInfo endpointInfo = control.createMock(EndpointInfo.class); + EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes(); + control.replay(); + + Message message = new MessageImpl(); + message.setExchange(new ExchangeImpl()); + message.put(Message.CONTENT_TYPE, "application/xml"); + message.setContent(Writer.class, sw); + + p.handleMessage(message); + + Writer w = message.getContent(Writer.class); + w.close(); + + String str = baos.toString(); + assertTrue(str.contains("<tomorrow/>")); + } @Test public void testCachedOutputStreamThreshold() throws Exception { @@ -112,4 +168,20 @@ public class LoggingOutInterceptorTest extends Assert { return (CachedOutputStream)os; } + private class CustomFormatLoggingOutInterceptor extends LoggingOutInterceptor { + CustomFormatLoggingOutInterceptor() { + super(); + } + + CustomFormatLoggingOutInterceptor(PrintWriter w) { + super(w); + } + + @Override + protected String formatLoggingMessage(LoggingMessage loggingMessage) { + loggingMessage.getPayload().append("<tomorrow/>"); + return super.formatLoggingMessage(loggingMessage); + } + + } }
