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/12/03 00:58:23 UTC

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

Author: veithen
Date: Fri Dec  2 23:58:23 2011
New Revision: 1209787

URL: http://svn.apache.org/viewvc?rev=1209787&view=rev
Log:
Ensure that DataSource objects created for MIME parts implement SizeAwareDataSource.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/CachedFileDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartContent.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java?rev=1209787&r1=1209786&r2=1209787&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java Fri Dec  2 23:58:23 2011
@@ -19,12 +19,15 @@
 
 package org.apache.axiom.attachments;
 
+import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
 import org.apache.axiom.attachments.lifecycle.LifecycleManager;
+import org.apache.axiom.ext.activation.SizeAwareDataSource;
 import org.apache.axiom.om.OMAttachmentAccessor;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.impl.MTOMConstants;
 
 import javax.activation.DataHandler;
+import javax.activation.DataSource;
 import javax.mail.internet.ContentType;
 
 import java.io.IOException;
@@ -177,7 +180,11 @@ public class Attachments implements OMAt
     }
 
     /**
-     * Get the {@link DataHandler} object for the MIME part with a given content ID.
+     * Get the {@link DataHandler} object for the MIME part with a given content ID. The returned
+     * instance MAY implement {@link DataHandlerExt} in which case the caller can use that API to
+     * stream the content of the part. In addition, the {@link DataSource} linked to the returned
+     * {@link DataHandler} MAY be of type {@link SizeAwareDataSource} in which case the caller can
+     * use that interface to determine the size of the MIME part.
      * 
      * @param contentID
      *            the raw content ID (without the surrounding angle brackets and <tt>cid:</tt>

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/CachedFileDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/CachedFileDataSource.java?rev=1209787&r1=1209786&r2=1209787&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/CachedFileDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/CachedFileDataSource.java Fri Dec  2 23:58:23 2011
@@ -22,11 +22,12 @@ package org.apache.axiom.attachments;
 import javax.activation.FileDataSource;
 import java.io.File;
 
+import org.apache.axiom.ext.activation.SizeAwareDataSource;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 
-public class CachedFileDataSource extends FileDataSource {
+public class CachedFileDataSource extends FileDataSource implements SizeAwareDataSource {
 
     String contentType = null;
     
@@ -74,4 +75,8 @@ public class CachedFileDataSource extend
     public void setContentType(String contentType) {
         this.contentType = contentType;
     }
+
+    public long getSize() {
+        return getFile().length();
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartContent.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartContent.java?rev=1209787&r1=1209786&r2=1209787&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartContent.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartContent.java Fri Dec  2 23:58:23 2011
@@ -24,6 +24,8 @@ import java.io.OutputStream;
 
 import javax.activation.DataSource;
 
+import org.apache.axiom.ext.activation.SizeAwareDataSource;
+
 /**
  * Stores the content of a MIME part using a particular buffering strategy.
  */
@@ -41,6 +43,8 @@ abstract class PartContent {
 
     /**
      * Get a {@link DataSource} implementation specific for this buffering strategy, if supported.
+     * If no {@link DataSource} is returned, then a default implementation will be used. If a
+     * {@link DataSource} is returned, it should implement {@link SizeAwareDataSource}.
      * 
      * @param contentType
      *            the content type for the {@link DataSource}, which must be returned by
@@ -63,7 +67,6 @@ abstract class PartContent {
      */
     abstract void writeTo(OutputStream out) throws IOException;
 
-    // TODO: currently not used; all our DataSources should implement SizeAwareDataSource!
     abstract long getSize();
 
     abstract void destroy() throws IOException;

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataSource.java?rev=1209787&r1=1209786&r2=1209787&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataSource.java Fri Dec  2 23:58:23 2011
@@ -22,9 +22,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
-import javax.activation.DataSource;
+import org.apache.axiom.ext.activation.SizeAwareDataSource;
 
-class PartDataSource implements DataSource {
+class PartDataSource implements SizeAwareDataSource {
     private final PartImpl part;
 
     public PartDataSource(PartImpl part) {
@@ -47,4 +47,8 @@ class PartDataSource implements DataSour
     public OutputStream getOutputStream() throws IOException {
         throw new UnsupportedOperationException();
     }
+
+    public long getSize() {
+        return part.getSize();
+    }
 }

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=1209787&r1=1209786&r2=1209787&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 Fri Dec  2 23:58:23 2011
@@ -43,6 +43,7 @@ import javax.mail.internet.MimeMultipart
 import javax.mail.util.ByteArrayDataSource;
 
 import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
+import org.apache.axiom.ext.activation.SizeAwareDataSource;
 import org.apache.axiom.om.AbstractTestCase;
 import org.apache.axiom.om.MIMEResource;
 import org.apache.axiom.om.OMException;
@@ -689,4 +690,32 @@ public class AttachmentsTest extends Abs
             // Expected
         }
     }
+
+    private void testGetSizeOnDataSource(boolean useFiles) throws Exception {
+        InputStream in = getTestResource(TestConstants.MTOM_MESSAGE.getName());
+        try {
+            Attachments attachments;
+            if (useFiles) {
+                attachments = new Attachments(in, TestConstants.MTOM_MESSAGE.getContentType(),
+                        true, getAttachmentsDir(), "4096");
+            } else {
+                attachments = new Attachments(in, TestConstants.MTOM_MESSAGE.getContentType());
+            }
+            DataHandler dh = attachments
+                    .getDataHandler("2.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org");
+            DataSource ds = dh.getDataSource();
+            assertTrue(ds instanceof SizeAwareDataSource);
+            assertEquals(13887, ((SizeAwareDataSource)ds).getSize());
+        } finally {
+            in.close();
+        }
+    }
+    
+    public void testGetSizeOnDataSourceOnMemory() throws Exception {
+        testGetSizeOnDataSource(false);
+    }
+    
+    public void testGetSizeOnDataSourceOnFile() throws Exception {
+        testGetSizeOnDataSource(true);
+    }
 }