Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EE6859C5A for ; Fri, 10 Feb 2012 15:58:31 +0000 (UTC) Received: (qmail 20114 invoked by uid 500); 10 Feb 2012 15:58:31 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 20013 invoked by uid 500); 10 Feb 2012 15:58:31 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 20006 invoked by uid 99); 10 Feb 2012 15:58:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 15:58:30 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 15:58:29 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 9E86823888E7 for ; Fri, 10 Feb 2012 15:58:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1242831 - in /cxf/trunk: api/src/main/java/org/apache/cxf/interceptor/ rt/core/src/test/java/org/apache/cxf/interceptor/ Date: Fri, 10 Feb 2012 15:58:09 -0000 To: commits@cxf.apache.org From: asoldano@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120210155809.9E86823888E7@eris.apache.org> Author: asoldano Date: Fri Feb 10 15:58:09 2012 New Revision: 1242831 URL: http://svn.apache.org/viewvc?rev=1242831&view=rev Log: [CXF-4030] Add option for setting in-memory threshold for dealing with messages caching in logging interceptors Modified: cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java cxf/trunk/rt/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java Modified: cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java?rev=1242831&r1=1242830&r2=1242831&view=diff ============================================================================== --- cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java (original) +++ cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java Fri Feb 10 15:58:09 2012 @@ -48,6 +48,7 @@ import org.apache.cxf.service.model.Inte public abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor { protected int limit = 100 * 1024; + protected long threshold = -1; protected PrintWriter writer; protected boolean prettyLogging; @@ -120,8 +121,15 @@ public abstract class AbstractLoggingInt public boolean isPrettyLogging() { return prettyLogging; } - - + + public void setInMemThreshold(long t) { + threshold = t; + } + + public long getInMemThreshold() { + return threshold; + } + protected void writePayload(StringBuilder builder, CachedOutputStream cos, String encoding, String contentType) throws Exception { Modified: cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java?rev=1242831&r1=1242830&r2=1242831&view=diff ============================================================================== --- cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java (original) +++ cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java Fri Feb 10 15:58:09 2012 @@ -123,6 +123,9 @@ public class LoggingInInterceptor extend InputStream is = message.getContent(InputStream.class); if (is != null) { CachedOutputStream bos = new CachedOutputStream(); + if (threshold > 0) { + bos.setThreshold(threshold); + } try { IOUtils.copy(is, bos); Modified: cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java?rev=1242831&r1=1242830&r2=1242831&view=diff ============================================================================== --- cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java (original) +++ cxf/trunk/api/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java Fri Feb 10 15:58:09 2012 @@ -70,6 +70,9 @@ public class LoggingOutInterceptor exten if (!hasLogged) { message.put(LOG_SETUP, Boolean.TRUE); final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os); + if (threshold > 0) { + newOut.setThreshold(threshold); + } message.setContent(OutputStream.class, newOut); newOut.registerCallback(new LoggingCallback(logger, message, os)); } Modified: cxf/trunk/rt/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java?rev=1242831&r1=1242830&r2=1242831&view=diff ============================================================================== --- cxf/trunk/rt/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java (original) +++ cxf/trunk/rt/core/src/test/java/org/apache/cxf/interceptor/LoggingOutInterceptorTest.java Fri Feb 10 15:58:09 2012 @@ -20,29 +20,48 @@ package org.apache.cxf.interceptor; import java.io.ByteArrayOutputStream; +import java.io.OutputStream; import java.io.PrintWriter; import java.util.logging.Logger; import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.endpoint.Endpoint; 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.apache.cxf.service.model.EndpointInfo; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class LoggingOutInterceptorTest extends Assert { - + + protected IMocksControl control; + + @Before + public void setUp() throws Exception { + control = EasyMock.createNiceControl(); + } + + @After + public void tearDown() throws Exception { + control.verify(); + } + @Test - public void testFormatting() throws Exception { + public void testFormatting() 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 = " " + String s = " " + ""; cos.write(s.getBytes()); Message message = new MessageImpl(); @@ -57,4 +76,40 @@ public class LoggingOutInterceptorTest e assertTrue(str.contains("")); } + + @Test + public void testCachedOutputStreamThreshold() throws Exception { + byte[] mex = "".getBytes(); + + LoggingOutInterceptor p = new LoggingOutInterceptor(); + p.setInMemThreshold(mex.length); + + CachedOutputStream cos = handleAndGetCachedOutputStream(p); + cos.write(mex); + assertNull(cos.getTempFile()); + cos.write("a".getBytes()); + assertNotNull(cos.getTempFile()); + } + + 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(); + 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; + } + }