You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2011/08/02 20:01:05 UTC

svn commit: r1153228 - in /webservices/commons/trunk/modules/axiom: modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/ modules/axiom-api/src/test/java/org/apache/axiom/attachments/ src/docbkx/

Author: veithen
Date: Tue Aug  2 18:01:03 2011
New Revision: 1153228

URL: http://svn.apache.org/viewvc?rev=1153228&view=rev
Log:
AXIOM-379: Always decode the attachment while reading from the MIME message instead of buffering the encoded content. Also make sure that the DataHandler instances created by Attachments actually return the decoded content.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartFactory.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemory.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemoryEnhanced.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
    webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartFactory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartFactory.java?rev=1153228&r1=1153227&r2=1153228&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartFactory.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartFactory.java Tue Aug  2 18:01:03 2011
@@ -143,13 +143,13 @@ public class PartFactory {
                     // of resizing and GC.  The BAAOutputStream 
                     // keeps the data in non-contiguous byte buffers.
                     BAAOutputStream baaos = new BAAOutputStream();
-                    BufferUtils.inputStream2OutputStream(parser.getInputStream(), baaos);
+                    BufferUtils.inputStream2OutputStream(parser.getDecodedInputStream(), baaos);
                     part = new PartOnMemoryEnhanced(headers, baaos.buffers(), baaos.length());
                 } else {
                     // We need to read the input stream to determine whether
                     // the size is bigger or smaller than the threshold.
                     BAAOutputStream baaos = new BAAOutputStream();
-                    InputStream in = parser.getInputStream();
+                    InputStream in = parser.getDecodedInputStream();
                     int count = BufferUtils.inputStream2OutputStream(in, baaos, thresholdSize);
 
                     if (count < thresholdSize) {

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemory.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemory.java?rev=1153228&r1=1153227&r2=1153228&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemory.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemory.java Tue Aug  2 18:01:03 2011
@@ -19,14 +19,9 @@
 
 package org.apache.axiom.attachments.impl;
 
-import org.apache.axiom.om.OMException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.mail.MessagingException;
-import javax.mail.internet.MimeUtility;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -44,7 +39,6 @@ import java.util.Hashtable;
  */
 public class PartOnMemory extends AbstractPart {
 
-    private static Log log = LogFactory.getLog(PartOnMemory.class);
     byte[] bytes;
     int length;
     
@@ -102,24 +96,7 @@ public class PartOnMemory extends Abstra
          * @see javax.activation.DataSource#getInputStream()
          */
         public InputStream getInputStream() throws IOException {
-            InputStream is  = new ByteArrayInputStream(bytes, 0, length);
-            String cte = null;
-            try {
-                cte = getContentTransferEncoding();
-                if(cte != null){
-                    if(log.isDebugEnabled()){
-                        log.debug("Start Decoding stream");
-                    }
-                    return MimeUtility.decode(is, cte);
-
-                }
-            } catch (MessagingException e) {
-                if(log.isDebugEnabled()){
-                    log.debug("Stream Failed decoding");
-                }
-                throw new OMException(e);
-            }
-            return is;
+            return new ByteArrayInputStream(bytes, 0, length);
         }
 
         /* (non-Javadoc)

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemoryEnhanced.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemoryEnhanced.java?rev=1153228&r1=1153227&r2=1153228&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemoryEnhanced.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/PartOnMemoryEnhanced.java Tue Aug  2 18:01:03 2011
@@ -20,14 +20,10 @@
 package org.apache.axiom.attachments.impl;
 
 import org.apache.axiom.attachments.utils.BAAInputStream;
-import org.apache.axiom.om.OMException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.mail.MessagingException;
-import javax.mail.internet.MimeUtility;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,7 +41,6 @@ import java.util.Hashtable;
  */
 public class PartOnMemoryEnhanced extends AbstractPart {
 
-    private static Log log = LogFactory.getLog(PartOnMemoryEnhanced.class);
     ArrayList data;  // Arrays of 4K buffers
     int length;      // total length of data
     
@@ -121,24 +116,7 @@ public class PartOnMemoryEnhanced extend
          * @see javax.activation.DataSource#getInputStream()
          */
         public InputStream getInputStream() throws IOException {
-            InputStream is  = new BAAInputStream(data, length);
-            String cte = null;
-            try {
-                cte = getContentTransferEncoding();
-                if(cte != null){
-                    if(log.isDebugEnabled()){
-                        log.debug("Start Decoding stream");
-                    }
-                    return MimeUtility.decode(is, cte);
-
-                }
-            } catch (MessagingException e) {
-                if(log.isDebugEnabled()){
-                    log.debug("Stream Failed decoding");
-                }
-                throw new OMException(e);
-            }
-            return is;
+            return new BAAInputStream(data, length);
         }
 
         /* (non-Javadoc)

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java?rev=1153228&r1=1153227&r2=1153228&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java Tue Aug  2 18:01:03 2011
@@ -18,21 +18,30 @@
  */
 package org.apache.axiom.attachments;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.Random;
 import java.util.Set;
 
 import javax.activation.DataHandler;
+import javax.mail.Session;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.util.ByteArrayDataSource;
 
-import org.apache.axiom.attachments.utils.IOUtils;
 import org.apache.axiom.om.AbstractTestCase;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.TestConstants;
 import org.apache.axiom.testutils.io.IOTestUtils;
 import org.apache.axiom.util.UIDGenerator;
+import org.apache.commons.io.IOUtils;
 
 public class AttachmentsTest extends AbstractTestCase {
     String img1FileName = "mtom/img/test.jpg";
@@ -119,7 +128,7 @@ public class AttachmentsTest extends Abs
         
         // Make sure the length is correct
         long length = attachments.getContentLength();
-        long fileSize = IOUtils.getStreamAsByteArray(getTestResource(TestConstants.MTOM_MESSAGE)).length;
+        long fileSize = IOUtils.toByteArray(getTestResource(TestConstants.MTOM_MESSAGE)).length;
         assertEquals("Return value of getContentLength()", fileSize, length);
     }
     
@@ -386,6 +395,56 @@ public class AttachmentsTest extends Abs
         assertEquals(1, attachments.getContentIDSet().size());
     }
 
+    private void testReadBase64EncodedAttachment(boolean useFile) throws Exception {
+        // Note: We are only interested in the MimeMultipart, but we need to create a
+        //       MimeMessage to be able to calculate the correct content type
+        MimeMessage message = new MimeMessage((Session)null);
+        MimeMultipart mp = new MimeMultipart("related");
+        
+        // Prepare the "SOAP" part
+        MimeBodyPart bp1 = new MimeBodyPart();
+        // Obviously this is not SOAP, but this is irrelevant for this test
+        bp1.setText("<root/>", "utf-8", "xml");
+        bp1.addHeader("Content-Transfer-Encoding", "binary");
+        bp1.addHeader("Content-ID", "part1@apache.org");
+        mp.addBodyPart(bp1);
+        
+        // Prepare the attachment
+        MimeBodyPart bp2 = new MimeBodyPart();
+        byte[] content = new byte[8192];
+        new Random().nextBytes(content);
+        bp2.setDataHandler(new DataHandler(new ByteArrayDataSource(content, "application/octet-stream")));
+        bp2.addHeader("Content-Transfer-Encoding", "base64");
+        bp2.addHeader("Content-ID", "part2@apache.org");
+        mp.addBodyPart(bp2);
+        
+        message.setContent(mp);
+        // Compute the correct content type
+        message.saveChanges();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        mp.writeTo(baos);
+        String contentType = message.getContentType();
+        
+        InputStream in = new ByteArrayInputStream(baos.toByteArray());
+        Attachments attachments;
+        if (useFile) {
+            attachments = new Attachments(in, contentType, true, System.getProperty("basedir", ".") + "/target", "1024");
+        } else {
+            attachments = new Attachments(in, contentType);
+        }
+        DataHandler dh = attachments.getDataHandler("part2@apache.org");
+        byte[] content2 = IOUtils.toByteArray(dh.getInputStream());
+        assertTrue(Arrays.equals(content, content2));
+    }
+
+    public void testReadBase64EncodedAttachmentWithPartOnMemory() throws Exception {
+        testReadBase64EncodedAttachment(false);
+    }
+
+    public void testReadBase64EncodedAttachmentWithPartOnFile() throws Exception {
+        testReadBase64EncodedAttachment(true);
+    }
+
     public void testCachedFilesExpired() throws Exception {
         
         // Set file expiration to 10 seconds

Modified: webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml?rev=1153228&r1=1153227&r2=1153228&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml (original)
+++ webservices/commons/trunk/modules/axiom/src/docbkx/userguide.xml Tue Aug  2 18:01:03 2011
@@ -1324,6 +1324,15 @@ while (iterator.hasNext()) {
                                 supported.
                             </para>
                         </listitem>
+                        <listitem>
+                            <para>
+                                The <literal>fileThreshold</literal> specified during the construction of the
+                                <classname>Attachments</classname> object is now interpreted relative to the size of the decoded
+                                content of the attachment instead of the size of the encoded content. Note that
+                                this only makes a difference if the attachment has a content transfer encoding other
+                                than <literal>binary</literal>.
+                            </para>
+                        </listitem>
                     </itemizedlist>
                     <para>
                         Several internal classes related to the old MIME parsing code have been removed, are