[CXF-5635] Optimize the LoggingInInterceptor to only slurp in "limit" bytes
from the InputStream then replay those bytes, then allow the rest to stream
like normal. Should prevent very large temp files if logging is on.
Conflicts:
api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/33b821aa
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/33b821aa
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/33b821aa
Branch: refs/heads/2.6.x-fixes
Commit: 33b821aad669f5ca999b4f7a4c169de470bb3f8f
Parents: 7629b03
Author: Daniel Kulp <[email protected]>
Authored: Fri Mar 21 19:35:41 2014 -0400
Committer: Daniel Kulp <[email protected]>
Committed: Fri Mar 21 21:04:41 2014 -0400
----------------------------------------------------------------------
.../cxf/interceptor/LoggingInInterceptor.java | 87 ++++++++++++--------
1 file changed, 52 insertions(+), 35 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf/blob/33b821aa/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
----------------------------------------------------------------------
diff --git
a/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
b/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
index 1c833f6..e363fb2 100644
--- a/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
+++ b/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
@@ -22,6 +22,7 @@ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
+import java.io.SequenceInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cxf.common.injection.NoJSR250Annotations;
@@ -132,40 +133,7 @@ public class LoggingInInterceptor extends
AbstractLoggingInterceptor {
InputStream is = message.getContent(InputStream.class);
if (is != null) {
- 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;
-
- IOUtils.copyAndCloseInput(bis, bos);
- bos.flush();
- bis = bos.getInputStream();
-
- // 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");
- }
- if (bos.size() > limit) {
- buffer.getMessage().append("(message truncated to " +
limit + " bytes)\n");
- }
- writePayload(buffer.getPayload(), bos, encoding, ct);
-
- bos.close();
- } catch (Exception e) {
- throw new Fault(e);
- }
+ logInputStream(message, is, buffer, encoding, ct);
} else {
Reader reader = message.getContent(Reader.class);
if (reader != null) {
@@ -180,12 +148,61 @@ public class LoggingInInterceptor extends
AbstractLoggingInterceptor {
} catch (Exception e) {
throw new Fault(e);
}
-
}
}
log(logger, formatLoggingMessage(buffer));
}
+ 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
+ byte bytes[] = new byte[2048];
+ int i = bis.read(bytes);
+ int count = 0;
+ while (count <= limit && i != -1) {
+ bos.write(bytes, 0, i);
+ count += i;
+ i = bis.read(bytes);
+ }
+ if (i > 0) {
+ bos.write(bytes, 0, i);
+ }
+ 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");
+ }
+ if (bos.size() > limit) {
+ buffer.getMessage().append("(message truncated to " + limit +
" bytes)\n");
+ }
+ writePayload(buffer.getPayload(), bos, encoding, ct);
+
+ bos.close();
+ } catch (Exception e) {
+ throw new Fault(e);
+ }
+ }
protected String formatLoggingMessage(LoggingMessage loggingMessage) {