chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fguilla...@apache.org
Subject svn commit: r794695 - in /incubator/chemistry/trunk/chemistry: chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/ chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/ chemistry-commons/src/main/...
Date Thu, 16 Jul 2009 14:40:14 GMT
Author: fguillaume
Date: Thu Jul 16 14:40:14 2009
New Revision: 794695

URL: http://svn.apache.org/viewvc?rev=794695&view=rev
Log:
CMIS-31: Implement getStream and getContentStream for AtomPub Document

Modified:
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPDocument.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientResponse.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Response.java
    incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleDocument.java
    incubator/chemistry/trunk/chemistry/chemistry-parent/pom.xml
    incubator/chemistry/trunk/chemistry/chemistry-tests/pom.xml
    incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPDocument.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPDocument.java?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPDocument.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPDocument.java
Thu Jul 16 14:40:14 2009
@@ -13,32 +13,133 @@
  *
  * Authors:
  *     Bogdan Stefanescu, Nuxeo
+ *     Emanuele Lombardi
+ *     Florent Guillaume, Nuxeo
  */
 package org.apache.chemistry.atompub.client;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Collection;
 
 import org.apache.chemistry.ContentStream;
 import org.apache.chemistry.Document;
+import org.apache.chemistry.Property;
 import org.apache.chemistry.Type;
+import org.apache.chemistry.atompub.CMIS;
+import org.apache.chemistry.atompub.client.connector.Connector;
+import org.apache.chemistry.atompub.client.connector.Request;
+import org.apache.chemistry.atompub.client.connector.Response;
 
 /**
  *
  */
 public class APPDocument extends APPObject implements Document {
 
+    protected static final String UNINITIALIZED_STRING = "__UNINITIALIZED__\0\0\0";
+
+    protected static final URI UNINITIALIZED_URI;
+    static {
+        try {
+            UNINITIALIZED_URI = new URI("http://__UNINITIALIZED__/%00%00%00");
+        } catch (URISyntaxException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
     public APPDocument(APPObjectEntry entry, Type type) {
         super(entry, type);
     }
 
     public ContentStream getContentStream() {
-        throw new UnsupportedOperationException("Not yet implemented");
+        String url = entry.getLink(CMIS.LINK_STREAM);
+        return url == null ? null : new APPContentStream(url);
+    }
+
+    /**
+     * ContentStream class that fetches a remote URL when needed.
+     */
+    public class APPContentStream implements ContentStream {
+
+        protected final Connector connector;
+
+        protected final String url;
+
+        protected String mimeType = UNINITIALIZED_STRING;
+
+        protected String filename = UNINITIALIZED_STRING;
+
+        protected URI uri = UNINITIALIZED_URI;
+
+        protected long length = -1;
+
+        public APPContentStream(String url) {
+            connector = APPDocument.this.entry.connection.connector;
+            this.url = url;
+        }
+
+        public String getMimeType() {
+            if (mimeType == UNINITIALIZED_STRING) {
+                mimeType = getString(Property.CONTENT_STREAM_MIME_TYPE);
+            }
+            return mimeType;
+        }
+
+        public String getFilename() {
+            if (filename == UNINITIALIZED_STRING) {
+                filename = getString(Property.CONTENT_STREAM_FILENAME);
+            }
+            return filename;
+        }
+
+        public URI getURI() {
+            if (uri == UNINITIALIZED_URI) {
+                uri = APPDocument.this.getURI(Property.CONTENT_STREAM_URI);
+            }
+            return uri;
+        }
+
+        public long getLength() {
+            if (length == -1) {
+                Integer value = getInteger(Property.CONTENT_STREAM_LENGTH);
+                return length = value == null ? -1 : value.longValue();
+            }
+            return length;
+        }
+
+        public InputStream getStream() throws IOException {
+            try {
+                Response resp = connector.get(new Request(url));
+                if (!resp.isOk()) {
+                    throw new IOException("Error: " + resp.getStatusCode()
+                            + " fetching: " + url);
+                }
+                if (length == -1) {
+                    // get the "official" length if available
+                    length = resp.getStreamLength();
+                }
+                return resp.getStream();
+            } catch (ContentManagerException e) {
+                throw (IOException) (new IOException(
+                        "Could not fetch stream from: " + url).initCause(e));
+            }
+        }
     }
 
     public InputStream getStream() throws IOException {
-        throw new UnsupportedOperationException("Not yet implemented");
+        String href = entry.getLink(CMIS.LINK_STREAM);
+        if (href == null) {
+            return null;
+        }
+        Response resp = entry.connection.connector.get(new Request(href));
+        if (!resp.isOk()) {
+            throw new ContentManagerException(
+                    "Remote server returned error code: "
+                            + resp.getStatusCode());
+        }
+        return resp.getStream();
     }
 
     public void setContentStream(ContentStream contentStream)

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientResponse.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientResponse.java?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientResponse.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientResponse.java
Thu Jul 16 14:40:14 2009
@@ -31,6 +31,7 @@
 import org.apache.chemistry.atompub.client.stax.ReadContext;
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpMethodBase;
 
 /**
  *
@@ -72,6 +73,13 @@
         }
     }
 
+    public long getStreamLength() {
+        if (method instanceof HttpMethodBase) {
+            return ((HttpMethodBase) method).getResponseContentLength();
+        }
+        return -1;
+    }
+
     public String getString() throws ContentManagerException {
         try {
             return method.getResponseBodyAsString();

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Response.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Response.java?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Response.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Response.java
Thu Jul 16 14:40:14 2009
@@ -37,6 +37,13 @@
 
     InputStream getStream() throws ContentManagerException;
 
+    /**
+     * Gets the stream length.
+     *
+     * @return the stream length, or -1 if not know
+     */
+    long getStreamLength() throws ContentManagerException;
+
     byte[] getBytes() throws ContentManagerException;
 
     String getString() throws ContentManagerException;

Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleDocument.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleDocument.java?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleDocument.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleDocument.java
Thu Jul 16 14:40:14 2009
@@ -104,7 +104,8 @@
             entry.setValue(SimpleProperty.CONTENT_BYTES_KEY, null);
         } else {
             entry.setValue(Property.CONTENT_STREAM_LENGTH,
-                    Integer.valueOf((int) contentStream.getLength())); // cast?
+                    Integer.valueOf((int) contentStream.getLength())); // TODO
+                                                                       // Long
             entry.setValue(Property.CONTENT_STREAM_MIME_TYPE,
                     contentStream.getMimeType());
             entry.setValue(Property.CONTENT_STREAM_FILENAME,

Modified: incubator/chemistry/trunk/chemistry/chemistry-parent/pom.xml
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-parent/pom.xml?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-parent/pom.xml (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-parent/pom.xml Thu Jul 16 14:40:14 2009
@@ -137,6 +137,11 @@
         <version>0.4.0-incubating</version>
       </dependency>
       <dependency>
+        <groupId>commons-io</groupId>
+        <artifactId>commons-io</artifactId>
+        <version>1.4</version>
+      </dependency>
+      <dependency>
         <groupId>commons-collections</groupId>
         <artifactId>commons-collections</artifactId>
         <version>3.1</version>

Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/pom.xml
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/pom.xml?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/pom.xml (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/pom.xml Thu Jul 16 14:40:14 2009
@@ -45,6 +45,10 @@
     </dependency>
 
     <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
+    <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <version>1.1</version>

Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java?rev=794695&r1=794694&r2=794695&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
Thu Jul 16 14:40:14 2009
@@ -17,6 +17,7 @@
  */
 package org.apache.chemistry.test;
 
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -26,6 +27,7 @@
 import org.apache.chemistry.BaseType;
 import org.apache.chemistry.CMISObject;
 import org.apache.chemistry.Connection;
+import org.apache.chemistry.ContentStream;
 import org.apache.chemistry.Document;
 import org.apache.chemistry.Folder;
 import org.apache.chemistry.ObjectEntry;
@@ -34,6 +36,7 @@
 import org.apache.chemistry.Repository;
 import org.apache.chemistry.SPI;
 import org.apache.chemistry.Type;
+import org.apache.commons.io.IOUtils;
 
 /**
  * Basic test on a repository created with {@link BasicHelper#makeRepository}.
@@ -142,4 +145,36 @@
         assertEquals(1, parents.size());
     }
 
+    @SuppressWarnings("null")
+    public void testGetStream() throws Exception {
+        Folder f1 = (Folder) conn.getRootFolder().getChildren(BaseType.FOLDER).get(
+                0);
+        Folder f2 = (Folder) f1.getChildren(BaseType.FOLDER).get(0);
+        Document other = null;
+        Document dog = null;
+        for (CMISObject child : f2.getChildren(null)) {
+            String name = child.getName();
+            if (name.equals("doc 2")) {
+                other = (Document) child;
+            } else if (name.equals("dog.jpg")) {
+                dog = (Document) child;
+            }
+        }
+        assertNotNull("doc 2 not found", other);
+        assertNull(other.getContentStream());
+        assertNull(other.getStream());
+
+        assertNotNull("dog not found", dog);
+        ContentStream cs = dog.getContentStream();
+        assertTrue(cs.getLength() != 0);
+        assertEquals("dog.jpg", cs.getFilename());
+        assertEquals("image/jpeg", cs.getMimeType());
+        assertNotNull(cs.getStream());
+        InputStream in = dog.getStream();
+        assertNotNull(in);
+        byte[] array = IOUtils.toByteArray(in);
+        assertTrue(array.length != 0);
+        assertEquals(array.length, cs.getLength());
+    }
+
 }



Mime
View raw message