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<Message> {
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 = "<today><is><the><twenty> <second> <of> <january> <two>
<thousand> <and> <nine></nine> "
+ 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();
@@ -57,4 +76,40 @@ public class LoggingOutInterceptorTest e
assertTrue(str.contains("<today>"));
}
+
+ @Test
+ public void testCachedOutputStreamThreshold() throws Exception {
+ byte[] mex = "<test><threshold/></test>".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;
+ }
+
}