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 2016/04/29 08:49:58 UTC

svn commit: r1741559 - in /webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments: LegacyPartDataHandler.java PartDataHandler.java PartImpl.java

Author: veithen
Date: Fri Apr 29 06:49:58 2016
New Revision: 1741559

URL: http://svn.apache.org/viewvc?rev=1741559&view=rev
Log:
Start to decouple MIMEMessage from the attachments lifecycle API.

Added:
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/LegacyPartDataHandler.java
      - copied, changed from r1740738, webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java
Modified:
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java

Copied: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/LegacyPartDataHandler.java (from r1740738, webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java)
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/LegacyPartDataHandler.java?p2=webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/LegacyPartDataHandler.java&p1=webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java&r1=1740738&r2=1741559&rev=1741559&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/LegacyPartDataHandler.java Fri Apr 29 06:49:58 2016
@@ -20,57 +20,33 @@ package org.apache.axiom.attachments;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 
-import javax.activation.DataHandler;
 import javax.activation.DataSource;
 
 import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
+import org.apache.axiom.blob.Blob;
 
-/**
- * {@link DataHandler} implementation for MIME parts read from a stream.
- */
-class PartDataHandler extends DataHandler implements DataHandlerExt {
-    private final PartImpl part;
-    private DataSource dataSource;
-
-    public PartDataHandler(PartImpl part) {
-        // We can't call PartImpl#getDataSource() here because it would fetch the content of the
-        // part and therefore disable streaming. We can't pass null here either because Geronimo's
-        // DataHandler implementation would throw a NullPointerException. Therefore we create the
-        // default PartDataSource. When the DataSource is requested, we check if for there is an
-        // implementation specific to the buffering strategy and return that instead of the default
-        // implementation.
-        super(new PartDataSource(part));
-        this.part = part;
+final class LegacyPartDataHandler extends PartDataHandler implements DataHandlerExt {
+    public LegacyPartDataHandler(PartImpl part) {
+        super(part);
     }
 
-    public DataSource getDataSource() {
-        if (dataSource == null) {
-            dataSource = part.getDataSource();
-            if (dataSource == null) {
-                // We get here if there is no DataSource implementation specific to the buffering
-                // strategy being used. In this case we use super.getDataSource() to get the
-                // default PartDataSource that we created in the constructor.
-                dataSource = super.getDataSource();
-            }
+    @Override
+    protected DataSource createDataSource(Part part, String contentType) {
+        Blob blob = part.getBlob();
+        if (blob instanceof LegacyTempFileBlob) {
+            return ((LegacyTempFileBlob)blob).getDataSource(contentType);
+        } else {
+            return null;
         }
-        return dataSource;
-    }
-
-    public void writeTo(OutputStream os) throws IOException {
-        // The PartContent may have an implementation of writeTo that is more efficient than the default
-        // DataHandler#writeTo method (which requests an input stream and then copies it to the output
-        // stream).
-        part.writeTo(os);
     }
 
     public InputStream readOnce() throws IOException {
-        return part.getInputStream(false);
+        return getPart().getInputStream(false);
     }
 
     public void purgeDataSource() throws IOException {
-        part.releaseContent();
+        getPart().releaseContent();
     }
 
     public void deleteWhenReadOnce() throws IOException {

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java?rev=1741559&r1=1741558&r2=1741559&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartDataHandler.java Fri Apr 29 06:49:58 2016
@@ -19,18 +19,15 @@
 package org.apache.axiom.attachments;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
 
-import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
-
 /**
  * {@link DataHandler} implementation for MIME parts read from a stream.
  */
-class PartDataHandler extends DataHandler implements DataHandlerExt {
+class PartDataHandler extends DataHandler {
     private final PartImpl part;
     private DataSource dataSource;
 
@@ -45,9 +42,13 @@ class PartDataHandler extends DataHandle
         this.part = part;
     }
 
+    public PartImpl getPart() {
+        return part;
+    }
+
     public DataSource getDataSource() {
         if (dataSource == null) {
-            dataSource = part.getDataSource();
+            dataSource = createDataSource(part, part.getDataSourceContentType());
             if (dataSource == null) {
                 // We get here if there is no DataSource implementation specific to the buffering
                 // strategy being used. In this case we use super.getDataSource() to get the
@@ -58,24 +59,26 @@ class PartDataHandler extends DataHandle
         return dataSource;
     }
 
+    /**
+     * Create the {@link DataSource} to be returned by {@link #getDataSource()}. This method may be
+     * overridden by subclasses to support custom {@link DataSource} implementation.
+     * 
+     * @param part
+     *            the {@link Part} backing this data handler
+     * @param contentType
+     *            the content type expected to be returned by {@link DataSource#getContentType()};
+     *            defaults to {@code application/octet-stream} if {@link Part#getContentType()}
+     *            returns {@code null}
+     * @return the {@link DataSource} instance, or {@code null} to use the default implementation
+     */
+    protected DataSource createDataSource(Part part, String contentType) {
+        return null;
+    }
+
     public void writeTo(OutputStream os) throws IOException {
         // The PartContent may have an implementation of writeTo that is more efficient than the default
         // DataHandler#writeTo method (which requests an input stream and then copies it to the output
         // stream).
         part.writeTo(os);
     }
-
-    public InputStream readOnce() throws IOException {
-        return part.getInputStream(false);
-    }
-
-    public void purgeDataSource() throws IOException {
-        part.releaseContent();
-    }
-
-    public void deleteWhenReadOnce() throws IOException {
-        // As shown in AXIOM-381, in all released versions of Axiom, deleteWhenReadOnce
-        // always has the same effect as purgeDataSource
-        purgeDataSource();
-    }
 }

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java?rev=1741559&r1=1741558&r2=1741559&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java Fri Apr 29 06:49:58 2016
@@ -97,7 +97,8 @@ final class PartImpl implements Part {
         this.contentID = contentID;
         this.headers = headers;
         this.parser = parser;
-        this.dataHandler = new PartDataHandler(this);
+        // TODO: use factory here
+        this.dataHandler = new LegacyPartDataHandler(this);
     }
     
     public String getHeader(String name) {
@@ -258,21 +259,6 @@ final class PartImpl implements Part {
         }
     }
     
-    DataSource getDataSource() {
-        WritableBlob blob = getContent();
-        if (blob instanceof OverflowableBlob) {
-            WritableBlob overflowBlob = ((OverflowableBlob)blob).getOverflowBlob();
-            if (overflowBlob != null) {
-                blob = overflowBlob;
-            }
-        }
-        if (blob instanceof LegacyTempFileBlob) {
-            return ((LegacyTempFileBlob)blob).getDataSource(getDataSourceContentType());
-        } else {
-            return null;
-        }
-    }
-
     void writeTo(OutputStream out) throws IOException {
         getContent().writeTo(out);
     }