chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1654763 - in /chemistry/opencmis/trunk: chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/ chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/ap...
Date Mon, 26 Jan 2015 11:15:46 GMT
Author: fmui
Date: Mon Jan 26 11:15:46 2015
New Revision: 1654763

URL: http://svn.apache.org/r1654763
Log:
Server: provide MIME type to temp stream, if available

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/TempStoreOutputStream.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/MultipartParser.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ThresholdOutputStreamTest.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/TempStoreOutputStream.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/TempStoreOutputStream.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/TempStoreOutputStream.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-api/src/main/java/org/apache/chemistry/opencmis/commons/server/TempStoreOutputStream.java
Mon Jan 26 11:15:46 2015
@@ -41,6 +41,19 @@ import java.io.OutputStream;
 public abstract class TempStoreOutputStream extends OutputStream {
 
     /**
+     * Sets the MIME type of the stream.
+     * 
+     * This method is usually be called once before {@link #getInputStream()} is
+     * called. It might never be called if the MIME type is unknown or multiple
+     * times if previous MIME type detections were inaccurate.
+     * 
+     * @param mimeType
+     *            the MIME type or {@code null} if the MIME type is unknown or
+     *            should be reset to unknown
+     */
+    public abstract void setMimeType(String mimeType);
+
+    /**
      * Returns an {@link InputStream} that serves the content that has been
      * provided to this {@link TempStoreOutputStream} instance.
      * 

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
Mon Jan 26 11:15:46 2015
@@ -18,8 +18,6 @@
  */
 package org.apache.chemistry.opencmis.server.impl.atompub;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigInteger;
@@ -29,7 +27,6 @@ import java.util.Locale;
 import java.util.Map;
 
 import javax.xml.namespace.QName;
-import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
@@ -359,10 +356,10 @@ public final class AtomEntryParser {
 
         // read attributes
         String type = "text";
+        String mimeType = "text/plain";
         for (int i = 0; i < parser.getAttributeCount(); i++) {
             QName attrName = parser.getAttributeName(i);
             if (ATTR_TYPE.equals(attrName.getLocalPart())) {
-                atomContentStream.setMimeType(parser.getAttributeValue(i));
                 if (parser.getAttributeValue(i) != null) {
                     type = parser.getAttributeValue(i).trim().toLowerCase(Locale.ENGLISH);
                 }
@@ -377,19 +374,28 @@ public final class AtomEntryParser {
         }
 
         TempStoreOutputStream tsos = null;
-        byte[] bytes = null;
-        if (type.equals("text") || type.equals("html")) {
-            tsos = readContentBytes(parser);
+        if (type.equals("text")) {
+            mimeType = "text/plain";
+            tsos = readContentBytes(parser, mimeType);
+        } else if (type.equals("html")) {
+            mimeType = "text/html";
+            tsos = readContentBytes(parser, mimeType);
         } else if (type.equals("xhtml")) {
-            bytes = copy(parser);
+            mimeType = "application/xhtml+xml";
+            tsos = copy(parser, mimeType);
         } else if (type.endsWith("/xml") || type.endsWith("+xml")) {
-            bytes = copy(parser);
+            mimeType = type;
+            tsos = copy(parser, mimeType);
         } else if (type.startsWith("text/")) {
-            tsos = readContentBytes(parser);
+            mimeType = type;
+            tsos = readContentBytes(parser, mimeType);
         } else {
-            tsos = readBase64(parser);
+            mimeType = type;
+            tsos = readBase64(parser, mimeType);
         }
 
+        atomContentStream.setMimeType(mimeType);
+
         if (tsos != null) {
             try {
                 atomContentStream.setStream(tsos.getInputStream());
@@ -399,12 +405,6 @@ public final class AtomEntryParser {
                 throw e;
             }
         }
-
-        if (bytes != null) {
-            atomContentStream.setStream(new ByteArrayInputStream(bytes));
-            atomContentStream.setLength(BigInteger.valueOf(bytes.length));
-            cappedStream.deductBytes(bytes.length);
-        }
     }
 
     /**
@@ -431,7 +431,7 @@ public final class AtomEntryParser {
                     if (TAG_MEDIATYPE.equals(name.getLocalPart())) {
                         cmisContentStream.setMimeType(XMLUtils.readText(parser, XMLConstraints.MAX_STRING_LENGTH));
                     } else if (TAG_BASE64.equals(name.getLocalPart())) {
-                        TempStoreOutputStream tsos = readBase64(parser);
+                        TempStoreOutputStream tsos = readBase64(parser, cmisContentStream.getMimeType());
                         try {
                             cmisContentStream.setStream(tsos.getInputStream());
                             cmisContentStream.setLength(BigInteger.valueOf(tsos.getLength()));
@@ -466,8 +466,10 @@ public final class AtomEntryParser {
     /**
      * Parses a tag that contains content bytes.
      */
-    private TempStoreOutputStream readContentBytes(XMLStreamReader parser) throws XMLStreamException,
IOException {
+    private TempStoreOutputStream readContentBytes(XMLStreamReader parser, String mimeType)
throws XMLStreamException,
+            IOException {
         TempStoreOutputStream bufferStream = streamFactory.newOutputStream();
+        bufferStream.setMimeType(mimeType);
 
         XMLUtils.next(parser);
 
@@ -510,8 +512,10 @@ public final class AtomEntryParser {
     /**
      * Parses a tag that contains base64 encoded content.
      */
-    private TempStoreOutputStream readBase64(XMLStreamReader parser) throws XMLStreamException,
IOException {
+    private TempStoreOutputStream readBase64(XMLStreamReader parser, String mimeType) throws
XMLStreamException,
+            IOException {
         TempStoreOutputStream bufferStream = streamFactory.newOutputStream();
+        bufferStream.setMimeType(mimeType);
         Base64.OutputStream b64stream = new Base64.OutputStream(bufferStream, Base64.DECODE);
 
         XMLUtils.next(parser);
@@ -562,42 +566,57 @@ public final class AtomEntryParser {
     /**
      * Copies a subtree into a stream.
      */
-    private static byte[] copy(XMLStreamReader parser) throws XMLStreamException {
+    private TempStoreOutputStream copy(XMLStreamReader parser, String mimeType) throws XMLStreamException,
IOException {
         // create a writer
-        ByteArrayOutputStream out = new ByteArrayOutputStream(32 * 1024);
-        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
+        TempStoreOutputStream bufferStream = streamFactory.newOutputStream();
+        bufferStream.setMimeType(mimeType);
 
-        writer.writeStartDocument();
+        try {
+            XMLStreamWriter writer = XMLUtils.createWriter(bufferStream);
 
-        // copy subtree
-        int level = 1;
-        while (XMLUtils.next(parser)) {
-            int event = parser.getEventType();
-            if (event == XMLStreamReader.START_ELEMENT) {
-                copyStartElement(parser, writer);
-                level++;
-            } else if (event == XMLStreamReader.CHARACTERS) {
-                writer.writeCharacters(parser.getText());
-            } else if (event == XMLStreamReader.COMMENT) {
-                writer.writeComment(parser.getText());
-            } else if (event == XMLStreamReader.CDATA) {
-                writer.writeCData(parser.getText());
-            } else if (event == XMLStreamReader.END_ELEMENT) {
-                level--;
-                if (level == 0) {
+            writer.writeStartDocument();
+
+            // copy subtree
+            int level = 1;
+            while (XMLUtils.next(parser)) {
+                int event = parser.getEventType();
+                if (event == XMLStreamReader.START_ELEMENT) {
+                    copyStartElement(parser, writer);
+                    level++;
+                } else if (event == XMLStreamReader.CHARACTERS) {
+                    writer.writeCharacters(parser.getText());
+                } else if (event == XMLStreamReader.COMMENT) {
+                    writer.writeComment(parser.getText());
+                } else if (event == XMLStreamReader.CDATA) {
+                    writer.writeCData(parser.getText());
+                } else if (event == XMLStreamReader.END_ELEMENT) {
+                    level--;
+                    if (level == 0) {
+                        break;
+                    }
+                    writer.writeEndElement();
+                } else {
                     break;
                 }
-                writer.writeEndElement();
-            } else {
-                break;
             }
-        }
 
-        writer.writeEndDocument();
+            writer.writeEndDocument();
+            writer.flush();
+
+            bufferStream.close();
+        } catch (XMLStreamException xse) {
+            // remove temp file
+            bufferStream.destroy(xse);
+            throw xse;
+        } catch (IOException ioe) {
+            // remove temp file
+            bufferStream.destroy(ioe);
+            throw ioe;
+        }
 
         XMLUtils.next(parser);
 
-        return out.toByteArray();
+        return bufferStream;
     }
 
     /**

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/MultipartParser.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/MultipartParser.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/MultipartParser.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/MultipartParser.java
Mon Jan 26 11:15:46 2015
@@ -413,8 +413,9 @@ public class MultipartParser {
         }
     }
 
-    private void readBodyAsStream() throws IOException {
+    private void readBodyAsStream(String contentType) throws IOException {
         TempStoreOutputStream stream = streamFactory.newOutputStream();
+        stream.setMimeType(contentType);
 
         try {
             while (true) {
@@ -485,7 +486,7 @@ public class MultipartParser {
                 contentType = Constants.MEDIATYPE_OCTETSTREAM;
             }
 
-            readBodyAsStream();
+            readBodyAsStream(contentType);
         } else {
             String name = params.get(MimeHelper.DISPOSITION_NAME);
             byte[] rawValue = readBodyBytes();

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
Mon Jan 26 11:15:46 2015
@@ -74,6 +74,7 @@ public class ThresholdOutputStream exten
     private OutputStream tmpStream;
     private Key key;
     private byte[] iv;
+    private String mimeType;
 
     /**
      * Constructor.
@@ -201,6 +202,14 @@ public class ThresholdOutputStream exten
         }
     }
 
+    public void setMimeType(String mimeType) {
+        this.mimeType = mimeType;
+    }
+
+    public String getMimeType() {
+        return mimeType;
+    }
+
     public long getLength() {
         return length;
     }
@@ -400,6 +409,15 @@ public class ThresholdOutputStream exten
         }
 
         /**
+         * Returns the MIME type of the stream.
+         * 
+         * @return the MIME type or {@code null} if the MIME type is unknown
+         */
+        public String getMimeType() {
+            return mimeType;
+        }
+
+        /**
          * Returns the length of the stream.
          * 
          * @return the length of the stream in bytes

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
Mon Jan 26 11:15:46 2015
@@ -36,6 +36,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.impl.IOUtils;
 import org.apache.chemistry.opencmis.server.impl.atompub.AtomEntryParser;
 import org.apache.chemistry.opencmis.server.shared.TempStoreOutputStreamFactory;
+import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream.ThresholdInputStream;
 import org.junit.Test;
 
 /**
@@ -143,19 +144,19 @@ public class AtomEntryParserTest {
 
     @Test
     public void testCmisContent() throws Exception {
-        byte[] content = parse(CMIS_ENTRY.getBytes());
+        byte[] content = parse(CMIS_ENTRY.getBytes(), "text/plain");
         assertEquals(CMIS_ENTRY_CONTENT, new String(content));
     }
 
     @Test
     public void testAtomContentText() throws Exception {
-        byte[] content = parse(ATOM_ENTRY_TEXT.getBytes());
+        byte[] content = parse(ATOM_ENTRY_TEXT.getBytes(), "text/plain");
         assertEquals(ATOM_ENTRY_TEXT_CONTENT, new String(content));
     }
 
     @Test
     public void testAtomContentXml() throws Exception {
-        byte[] content = parse(ATOM_ENTRY_XML.getBytes());
+        byte[] content = parse(ATOM_ENTRY_XML.getBytes(), "text/xml");
         String xmlContent = new String(content);
         assertTrue(xmlContent.indexOf('>') > -1);
         assertEquals(ATOM_ENTRY_XML_CONTENT, xmlContent.substring(xmlContent.indexOf('>')
+ 1));
@@ -163,7 +164,7 @@ public class AtomEntryParserTest {
 
     @Test
     public void testAtomContentXHtml() throws Exception {
-        byte[] content = parse(ATOM_ENTRY_XHTML.getBytes());
+        byte[] content = parse(ATOM_ENTRY_XHTML.getBytes(), "application/xhtml+xml");
         String xmlContent = new String(content);
         assertTrue(xmlContent.indexOf('>') > -1);
         assertEquals(ATOM_ENTRY_XHTML_CONTENT, xmlContent.substring(xmlContent.indexOf('>')
+ 1));
@@ -171,7 +172,7 @@ public class AtomEntryParserTest {
 
     @Test
     public void testAtomContentBase64() throws Exception {
-        byte[] content = parse(ATOM_ENTRY_BASE64.getBytes());
+        byte[] content = parse(ATOM_ENTRY_BASE64.getBytes(), "application/something");
         assertEquals(ATOM_ENTRY_BASE64_CONTENT, new String(content));
     }
 
@@ -249,7 +250,7 @@ public class AtomEntryParserTest {
         assertNull(aep.getContentStream());
     }
 
-    private static byte[] parse(byte[] entry) throws Exception {
+    private static byte[] parse(byte[] entry, String mimeType) throws Exception {
         TempStoreOutputStreamFactory streamFactory = TempStoreOutputStreamFactory.newInstance(null,
THRESHOLD,
                 MAX_SIZE, false);
         AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(entry), streamFactory);
@@ -258,6 +259,11 @@ public class AtomEntryParserTest {
         assertNotNull(contentStream);
         assertNotNull(contentStream.getStream());
 
+        assertEquals(mimeType, contentStream.getMimeType());
+        if (contentStream.getStream() instanceof ThresholdInputStream) {
+            assertEquals(mimeType, ((ThresholdInputStream) contentStream.getStream()).getMimeType());
+        }
+
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
         IOUtils.copy(contentStream.getStream(), baos);

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ThresholdOutputStreamTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ThresholdOutputStreamTest.java?rev=1654763&r1=1654762&r2=1654763&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ThresholdOutputStreamTest.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/ThresholdOutputStreamTest.java
Mon Jan 26 11:15:46 2015
@@ -36,6 +36,8 @@ import org.junit.Test;
 
 public class ThresholdOutputStreamTest {
 
+    private static final String MIME_TYPE_1 = "some/type";
+    private static final String MIME_TYPE_2 = "another/type";
     private static final byte[] CONTENT = "Hello".getBytes();
 
     @Test
@@ -43,6 +45,7 @@ public class ThresholdOutputStreamTest {
         TempStoreOutputStreamFactory streamFactory = TempStoreOutputStreamFactory.newInstance(null,
1024, 1024, false);
 
         TempStoreOutputStream tempStream = streamFactory.newOutputStream();
+        tempStream.setMimeType(MIME_TYPE_1);
         assertTrue(tempStream instanceof ThresholdOutputStream);
 
         // set content
@@ -58,6 +61,7 @@ public class ThresholdOutputStreamTest {
         assertTrue(tis.markSupported());
         assertEquals(CONTENT.length, tis.getLength());
         assertArrayEquals(CONTENT, getBytesFromArray(tis.getBytes(), (int) tis.getLength()));
+        assertEquals(MIME_TYPE_1, tis.getMimeType());
 
         // read stream
         byte[] buffer = new byte[CONTENT.length];
@@ -99,6 +103,7 @@ public class ThresholdOutputStreamTest {
         TempStoreOutputStreamFactory streamFactory = TempStoreOutputStreamFactory.newInstance(null,
0, 1024, false);
 
         TempStoreOutputStream tempStream = streamFactory.newOutputStream();
+        tempStream.setMimeType(MIME_TYPE_2);
         assertTrue(tempStream instanceof ThresholdOutputStream);
 
         // set content
@@ -113,6 +118,7 @@ public class ThresholdOutputStreamTest {
         assertTrue(tis.markSupported());
         assertNull(tis.getBytes());
         assertEquals(CONTENT.length, tis.getLength());
+        assertEquals(MIME_TYPE_2, tis.getMimeType());
 
         assertTrue(tis.getTemporaryFile().exists());
         assertEquals(CONTENT.length, tis.getTemporaryFile().length());



Mime
View raw message