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 2012/11/14 10:12:29 UTC

svn commit: r1409122 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ axiom-testsuite/src/main/java/org/apache/axiom/ts/om/ axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/ axi...

Author: veithen
Date: Wed Nov 14 09:12:28 2012
New Revision: 1409122

URL: http://svn.apache.org/viewvc?rev=1409122&view=rev
Log:
AXIOM-442: Implemented streaming of base64 encoded binary content in the SAXSource returned by getSAXSource. 

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java
      - copied, changed from r1409022, webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64Streaming.java
Removed:
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64Streaming.java
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java?rev=1409122&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java Wed Nov 14 09:12:28 2012
@@ -0,0 +1,53 @@
+/*
+ * 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.om.impl.serialize;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * {@link Writer} that writes character data as {@link ContentHandler#characters(char[], int, int)}
+ * to a {@link ContentHandler}.
+ */
+final class ContentHandlerWriter extends Writer {
+    private final ContentHandler contentHandler;
+
+    public ContentHandlerWriter(ContentHandler contentHandler) {
+        this.contentHandler = contentHandler;
+    }
+
+    public void write(char[] cbuf, int off, int len) throws IOException {
+        try {
+            contentHandler.characters(cbuf, off, len);
+        } catch (SAXException ex) {
+            IOException ioException = new IOException();
+            ioException.initCause(ex);
+            throw ioException;
+        }
+    }
+
+    public void close() throws IOException {
+    }
+
+    public void flush() throws IOException {
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ContentHandlerWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java?rev=1409122&r1=1409121&r2=1409122&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/OMXMLReader.java Wed Nov 14 09:12:28 2012
@@ -26,6 +26,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
+import javax.activation.DataHandler;
+
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMComment;
 import org.apache.axiom.om.OMContainer;
@@ -37,6 +39,7 @@ import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMProcessingInstruction;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.util.base64.Base64EncodingWriterOutputStream;
 import org.apache.axiom.util.sax.AbstractXMLReader;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
@@ -227,11 +230,28 @@ public class OMXMLReader extends Abstrac
     }
     
     private void generateEvents(OMText omText, boolean space) throws SAXException {
-        char[] ch = omText.getTextCharacters();
-        if (space) {
-            contentHandler.ignorableWhitespace(ch, 0, ch.length);
+        if (omText.isBinary()) {
+            // Stream the binary content
+            DataHandler dh = (DataHandler)omText.getDataHandler();
+            Base64EncodingWriterOutputStream out = new Base64EncodingWriterOutputStream(new ContentHandlerWriter(contentHandler));
+            try {
+                dh.writeTo(out);
+                out.complete();
+            } catch (IOException ex) {
+                Throwable cause = ex.getCause();
+                if (cause instanceof SAXException) {
+                    throw (SAXException)ex.getCause();
+                } else {
+                    throw new SAXException(ex);
+                }
+            }
         } else {
-            contentHandler.characters(ch, 0, ch.length);
+            char[] ch = omText.getTextCharacters();
+            if (space) {
+                contentHandler.ignorableWhitespace(ch, 0, ch.length);
+            } else {
+                contentHandler.characters(ch, 0, ch.length);
+            }
         }
     }
 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1409122&r1=1409121&r2=1409122&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java Wed Nov 14 09:12:28 2012
@@ -473,7 +473,8 @@ public class OMTestSuiteBuilder extends 
             addTest(new org.apache.axiom.ts.om.sourcedelement.push.TestWriteStartElementWithDefaultNamespaceDeclaredOnParent(metaFactory));
         }
         addTest(new org.apache.axiom.ts.om.pi.TestDigest(metaFactory));
-        addTest(new org.apache.axiom.ts.om.text.TestBase64Streaming(metaFactory));
+        addTest(new org.apache.axiom.ts.om.text.TestBase64StreamingWithGetSAXSource(metaFactory));
+        addTest(new org.apache.axiom.ts.om.text.TestBase64StreamingWithSerialize(metaFactory));
         addTest(new org.apache.axiom.ts.om.text.TestDigest(metaFactory));
         addTest(new org.apache.axiom.ts.om.text.TestGetTextCharactersFromDataHandler(metaFactory));
         Method[] methods = AXIOMXPathTestCase.class.getMethods();

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java?rev=1409122&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java Wed Nov 14 09:12:28 2012
@@ -0,0 +1,92 @@
+/*
+ * 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.ts.om.text;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.xml.transform.sax.SAXSource;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.testutils.activation.RandomDataSource;
+import org.apache.axiom.testutils.io.ByteStreamComparator;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.util.base64.Base64DecodingOutputStreamWriter;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Tests that when an {@link OMText} node is serialized by the {@link SAXSource} returned by
+ * {@link OMContainer#getSAXSource(boolean)}, the implementation doesn't construct an in-memory
+ * base64 representation of the complete binary content, but writes it in chunks (streaming).
+ * <p>
+ * Regression test for <a href="https://issues.apache.org/jira/browse/AXIOM-442">AXIOM-442</a>.
+ */
+public class TestBase64StreamingWithGetSAXSource extends AxiomTestCase {
+    private static class Base64Comparator extends DefaultHandler {
+        private final Writer out;
+        
+        public Base64Comparator(InputStream expected) {
+            out = new Base64DecodingOutputStreamWriter(new ByteStreamComparator(expected));
+        }
+
+        public void characters(char[] ch, int start, int length) throws SAXException {
+            try {
+                out.write(ch, start, length);
+            } catch (IOException ex) {
+                throw new SAXException(ex);
+            }
+        }
+
+        public void endDocument() throws SAXException {
+            try {
+                out.close();
+            } catch (IOException ex) {
+                throw new SAXException(ex);
+            }
+        }
+    }
+    
+    public TestBase64StreamingWithGetSAXSource(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement elem = factory.createOMElement("test", null);
+        // Create a data source that would eat up all memory when loaded. If the test
+        // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
+        // supports streaming.
+        DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
+        OMText text = factory.createOMText(new DataHandler(ds), false);
+        elem.addChild(text);
+        SAXSource saxSource = elem.getSAXSource(true);
+        XMLReader xmlReader = saxSource.getXMLReader();
+        xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
+        xmlReader.parse(saxSource.getInputSource());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithGetSAXSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java (from r1409022, webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64Streaming.java)
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java?p2=webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java&p1=webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64Streaming.java&r1=1409022&r2=1409122&rev=1409122&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64Streaming.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java Wed Nov 14 09:12:28 2012
@@ -36,8 +36,8 @@ import org.apache.commons.io.output.Null
  * <p>
  * Regression test for <a href="https://issues.apache.org/jira/browse/AXIOM-236">AXIOM-236</a>.
  */
-public class TestBase64Streaming extends AxiomTestCase {
-    public TestBase64Streaming(OMMetaFactory metaFactory) {
+public class TestBase64StreamingWithSerialize extends AxiomTestCase {
+    public TestBase64StreamingWithSerialize(OMMetaFactory metaFactory) {
         super(metaFactory);
     }
 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java?rev=1409122&r1=1409121&r2=1409122&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java Wed Nov 14 09:12:28 2012
@@ -30,20 +30,20 @@ public class RandomDataSource implements
     private final long seed;
     private final int rangeStart;
     private final int rangeEnd;
-    private final int length;
+    private final long length;
 
-    public RandomDataSource(long seed, int rangeStart, int rangeEnd, int length) {
+    public RandomDataSource(long seed, int rangeStart, int rangeEnd, long length) {
         this.seed = seed;
         this.rangeStart = rangeStart;
         this.rangeEnd = rangeEnd;
         this.length = length;
     }
     
-    public RandomDataSource(long seed, int length) {
+    public RandomDataSource(long seed, long length) {
         this(seed, 0, 256, length);
     }
     
-    public RandomDataSource(int length) {
+    public RandomDataSource(long length) {
         this(System.currentTimeMillis(), length);
     }
 
@@ -58,7 +58,7 @@ public class RandomDataSource implements
     public InputStream getInputStream() throws IOException {
         final Random random = new Random(seed);
         return new InputStream() {
-            private int position;
+            private long position;
             
             public int read() throws IOException {
                 if (position == length) {