You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2018/04/10 17:10:06 UTC

[cxf] 02/02: CXF-7688 - AttachmentDeserializer does not respect "org.apache.cxf.io.CachedOutputStream.Threshold" property

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit cc0155e28ffff033690188caf86fa87e509b7617
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Tue Apr 10 17:31:44 2018 +0100

    CXF-7688 - AttachmentDeserializer does not respect "org.apache.cxf.io.CachedOutputStream.Threshold" property
---
 .../cxf/attachment/AttachmentDeserializer.java     | 14 ++++----
 .../org/apache/cxf/attachment/AttachmentUtil.java  |  5 ++-
 .../org/apache/cxf/io/CachedOutputStreamTest.java  | 41 +++++++++++++++++++++-
 3 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
index 92de751..7f46ec5 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java
@@ -57,7 +57,7 @@ public class AttachmentDeserializer {
      * The maximum MIME Header Length. The default is 300.
      */
     public static final String ATTACHMENT_MAX_HEADER_SIZE = "attachment-max-header-size";
-    public static final int DEFAULT_MAX_HEADER_SIZE = 
+    public static final int DEFAULT_MAX_HEADER_SIZE =
         SystemPropertyAction.getInteger("org.apache.cxf.attachment-max-header-size", 300);
 
     public static final int THRESHOLD = 1024 * 100; //100K (byte unit)
@@ -69,15 +69,16 @@ public class AttachmentDeserializer {
 
     private static final Logger LOG = LogUtils.getL7dLogger(AttachmentDeserializer.class);
 
+    private static final int PUSHBACK_AMOUNT = 2048;
+
     private boolean lazyLoading = true;
 
-    private int pbAmount = 2048;
     private PushbackInputStream stream;
     private int createCount;
     private int closedCount;
     private boolean closed;
 
-    private byte boundary[];
+    private byte[] boundary;
 
     private String contentType;
 
@@ -134,8 +135,7 @@ public class AttachmentDeserializer {
             }
             boundary = boundaryString.getBytes("utf-8");
 
-            stream = new PushbackInputStream(message.getContent(InputStream.class),
-                                             pbAmount);
+            stream = new PushbackInputStream(message.getContent(InputStream.class), PUSHBACK_AMOUNT);
             if (!readTillFirstBoundary(stream, boundary)) {
                 throw new IOException("Couldn't find MIME boundary: " + boundaryString);
             }
@@ -151,7 +151,7 @@ public class AttachmentDeserializer {
             }
             val = AttachmentUtil.getHeader(ih, "Content-Transfer-Encoding");
 
-            MimeBodyPartInputStream mmps = new MimeBodyPartInputStream(stream, boundary, pbAmount);
+            MimeBodyPartInputStream mmps = new MimeBodyPartInputStream(stream, boundary, PUSHBACK_AMOUNT);
             InputStream ins = AttachmentUtil.decode(mmps, val);
             if (ins != mmps) {
                 ih.remove("Content-Transfer-Encoding");
@@ -293,7 +293,7 @@ public class AttachmentDeserializer {
      */
     private Attachment createAttachment(Map<String, List<String>> headers) throws IOException {
         InputStream partStream =
-            new DelegatingInputStream(new MimeBodyPartInputStream(stream, boundary, pbAmount),
+            new DelegatingInputStream(new MimeBodyPartInputStream(stream, boundary, PUSHBACK_AMOUNT),
                                       this);
         createCount++;
 
diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
index c11fa63..0496ec2 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
@@ -53,8 +53,10 @@ import javax.activation.MailcapCommandMap;
 import javax.activation.URLDataSource;
 
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.util.SystemPropertyAction;
 import org.apache.cxf.helpers.HttpHeaderHelper;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.io.CachedConstants;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.message.Attachment;
 import org.apache.cxf.message.Message;
@@ -177,7 +179,8 @@ public final class AttachmentUtil {
             } else {
                 bos.setThreshold(Long.parseLong((String)threshold));
             }
-        } else {
+        } else if (SystemPropertyAction.getProperty(CachedConstants.THRESHOLD_SYS_PROP) == null) {
+            // Use the default AttachmentDeserializer Threshold only if there is no system property defined
             bos.setThreshold(AttachmentDeserializer.THRESHOLD);
         }
 
diff --git a/core/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java b/core/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
index 7d4366a..9a8299f 100644
--- a/core/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
+++ b/core/src/test/java/org/apache/cxf/io/CachedOutputStreamTest.java
@@ -22,6 +22,13 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.attachment.AttachmentUtil;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
 
 public class CachedOutputStreamTest extends CachedStreamTestBase {
 
@@ -62,7 +69,7 @@ public class CachedOutputStreamTest extends CachedStreamTestBase {
     @Override
     protected File getTmpFile(String result, Object cache) throws IOException {
         CachedOutputStream cos = (CachedOutputStream)cache;
-        cos.write(result.getBytes("utf-8"));
+        cos.write(result.getBytes(StandardCharsets.UTF_8));
         cos.flush();
         cos.getOut().close();
         return cos.getTempFile();
@@ -82,6 +89,38 @@ public class CachedOutputStreamTest extends CachedStreamTestBase {
     protected String readPartiallyFromStreamObject(Object cache, int len) throws IOException {
         return readPartiallyFromStream((InputStream)cache, len);
     }
+
+    @Test
+    public void testUseSysPropsWithAttachmentDeserializer() throws Exception {
+        String old = System.getProperty(CachedConstants.THRESHOLD_SYS_PROP);
+        try {
+            System.clearProperty(CachedConstants.THRESHOLD_SYS_PROP);
+            reloadDefaultProperties();
+            CachedOutputStream cache = new CachedOutputStream();
+
+            Message message = new MessageImpl();
+            AttachmentUtil.setStreamedAttachmentProperties(message, cache);
+
+            File tmpfile = getTmpFile("Hello World!", cache);
+            assertNull("expects no tmp file", tmpfile);
+            cache.close();
+
+            System.setProperty(CachedConstants.THRESHOLD_SYS_PROP, "4");
+            reloadDefaultProperties();
+            cache = new CachedOutputStream();
+            AttachmentUtil.setStreamedAttachmentProperties(message, cache);
+
+            tmpfile = getTmpFile("Hello World!", cache);
+            assertNotNull("expects a tmp file", tmpfile);
+            assertTrue("expects a tmp file", tmpfile.exists());
+            cache.close();
+            assertFalse("expects no tmp file", tmpfile.exists());
+        } finally {
+            if (old != null) {
+                System.setProperty(CachedConstants.THRESHOLD_SYS_PROP, old);
+            }
+        }
+    }
 }
 
 

-- 
To stop receiving notification emails like this one, please contact
coheigea@apache.org.