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/27 14:41:30 UTC

svn commit: r1162335 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/attachments/ axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/ axiom-api/src/test/java/org/apache/axiom/attachments/ axio...

Author: veithen
Date: Sat Aug 27 12:41:29 2011
New Revision: 1162335

URL: http://svn.apache.org/viewvc?rev=1162335&view=rev
Log:
Make sure that DataHandlerExt#readOnce() always has the desired effect of consuming the MIME part content, even if it is buffered.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java?rev=1162335&r1=1162334&r2=1162335&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java Sat Aug 27 12:41:29 2011
@@ -248,7 +248,12 @@ final class PartImpl implements Part {
             state = STATE_STREAMING;
             return parser.getDecodedInputStream();
         } else {
-            return getContent().getInputStream();
+            ContentStore content = getContent();
+            InputStream stream = content.getInputStream();
+            if (!preserve) {
+                stream = new ReadOnceInputStreamWrapper(this, stream);
+            }
+            return stream;
         }
     }
     

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java?rev=1162335&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java Sat Aug 27 12:41:29 2011
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.attachments;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Input stream wrapper that automatically calls {@link PartImpl#releaseContent()} when the content
+ * has been consumed.
+ */
+class ReadOnceInputStreamWrapper extends InputStream {
+    private final PartImpl part;
+    private InputStream in;
+    
+    ReadOnceInputStreamWrapper(PartImpl part, InputStream in) {
+        this.part = part;
+        this.in = in;
+    }
+    
+    public int available() throws IOException {
+        return in == null ? 0 : in.available();
+    }
+
+    public int read() throws IOException {
+        if (in == null) {
+            return -1;
+        } else {
+            int result = in.read();
+            if (result == -1) {
+                close();
+            }
+            return result;
+        }
+    }
+
+    public int read(byte[] b, int off, int len) throws IOException {
+        if (in == null) {
+            return -1;
+        } else {
+            int result = in.read(b, off, len);
+            if (result == -1) {
+                close();
+            }
+            return result;
+        }
+    }
+
+    public int read(byte[] b) throws IOException {
+        if (in == null) {
+            return -1;
+        } else {
+            int result = in.read(b);
+            if (result == -1) {
+                close();
+            }
+            return result;
+        }
+    }
+
+    public long skip(long n) throws IOException {
+        return in == null ? 0 : in.skip(n);
+    }
+
+    public void close() throws IOException {
+        if (in != null) {
+            in.close();
+            part.releaseContent();
+            in = null;
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java?rev=1162335&r1=1162334&r2=1162335&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/lifecycle/DataHandlerExt.java Sat Aug 27 12:41:29 2011
@@ -30,6 +30,9 @@ public interface DataHandlerExt {
      * similar to {@link DataHandler#getInputStream()} except that it can be invoked only once. If
      * the content has not been buffered yet, then the implementation may choose to enable streaming
      * of the content.
+     * <p>
+     * The implementation ensures that after the returned input steam is consumed, the data handler
+     * will be in the same state as after a call to {@link #purgeDataSource()}.
      * 
      * @return the stream representing the content; never <code>null</code>
      * @throws IOException

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=1162335&r1=1162334&r2=1162335&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 Sat Aug 27 12:41:29 2011
@@ -502,6 +502,39 @@ public class AttachmentsTest extends Abs
     }
     
     /**
+     * Tests that after consuming the input stream returned by {@link DataHandlerExt#readOnce()} for
+     * an attachment that has been buffered on disk, the temporary file for that attachment is
+     * deleted.
+     * 
+     * @throws Exception
+     */
+    public void testReadOnceOnBufferedPart() throws Exception {
+        InputStream in = getTestResource("mtom/msg-soap-wls81.txt");
+        MyLifecycleManager manager = new MyLifecycleManager();
+        Attachments attachments = new Attachments(manager, in,
+                "multipart/related;type=\"text/xml\";boundary=\"----=_Part_0_3437046.1188904239130\";start=__WLS__1188904239161__SOAP__",
+                true, getAttachmentsDir(), "1024");
+        
+        // Read the attachment once to make sure it is buffered
+        DataHandler dh = attachments.getDataHandler("__WLS__1188904239162__SOAP__");
+        InputStream content = dh.getInputStream();
+        IOUtils.copy(content, new NullOutputStream());
+        content.close();
+        
+        assertEquals(1, manager.getFileCount());
+
+        // Now consume the content of the attachment
+        content = ((DataHandlerExt)dh).readOnce();
+        IOUtils.copy(content, new NullOutputStream());
+        content.close();
+        
+        // The temporary file should have been deleted
+        assertEquals(0, manager.getFileCount());
+        
+        in.close();
+    }
+    
+    /**
      * Tests that attachments are correctly buffered on file if the threshold is very low. This is a
      * regression test for <a href="https://issues.apache.org/jira/browse/AXIOM-61">AXIOM-61</a>.
      * 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java?rev=1162335&r1=1162334&r2=1162335&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/mtom/MTOMStAXSOAPModelBuilderTest.java Sat Aug 27 12:41:29 2011
@@ -87,7 +87,7 @@ public class MTOMStAXSOAPModelBuilderTes
         OMOutputFormat format = new OMOutputFormat();
         format.setDoOptimize(optimize);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        root.serializeAndConsume(baos, format);
+        root.serialize(baos, format);
         String msg = baos.toString();
         if (optimize) {
             // Make sure there is an xop:Include element and an optimized attachment
@@ -194,7 +194,6 @@ public class MTOMStAXSOAPModelBuilderTes
      * the XOP is preserved when it is serialized.
      * @throws Exception
      */
-    // TODO: because of the serializeAndConsume, this is actually NOT testing MTOMStAXSOAPModelBuilder, but StreamingOMSerializer!!!
     public void testCreateAndSerializeOptimized() throws Exception {
         OMElement root = createTestMTOMMessage();
         checkSerialization(root, true);