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/25 22:38:42 UTC

svn commit: r1161739 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/ axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-testsuite/...

Author: veithen
Date: Thu Aug 25 20:38:42 2011
New Revision: 1161739

URL: http://svn.apache.org/viewvc?rev=1161739&view=rev
Log:
AXIOM-377: Implemented MTOM attachment streaming with serializeAndConsume.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSerializableImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/SOAPTestSuiteBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java Thu Aug 25 20:38:42 2011
@@ -30,6 +30,8 @@ import javax.xml.stream.FactoryConfigura
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.axiom.attachments.impl.BufferUtils;
+import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMOutputFormat;
@@ -63,6 +65,7 @@ public class MTOMXMLStreamWriter impleme
     private OutputStream rootPartOutputStream;
     private OMOutputFormat format = new OMOutputFormat();
     private final OptimizationPolicy optimizationPolicy;
+    private final boolean preserveAttachments;
     
     // State variables
     private boolean isEndDocument = false; // has endElement been called
@@ -78,29 +81,42 @@ public class MTOMXMLStreamWriter impleme
             log.trace("Call Stack =" + CommonUtils.callStackToString());
         }
         optimizationPolicy = new OptimizationPolicyImpl(format);
+        preserveAttachments = true;
     }
 
+    public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format)
+            throws XMLStreamException, FactoryConfigurationError {
+        this(outStream, format, true);
+    }
+    
     /**
      * Creates a new MTOMXMLStreamWriter with specified encoding.
-     *
+     * 
      * @param outStream
      * @param format
+     * @param preserveAttachments
+     *            specifies whether attachments must be preserved or can be consumed (i.e. streamed)
+     *            during serialization; if set to <code>false</code> then
+     *            {@link DataHandlerExt#readOnce()} or an equivalent method may be used to get the
+     *            data for an attachment
      * @throws XMLStreamException
      * @throws FactoryConfigurationError
      * @see OMOutputFormat#DEFAULT_CHAR_SET_ENCODING
      */
-    public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format)
+    public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format, boolean preserveAttachments)
             throws XMLStreamException, FactoryConfigurationError {
         if (isDebugEnabled) {
             log.debug("Creating MTOMXMLStreamWriter");
             log.debug("OutputStream =" + outStream.getClass());
             log.debug("OMFormat = " + format.toString());
+            log.debug("preserveAttachments = " + preserveAttachments);
         }
         if (isTraceEnabled) {
             log.trace("Call Stack =" + CommonUtils.callStackToString());
         }
         this.format = format;
         this.outStream = outStream;
+        this.preserveAttachments = preserveAttachments;
 
         String encoding = format.getCharSetEncoding();
         if (encoding == null) { //Default encoding is UTF-8
@@ -210,7 +226,14 @@ public class MTOMXMLStreamWriter impleme
                 XOPEncodingStreamWriter encoder = (XOPEncodingStreamWriter)xmlWriter;
                 for (Iterator it = encoder.getContentIDs().iterator(); it.hasNext(); ) {
                     String contentID = (String)it.next();
-                    multipartWriter.writePart(encoder.getDataHandler(contentID), contentID);
+                    DataHandler dataHandler = encoder.getDataHandler(contentID);
+                    if (preserveAttachments || !(dataHandler instanceof DataHandlerExt)) {
+                        multipartWriter.writePart(dataHandler, contentID);
+                    } else {
+                        OutputStream out = multipartWriter.writePart(dataHandler.getContentType(), contentID);
+                        BufferUtils.inputStream2OutputStream(((DataHandlerExt)dataHandler).readOnce(), out);
+                        out.close();
+                    }
                 }
                 // This is for compatibility with writeOptimized
                 for (Iterator it = binaryNodeList.iterator(); it.hasNext();) {

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java Thu Aug 25 20:38:42 2011
@@ -809,7 +809,7 @@ public abstract class NodeImpl implement
 
     public void serialize(OutputStream output, OMOutputFormat format)
             throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format);
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, true);
         try {
             internalSerialize(writer, true);
             // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
@@ -835,7 +835,7 @@ public abstract class NodeImpl implement
 
     public void serializeAndConsume(OutputStream output, OMOutputFormat format)
             throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format);
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, false);
         try {
             internalSerialize(writer, false);
             // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java Thu Aug 25 20:38:42 2011
@@ -447,8 +447,13 @@ public abstract class TextNodeImpl exten
             writeOutput(writer);
         } else {
             try {
-                XMLStreamWriterUtils.writeDataHandler(writer, (DataHandler) getDataHandler(),
-                        contentID, optimize);
+                if (dataHandlerObject instanceof DataHandlerProvider) {
+                    XMLStreamWriterUtils.writeDataHandler(writer, (DataHandlerProvider)dataHandlerObject,
+                            contentID, optimize);
+                } else {
+                    XMLStreamWriterUtils.writeDataHandler(writer, (DataHandler)getDataHandler(),
+                            contentID, optimize);
+                }
             } catch (IOException ex) {
                 throw new OMException("Error reading data handler", ex);
             }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSerializableImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSerializableImpl.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSerializableImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSerializableImpl.java Thu Aug 25 20:38:42 2011
@@ -163,7 +163,7 @@ public abstract class OMSerializableImpl
     }
 
     public void serialize(OutputStream output, OMOutputFormat format) throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format);
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, true);
         try {
             internalSerialize(writer, true);
             // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter
@@ -188,7 +188,7 @@ public abstract class OMSerializableImpl
 
     public void serializeAndConsume(OutputStream output, OMOutputFormat format)
             throws XMLStreamException {
-        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format);
+        MTOMXMLStreamWriter writer = new MTOMXMLStreamWriter(output, format, false);
         try {
             internalSerialize(writer, false);
             // TODO: the flush is necessary because of an issue with the lifecycle of MTOMXMLStreamWriter

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java Thu Aug 25 20:38:42 2011
@@ -405,8 +405,13 @@ public class OMTextImpl extends OMNodeIm
             writeOutput(writer);
         } else {
             try {
-                XMLStreamWriterUtils.writeDataHandler(writer, (DataHandler)getDataHandler(),
-                        contentID, optimize);
+                if (dataHandlerObject instanceof DataHandlerProvider) {
+                    XMLStreamWriterUtils.writeDataHandler(writer, (DataHandlerProvider)dataHandlerObject,
+                            contentID, optimize);
+                } else {
+                    XMLStreamWriterUtils.writeDataHandler(writer, (DataHandler)getDataHandler(),
+                            contentID, optimize);
+                }
             } catch (IOException ex) {
                 throw new OMException("Error reading data handler", ex);
             }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/SOAPTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/SOAPTestSuiteBuilder.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/SOAPTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/SOAPTestSuiteBuilder.java Thu Aug 25 20:38:42 2011
@@ -106,8 +106,8 @@ public class SOAPTestSuiteBuilder extend
         addTest(new org.apache.axiom.ts.soap11.header.TestGetHeadersToProcessWithParser(metaFactory));
         addTest(new org.apache.axiom.ts.soap12.envelope.TestAddElementAfterBody(metaFactory));
         addTest(new org.apache.axiom.ts.soap12.envelope.TestBuildWithAttachments(metaFactory));
-//        addTest(new org.apache.axiom.ts.soap12.envelope.TestMTOMForwardStreaming(metaFactory, true));
-//        addTest(new org.apache.axiom.ts.soap12.envelope.TestMTOMForwardStreaming(metaFactory, false));
+        addTest(new org.apache.axiom.ts.soap12.envelope.TestMTOMForwardStreaming(metaFactory, true));
+        addTest(new org.apache.axiom.ts.soap12.envelope.TestMTOMForwardStreaming(metaFactory, false));
         addTest(new org.apache.axiom.ts.soap12.fault.TestGetNode(metaFactory));
         addTest(new org.apache.axiom.ts.soap12.fault.TestGetNodeWithParser(metaFactory));
         addTest(new org.apache.axiom.ts.soap12.fault.TestMoreChildrenAddition(metaFactory));

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java?rev=1161739&r1=1161738&r2=1161739&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java Thu Aug 25 20:38:42 2011
@@ -32,6 +32,7 @@ import org.apache.axiom.om.OMMetaFactory
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.soap.SOAP12Constants;
 import org.apache.axiom.soap.SOAPBody;
 import org.apache.axiom.soap.SOAPEnvelope;
 import org.apache.axiom.soap.SOAPFactory;
@@ -105,7 +106,8 @@ public class TestMTOMForwardStreaming ex
                         Attachments attachments = new Attachments(pipe1In, contentType);
                         SOAPEnvelope envelope = new MTOMStAXSOAPModelBuilder(
                                 StAXUtils.createXMLStreamReader(attachments.getSOAPPartInputStream()),
-                                        attachments).getSOAPEnvelope();
+                                        metaFactory.getSOAP12Factory(), attachments,
+                                        SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI).getSOAPEnvelope();
                         // The code path executed by serializeAndConsume is significantly different if
                         // the element is built. Therefore we need two different test executions.
                         if (buildSOAPPart) {
@@ -127,7 +129,8 @@ public class TestMTOMForwardStreaming ex
             Attachments attachments = new Attachments(pipe2In, contentType);
             SOAPEnvelope envelope = new MTOMStAXSOAPModelBuilder(
                     StAXUtils.createXMLStreamReader(attachments.getSOAPPartInputStream()),
-                            attachments).getSOAPEnvelope();
+                        metaFactory.getSOAP12Factory(), attachments,
+                        SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI).getSOAPEnvelope();
             OMElement bodyElement = envelope.getBody().getFirstElement();
             Iterator it = bodyElement.getChildElements();
             OMElement data1 = (OMElement)it.next();