Repository: cxf Updated Branches: refs/heads/master 958bb235f -> 1b0d3c964
[CXF-7430] Avoiding the pretty-printing if the payload has been truncated, modified patch from Alex Korobko applied, This closes #294 Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1b0d3c96 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1b0d3c96 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1b0d3c96 Branch: refs/heads/master Commit: 1b0d3c964e0896dbdeb8fe5d59178c10cf4fab06 Parents: 958bb23 Author: Sergey Beryozkin <[email protected]> Authored: Wed Aug 9 12:34:58 2017 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Wed Aug 9 12:34:58 2017 +0100 ---------------------------------------------------------------------- .../interceptor/AbstractLoggingInterceptor.java | 4 +- .../cxf/interceptor/LoggingInInterceptor.java | 4 +- .../cxf/interceptor/LoggingOutInterceptor.java | 5 +- .../interceptor/LoggingInInterceptorTest.java | 137 +++++++++++++++++++ 4 files changed, 146 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1b0d3c96/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java index eebccbd..c3e232c 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java @@ -167,10 +167,10 @@ public abstract class AbstractLoggingInterceptor extends AbstractPhaseIntercepto } protected void writePayload(StringBuilder builder, CachedOutputStream cos, - String encoding, String contentType) + String encoding, String contentType, boolean truncated) throws Exception { // Just transform the XML message when the cos has content - if (isPrettyLogging() && contentType != null && contentType.contains("xml") + if (!truncated && isPrettyLogging() && contentType != null && contentType.contains("xml") && !contentType.toLowerCase().contains("multipart/related") && cos.size() > 0) { StringWriter swriter = new StringWriter(); http://git-wip-us.apache.org/repos/asf/cxf/blob/1b0d3c96/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java index b26b5d3..f2c571e 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java @@ -217,10 +217,12 @@ public class LoggingInInterceptor extends AbstractLoggingInterceptor { 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); + writePayload(buffer.getPayload(), bos, encoding, ct, truncated); bos.close(); } catch (Exception e) { http://git-wip-us.apache.org/repos/asf/cxf/blob/1b0d3c96/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 fb31ea0..dff6bb6 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java @@ -231,21 +231,24 @@ public class LoggingOutInterceptor extends AbstractLoggingInterceptor { return; } + boolean truncated = false; if (cos.getTempFile() == null) { //buffer.append("Outbound Message:\n"); if (cos.size() >= lim) { buffer.getMessage().append("(message truncated to " + lim + " bytes)\n"); + truncated = true; } } else { buffer.getMessage().append("Outbound Message (saved to tmp file):\n"); buffer.getMessage().append("Filename: " + cos.getTempFile().getAbsolutePath() + "\n"); if (cos.size() >= lim) { buffer.getMessage().append("(message truncated to " + lim + " bytes)\n"); + truncated = true; } } try { String encoding = (String)message.get(Message.ENCODING); - writePayload(buffer.getPayload(), cos, encoding, ct); + writePayload(buffer.getPayload(), cos, encoding, ct, truncated); } catch (Exception ex) { //ignore } http://git-wip-us.apache.org/repos/asf/cxf/blob/1b0d3c96/core/src/test/java/org/apache/cxf/interceptor/LoggingInInterceptorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/interceptor/LoggingInInterceptorTest.java b/core/src/test/java/org/apache/cxf/interceptor/LoggingInInterceptorTest.java new file mode 100644 index 0000000..ccf9aee --- /dev/null +++ b/core/src/test/java/org/apache/cxf/interceptor/LoggingInInterceptorTest.java @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.interceptor; + +import java.io.InputStream; +import java.io.StringWriter; +import java.io.Writer; + +import org.apache.cxf.io.CachedOutputStream; +import org.apache.cxf.message.ExchangeImpl; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageImpl; + +import org.easymock.EasyMock; +import org.easymock.IAnswer; +import org.easymock.IMocksControl; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +@SuppressWarnings("deprecation") +public class LoggingInInterceptorTest extends Assert { + static String encoding = "UTF-8"; + static String contentType = "text/xml"; + + static String bufferContent = "<today><is><the><eighteenth><of><july><two><thousand><seventeen>" + + "</seventeen></thousand></two></july></of></eighteenth></the></is></today>"; + static int bufferLength = bufferContent.getBytes().length; + + protected IMocksControl control; + private Message message; + private InputStream inputStream; + private LoggingMessage loggingMessage; + private LoggingInInterceptorAncestorTester classUnderTest; + + @Before + public void setUp() throws Exception { + loggingMessage = new LoggingMessage("", ""); + control = EasyMock.createNiceControl(); + + StringWriter sw = new StringWriter(); + sw.append("<today/>"); + message = new MessageImpl(); + message.setExchange(new ExchangeImpl()); + message.put(Message.CONTENT_TYPE, "application/xml"); + message.setContent(Writer.class, sw); + + inputStream = control.createMock(InputStream.class); + EasyMock.expect(inputStream.read(EasyMock.anyObject(byte[].class), EasyMock.anyInt(), EasyMock.anyInt())) + .andAnswer(new IAnswer<Integer>() { + public Integer answer() { + System.arraycopy(bufferContent.getBytes(), 0, + EasyMock.getCurrentArguments()[0], 0, + bufferLength); + return bufferLength; + } + }).andStubReturn(-1); + control.replay(); + } + + @After + public void tearDown() throws Exception { + control.verify(); + } + + @Test + public void testLogInputStreamInLimit() throws Exception { + //arrange + classUnderTest = new LoggingInInterceptorAncestorTester(4098); + //act + classUnderTest.testLogInputStream(message, inputStream, loggingMessage, encoding, contentType); + //assert + assertEquals("The truncated status should be set to false", + false, + classUnderTest.isTruncated()); + } + + @Test + public void testLogInputStreamOffLimit() throws Exception { + //arrange + classUnderTest = new LoggingInInterceptorAncestorTester(16); + //act + classUnderTest.testLogInputStream(message, inputStream, loggingMessage, encoding, contentType); + //assert + assertEquals("The truncated status should be set to true", + true, + classUnderTest.isTruncated()); + } + + class LoggingInInterceptorAncestorTester extends LoggingInInterceptor { + private boolean truncated; + + LoggingInInterceptorAncestorTester(int limit) { + super(limit); + } + + public boolean isTruncated() { + return truncated; + } + + public void testLogInputStream(Message logMessage, + InputStream is, + LoggingMessage buffer, + String contentEncoding, + String logContentType) { + this.logInputStream(logMessage, is, buffer, contentEncoding, logContentType); + } + + @Override + protected void writePayload(StringBuilder builder, + CachedOutputStream cos, + String contentEncoding, + String logContentType, + boolean truncatedStatus) { + this.truncated = truncatedStatus; + } + } +}
