chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fguilla...@apache.org
Subject svn commit: r886201 - in /incubator/chemistry/trunk/chemistry: chemistry-api/src/main/java/org/apache/chemistry/ chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/ chemistry-atompub-client/src/main/java/org/apache/chemistry/ato...
Date Wed, 02 Dec 2009 16:44:21 GMT
Author: fguillaume
Date: Wed Dec  2 16:44:15 2009
New Revision: 886201

URL: http://svn.apache.org/viewvc?rev=886201&view=rev
Log:
Implemented setContentStream/deleteContentStream for AtomPub client and server

Modified:
    incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/ContentStream.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
    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/APPObjectEntry.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntryReader.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Connector.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientConnector.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISCheckedOutCollection.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISChildrenCollection.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISObjectsCollection.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISParentsCollection.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISQueryFeed.java
    incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/AtomPub.java
    incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
    incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java

Modified: incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/ContentStream.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/ContentStream.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/ContentStream.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/ContentStream.java
Wed Dec  2 16:44:15 2009
@@ -41,6 +41,9 @@
 
     /**
      * The actual byte stream for this content stream.
+     * <p>
+     * Note that some implementations may allow the stream to be fetched and
+     * read only once.
      *
      * @throws IOException
      */

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPConnection.java
Wed Dec  2 16:44:15 2009
@@ -19,6 +19,7 @@
 package org.apache.chemistry.atompub.client;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -60,6 +61,7 @@
 import org.apache.chemistry.atompub.client.connector.Response;
 import org.apache.chemistry.atompub.client.stax.ReadContext;
 import org.apache.chemistry.atompub.client.stax.XmlProperty;
+import org.apache.chemistry.impl.simple.SimpleContentStream;
 import org.apache.chemistry.impl.simple.SimpleListPage;
 import org.apache.chemistry.impl.simple.SimpleObjectId;
 
@@ -296,38 +298,16 @@
      * ----- Object Services -----
      */
 
-    /*
-     * TODO hardcoded Chemistry URL pattern here...
-     */
+    // TODO add hints about what we'd like to fetch from the entry (stream,
+    // props, etc.)
     protected APPObjectEntry getObjectEntry(ObjectId objectId) {
         if (objectId instanceof APPObjectEntry) {
-            return ((APPObjectEntry) objectId);
+            return (APPObjectEntry) objectId;
         }
-        String href;
         URITemplate uriTemplate = repository.getURITemplate(AtomPubCMIS.URITMPL_OBJECT_BY_ID);
-        if (uriTemplate != null) {
-            // use entry-by-id URI template
-            href = uriTemplate.template;
-            // TODO proper URI template syntax
-            href = href.replace("{id}", objectId.getId());
-        } else {
-            // TODO do a search (maybe 4 searches as base type unknown)
-
-            // XXX hardcoded Chemistry URL pattern
-            href = repository.getCollectionHref(AtomPubCMIS.COL_ROOT);
-            if (href.matches(".*/children/[0-9a-f-]{36}")) {
-                href = href.substring(0, href.length() - "/children/".length()
-                        - 36);
-            } else {
-                if (href.matches(".*/children$")) {
-                    href = href.substring(0, href.length()
-                            - "/children".length());
-                } else {
-                    throw new AssertionError(href);
-                }
-            }
-            href += "/object/" + objectId.getId();
-        }
+        String href = uriTemplate.template;
+        // TODO proper URI template syntax
+        href = href.replace("{id}", objectId.getId());
         Response resp = connector.get(new Request(href));
         if (!resp.isOk()) {
             throw new ContentManagerException(
@@ -443,26 +423,92 @@
     }
 
     public boolean hasContentStream(ObjectId document) {
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException();
+        APPObjectEntry current = getObjectEntry(document);
+        ContentStream cs = current.getContentStream();
+        if (cs == null) {
+            return false;
+        }
+        if (cs != APPObjectEntry.REMOTE_CONTENT_STREAM) {
+            return true;
+        }
+        String href = current.getContentHref();
+        return href != null;
     }
 
     public ContentStream getContentStream(ObjectId object,
             String contentStreamId) throws IOException {
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException();
+        APPObjectEntry current = getObjectEntry(object);
+        ContentStream cs = current.getContentStream();
+        if (cs != APPObjectEntry.REMOTE_CONTENT_STREAM) {
+            return cs;
+        }
+
+        // must fetch the content stream
+        String href = current.getContentHref();
+        if (href == null) {
+            throw new RuntimeException("Object is missing content src");
+        }
+        Request req = new Request(href);
+        Response resp = connector.get(req);
+        if (!resp.isOk()) {
+            // TODO exceptions
+            throw new ContentManagerException(
+                    "Remote server returned error code: "
+                            + resp.getStatusCode());
+        }
+        // get MIME type and filename from entry
+        InputStream stream = resp.getStream();
+        // String mimeType = resp.getHeader("Content-Type");
+        String mimeType = (String) current.getValue(Property.CONTENT_STREAM_MIME_TYPE);
+        String filename = (String) current.getValue(Property.CONTENT_STREAM_FILE_NAME);
+        cs = new SimpleContentStream(stream, mimeType, filename);
+        // current.localContentStream = cs; // problem reusing the stream
+        return cs;
     }
 
     public ObjectId setContentStream(ObjectId document, boolean overwrite,
-            ContentStream contentStream) {
-        // LINK_EDIT_MEDIA
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException();
+            ContentStream cs) throws IOException {
+        APPObjectEntry current = getObjectEntry(document);
+        String href = current.getLink(AtomPub.LINK_EDIT_MEDIA);
+        if (href == null) {
+            throw new RuntimeException("Document is missing link "
+                    + AtomPub.LINK_EDIT_MEDIA);
+        }
+        Request req = new Request(href);
+        String filename = cs.getFileName();
+        if (filename != null) {
+            // Use Slug: header for filename
+            req.setHeader(AtomPub.HEADER_SLUG, filename);
+        }
+        Response resp = connector.put(req, cs.getStream(), cs.getLength(),
+                cs.getMimeType());
+        if (!resp.isOk()) {
+            // TODO exceptions
+            throw new ContentManagerException(
+                    "Remote server returned error code: "
+                            + resp.getStatusCode());
+        }
+        // TODO AtomPub cannot return a new id... (autoversioning)
+        return new SimpleObjectId(document.getId());
     }
 
     public ObjectId deleteContentStream(ObjectId document) {
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException();
+        APPObjectEntry current = getObjectEntry(document);
+        String href = current.getLink(AtomPub.LINK_EDIT_MEDIA);
+        if (href == null) {
+            throw new RuntimeException("Document is missing link "
+                    + AtomPub.LINK_EDIT_MEDIA);
+        }
+        Response resp = connector.delete(new Request(href));
+        if (!resp.isOk()) {
+            // TODO exceptions
+            throw new ContentManagerException(
+                    "Remote server returned error code: "
+                            + resp.getStatusCode());
+        }
+
+        // TODO AtomPub cannot return a new id... (autoversioning)
+        return new SimpleObjectId(document.getId());
     }
 
     public ObjectId updateProperties(ObjectId object, String changeToken,
@@ -478,6 +524,10 @@
         update._setValue(Property.NAME, current.getValue(Property.NAME));
 
         String href = current.getLink(AtomPub.LINK_EDIT);
+        if (href == null) {
+            throw new RuntimeException("Object is missing link "
+                    + AtomPub.LINK_EDIT);
+        }
         Request req = new Request(href);
         req.setHeader("Content-Type", AtomPub.MEDIA_TYPE_ATOM_ENTRY);
         Response resp = connector.putObject(req, update);

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=886201&r1=886200&r2=886201&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
Wed Dec  2 16:44:15 2009
@@ -28,7 +28,6 @@
 import org.apache.chemistry.Document;
 import org.apache.chemistry.Property;
 import org.apache.chemistry.Type;
-import org.apache.chemistry.atompub.AtomPub;
 import org.apache.chemistry.atompub.client.connector.Connector;
 import org.apache.chemistry.atompub.client.connector.Request;
 import org.apache.chemistry.atompub.client.connector.Response;
@@ -58,7 +57,7 @@
         if (contentStream != APPObjectEntry.REMOTE_CONTENT_STREAM) {
             return contentStream;
         }
-        String url = entry.getLink(AtomPub.LINK_EDIT_MEDIA);
+        String url = entry.getContentHref();
         return url == null ? null : new APPContentStream(url);
     }
 

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntry.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntry.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntry.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntry.java
Wed Dec  2 16:44:15 2009
@@ -61,6 +61,10 @@
 
     protected Map<QName, Boolean> allowableActions;
 
+    protected String remoteContentHref;
+
+    protected String remoteContentType;
+
     protected final List<Link> links;
 
     public static class Link {
@@ -112,6 +116,15 @@
         links = new ArrayList<Link>();
     }
 
+    public void addContentHref(String href, String type) {
+        remoteContentHref = href;
+        remoteContentType = type;
+    }
+
+    public String getContentHref() {
+        return remoteContentHref;
+    }
+
     public void addLink(String rel, String href, String type) {
         links.add(new Link(rel, href, type));
     }

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntryReader.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntryReader.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntryReader.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObjectEntryReader.java
Wed Dec  2 16:44:15 2009
@@ -69,7 +69,12 @@
     @Override
     protected void readAtomElement(ReadContext ctx, StaxReader reader,
             APPObjectEntry object) throws XMLStreamException {
-        if (AtomPub.ATOM_LINK.equals(reader.getName())) {
+        QName name = reader.getName();
+        if (AtomPub.ATOM_CONTENT.equals(name)) {
+            String href = reader.getAttributeValue(AtomPub.ATOM_NS, "src");
+            String type = reader.getAttributeValue(AtomPub.ATOM_NS, "type");
+            object.addContentHref(href, type);
+        } else if (AtomPub.ATOM_LINK.equals(name)) {
             String rel = reader.getAttributeValue(AtomPub.ATOM_NS, "rel");
             String href = reader.getAttributeValue(AtomPub.ATOM_NS, "href");
             String type = reader.getAttributeValue(AtomPub.ATOM_NS, "type");

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Connector.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Connector.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Connector.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/Connector.java
Wed Dec  2 16:44:15 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.chemistry.atompub.client.connector;
 
+import java.io.InputStream;
 import java.util.List;
 
 import org.apache.chemistry.ObjectEntry;
@@ -40,6 +41,9 @@
     <T> Response put(Request operation, XmlObjectWriter<T> writer, T object)
             throws ContentManagerException;
 
+    Response put(Request operation, InputStream in, long length, String type)
+            throws ContentManagerException;
+
     Response get(Request operation) throws ContentManagerException;
 
     Response head(Request operation) throws ContentManagerException;

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientConnector.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientConnector.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientConnector.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/connector/HttpClientConnector.java
Wed Dec  2 16:44:15 2009
@@ -18,6 +18,7 @@
 package org.apache.chemistry.atompub.client.connector;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.List;
 
@@ -35,6 +36,7 @@
 import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
@@ -149,6 +151,20 @@
         }
     }
 
+    public Response put(Request request, InputStream in, long length,
+            String type) throws ContentManagerException {
+        try {
+            PutMethod method = new PutMethod(request.getUrl());
+            setMethodParams(method, request);
+            setMethodHeaders(method, request);
+            method.setRequestEntity(new InputStreamRequestEntity(in, length, type));
+            client.executeMethod(method);
+            return new HttpClientResponse(method, io);
+        } catch (Exception e) {
+            throw new ContentManagerException("PUT request failed", e);
+        }
+    }
+
     public Type getType(ReadContext ctx, String href) {
         Request req = new Request(href);
         Response resp = get(req);

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISCheckedOutCollection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISCheckedOutCollection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISCheckedOutCollection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISCheckedOutCollection.java
Wed Dec  2 16:44:15 2009
@@ -60,22 +60,27 @@
     @Override
     public Iterable<ObjectEntry> getEntries(RequestContext request)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        Target target = request.getTarget();
-        String folderIdString = target.getParameter(AtomPubCMIS.PARAM_FOLDER_ID);
-        ObjectId folderId = folderIdString == null ? null
-                : spi.newObjectId(folderIdString);
-        String filter = target.getParameter(AtomPubCMIS.PARAM_FILTER);
-        int maxItems = getParameter(request, AtomPubCMIS.PARAM_MAX_ITEMS, 0);
-        int skipCount = getParameter(request, AtomPubCMIS.PARAM_SKIP_COUNT, 0);
-        boolean includeAllowableActions = getParameter(request,
-                AtomPubCMIS.PARAM_INCLUDE_ALLOWABLE_ACTIONS, false);
-        boolean includeRelationships = getParameter(request,
-                AtomPubCMIS.PARAM_INCLUDE_RELATIONSHIPS, false);
-        ListPage<ObjectEntry> objectEntries = spi.getCheckedOutDocuments(
-                folderId, filter, includeAllowableActions,
-                includeRelationships, new Paging(maxItems, skipCount));
-        return objectEntries;
+        SPI spi = repository.getSPI();
+        try {
+            Target target = request.getTarget();
+            String folderIdString = target.getParameter(AtomPubCMIS.PARAM_FOLDER_ID);
+            ObjectId folderId = folderIdString == null ? null
+                    : spi.newObjectId(folderIdString);
+            String filter = target.getParameter(AtomPubCMIS.PARAM_FILTER);
+            int maxItems = getParameter(request, AtomPubCMIS.PARAM_MAX_ITEMS, 0);
+            int skipCount = getParameter(request, AtomPubCMIS.PARAM_SKIP_COUNT,
+                    0);
+            boolean includeAllowableActions = getParameter(request,
+                    AtomPubCMIS.PARAM_INCLUDE_ALLOWABLE_ACTIONS, false);
+            boolean includeRelationships = getParameter(request,
+                    AtomPubCMIS.PARAM_INCLUDE_RELATIONSHIPS, false);
+            ListPage<ObjectEntry> objectEntries = spi.getCheckedOutDocuments(
+                    folderId, filter, includeAllowableActions,
+                    includeRelationships, new Paging(maxItems, skipCount));
+            return objectEntries;
+        } finally {
+            spi.close();
+        }
     }
 
 }

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISChildrenCollection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISChildrenCollection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISChildrenCollection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISChildrenCollection.java
Wed Dec  2 16:44:15 2009
@@ -17,11 +17,15 @@
  */
 package org.apache.chemistry.atompub.server;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import javax.activation.MimeType;
+
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
@@ -31,6 +35,8 @@
 import org.apache.abdera.protocol.server.Target;
 import org.apache.abdera.protocol.server.context.ResponseContextException;
 import org.apache.axiom.om.OMElement;
+import org.apache.chemistry.ContentAlreadyExistsException;
+import org.apache.chemistry.ContentStream;
 import org.apache.chemistry.ListPage;
 import org.apache.chemistry.ObjectEntry;
 import org.apache.chemistry.ObjectId;
@@ -38,9 +44,12 @@
 import org.apache.chemistry.Property;
 import org.apache.chemistry.Repository;
 import org.apache.chemistry.SPI;
+import org.apache.chemistry.UpdateConflictException;
 import org.apache.chemistry.atompub.AtomPub;
 import org.apache.chemistry.atompub.AtomPubCMIS;
+import org.apache.chemistry.impl.simple.SimpleContentStream;
 import org.apache.chemistry.impl.simple.SimpleListPage;
+import org.apache.chemistry.impl.simple.SimpleObjectId;
 
 /**
  * CMIS Collection for the children of an object.
@@ -75,9 +84,13 @@
                 AtomPub.MEDIA_TYPE_ATOM_FEED, null, null, -1);
 
         // link to parent children feed, needs parent id
+        ObjectEntry entry;
         SPI spi = repository.getSPI();
-        ObjectEntry entry = spi.getProperties(spi.newObjectId(id), null, false,
-                false);
+        try {
+            entry = spi.getProperties(spi.newObjectId(id), null, false, false);
+        } finally {
+            spi.close();
+        }
         if (entry == null) {
             throw new ResponseContextException("Not found: " + id, 404);
         }
@@ -86,7 +99,6 @@
             feed.addLink(getChildrenLink(pid, request), AtomPub.LINK_UP,
                     AtomPub.MEDIA_TYPE_ATOM_FEED, null, null, -1);
         }
-        spi.close();
 
         // AtomPub paging
         // next
@@ -144,37 +156,77 @@
     @Override
     public ListPage<ObjectEntry> getEntries(RequestContext request)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        ObjectId objectId = spi.newObjectId(id);
-        Target target = request.getTarget();
-        String filter = target.getParameter(AtomPubCMIS.PARAM_FILTER);
-        boolean includeAllowableActions = getParameter(request,
-                AtomPubCMIS.PARAM_INCLUDE_ALLOWABLE_ACTIONS, false);
-        boolean includeRelationships = getParameter(request,
-                AtomPubCMIS.PARAM_INCLUDE_RELATIONSHIPS, false);
-        // TODO proper renditionFilter use
-        boolean includeRenditions = target.getParameter(AtomPubCMIS.PARAM_RENDITION_FILTER)
== null ? false
-                : true;
-        String orderBy = target.getParameter(AtomPubCMIS.PARAM_ORDER_BY);
-        if ("descendants".equals(getType())) {
-            int depth = getParameter(request, AtomPubCMIS.PARAM_DEPTH, 1);
-            List<ObjectEntry> descendants = spi.getDescendants(objectId, depth,
-                    filter, includeAllowableActions, includeRelationships,
-                    includeRenditions, orderBy);
-            SimpleListPage<ObjectEntry> page = new SimpleListPage<ObjectEntry>(
-                    descendants);
-            page.setHasMoreItems(false);
-            page.setNumItems(page.size());
-            return page;
-        } else {
-            int maxItems = getParameter(request, AtomPubCMIS.PARAM_MAX_ITEMS, 0);
-            int skipCount = getParameter(request, AtomPubCMIS.PARAM_SKIP_COUNT,
-                    0);
-            ListPage<ObjectEntry> children = spi.getChildren(objectId, filter,
-                    includeAllowableActions, includeRelationships,
-                    includeRenditions, orderBy, new Paging(maxItems, skipCount));
-            return children;
+        SPI spi = repository.getSPI();
+        try {
+            ObjectId objectId = spi.newObjectId(id);
+            Target target = request.getTarget();
+            String filter = target.getParameter(AtomPubCMIS.PARAM_FILTER);
+            boolean includeAllowableActions = getParameter(request,
+                    AtomPubCMIS.PARAM_INCLUDE_ALLOWABLE_ACTIONS, false);
+            boolean includeRelationships = getParameter(request,
+                    AtomPubCMIS.PARAM_INCLUDE_RELATIONSHIPS, false);
+            // TODO proper renditionFilter use
+            boolean includeRenditions = target.getParameter(AtomPubCMIS.PARAM_RENDITION_FILTER)
== null ? false
+                    : true;
+            String orderBy = target.getParameter(AtomPubCMIS.PARAM_ORDER_BY);
+            if ("descendants".equals(getType())) {
+                int depth = getParameter(request, AtomPubCMIS.PARAM_DEPTH, 1);
+                List<ObjectEntry> descendants = spi.getDescendants(objectId,
+                        depth, filter, includeAllowableActions,
+                        includeRelationships, includeRenditions, orderBy);
+                SimpleListPage<ObjectEntry> page = new SimpleListPage<ObjectEntry>(
+                        descendants);
+                page.setHasMoreItems(false);
+                page.setNumItems(page.size());
+                return page;
+            } else {
+                int maxItems = getParameter(request,
+                        AtomPubCMIS.PARAM_MAX_ITEMS, 0);
+                int skipCount = getParameter(request,
+                        AtomPubCMIS.PARAM_SKIP_COUNT, 0);
+                ListPage<ObjectEntry> children = spi.getChildren(objectId,
+                        filter, includeAllowableActions, includeRelationships,
+                        includeRenditions, orderBy, new Paging(maxItems,
+                                skipCount));
+                return children;
+            }
+        } finally {
+            spi.close();
+        }
+    }
+
+    @Override
+    public void putMedia(ObjectEntry entry, MimeType contentType, String slug,
+            InputStream in, RequestContext request)
+            throws ResponseContextException {
+        SPI spi = repository.getSPI();
+        try {
+            ContentStream cs = new SimpleContentStream(in,
+                    contentType.toString(), slug);
+            spi.setContentStream(entry, true, cs);
+        } catch (IOException e) {
+            throw new ResponseContextException(e.toString(), 500);
+        } catch (UpdateConflictException e) {
+            throw new ResponseContextException(e.toString(), 409); // Conflict
+        } catch (ContentAlreadyExistsException e) {
+            // cannot happen, overwrite = true
+            throw new ResponseContextException(e.toString(), 409); // Conflict
+        } finally {
+            spi.close();
         }
     }
 
+    @Override
+    public void deleteMedia(String resourceName, RequestContext request)
+            throws ResponseContextException {
+        SPI spi = repository.getSPI();
+        try {
+            String id = getResourceName(request);
+            spi.deleteContentStream(new SimpleObjectId(id));
+        } catch (UpdateConflictException e) {
+            throw new ResponseContextException(e.toString(), 409); // Conflict
+        } finally {
+            spi.close();
+        }
+    }
 }

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISObjectsCollection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISObjectsCollection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISObjectsCollection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISObjectsCollection.java
Wed Dec  2 16:44:15 2009
@@ -116,6 +116,15 @@
         return rc;
     }
 
+    protected ResponseContext buildCreateMediaResponse(String mediaLink,
+            Entry entry) {
+        BaseResponseContext<Entry> rc = new BaseResponseContext<Entry>(entry);
+        rc.setLocation(mediaLink);
+        rc.setContentLocation(mediaLink);
+        rc.setStatus(201);
+        return rc;
+    }
+
     /*
      * ----- CollectionInfo -----
      */
@@ -179,6 +188,8 @@
             entry.addLink(getDescendantsLink(oid, request), AtomPub.LINK_DOWN,
                     AtomPubCMIS.MEDIA_TYPE_CMIS_TREE, null, null, -1);
         } else if (baseType == BaseType.DOCUMENT) {
+            // edit-media link always needed for setContentStream
+            entry.addLink(getMediaLink(oid, request), AtomPub.LINK_EDIT_MEDIA);
             // TODO don't add link if no parents
             entry.addLink(getParentsLink(oid, request), AtomPub.LINK_UP,
                     AtomPub.MEDIA_TYPE_ATOM_FEED, null, null, -1);
@@ -330,10 +341,10 @@
     public ResponseContext postEntry(RequestContext request) {
         // TODO parameter sourceFolderId
         // TODO parameter versioningState
+        SPI spi = repository.getSPI();
         try {
             PropertiesAndStream posted = extractCMISProperties(request, null);
 
-            SPI spi = repository.getSPI(); // TODO XXX connection leak
             ObjectId folderId = spi.newObjectId(id);
             ObjectId objectId;
             String typeId = (String) posted.properties.get(Property.TYPE_ID);
@@ -374,6 +385,8 @@
             return createErrorResponse(new ResponseContextException(500, e));
         } catch (Exception e) {
             return createErrorResponse(new ResponseContextException(500, e));
+        } finally {
+            spi.close();
         }
     }
 
@@ -387,10 +400,10 @@
 
     @Override
     public ResponseContext putEntry(RequestContext request) {
+        SPI spi = repository.getSPI();
         try {
             // existing object
             String id = getResourceName(request);
-            SPI spi = repository.getSPI(); // TODO XXX connection leak
             ObjectEntry object = spi.getProperties(spi.newObjectId(id), null,
                     false, false);
             if (object == null) {
@@ -423,7 +436,8 @@
             } else {
                 addContent(entry, object, request);
             }
-            return buildGetEntryResponse(request, entry);
+            String mediaLink = getMediaLink(object.getId(), request);
+            return buildCreateMediaResponse(mediaLink, entry);
 
         } catch (ResponseContextException e) {
             return createErrorResponse(e);
@@ -431,6 +445,8 @@
             return createErrorResponse(new ResponseContextException(500, e));
         } catch (Exception e) {
             return createErrorResponse(new ResponseContextException(500, e));
+        } finally {
+            spi.close();
         }
     }
 
@@ -467,9 +483,14 @@
     @Override
     public boolean isMediaEntry(ObjectEntry object)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        return getContentType(object) != null && getContentSize(object) != -1
-                && spi.hasContentStream(object);
+        SPI spi = repository.getSPI();
+        try {
+            return getContentType(object) != null
+                    && getContentSize(object) != -1
+                    && spi.hasContentStream(object);
+        } finally {
+            spi.close();
+        }
     }
 
     @Override
@@ -478,8 +499,7 @@
             throws ResponseContextException {
         String mediaLink = getMediaLink(object.getId(), request);
         entry.setContent(new IRI(mediaLink), getContentType(object));
-        entry.addLink(mediaLink, AtomPub.LINK_EDIT_MEDIA,
-                getContentType(object), null, null, getContentSize(object));
+        // LINK_EDIT_MEDIA already added (always) by addEntryDetails
         return mediaLink;
     }
 
@@ -487,12 +507,9 @@
     @Override
     public Object getContent(ObjectEntry object, RequestContext request)
             throws ResponseContextException {
-        Factory factory = request.getAbdera().getFactory();
-        Content content = factory.newContent();
-        content.setContentType(null);
-        String mediaLink = getMediaLink(object.getId(), request);
-        content.setSrc(mediaLink);
-        return content;
+        // no content (content stream is exposed as media entry)
+        // mandatory LINK_ALTERNATE already added by addEntryDetails
+        return null;
     }
 
     public long getContentSize(ObjectEntry object) {
@@ -512,16 +529,21 @@
     @Override
     public ObjectEntry getEntry(String resourceName, RequestContext request)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        if ("path".equals(getType())) {
-            String path = resourceName;
-            if (!path.startsWith("/")) {
-                path = "/" + path;
-            }
-            return spi.getObjectByPath(path, null, false, false);
-        } else { // object
-            String id = resourceName;
-            return spi.getProperties(spi.newObjectId(id), null, false, false);
+        SPI spi = repository.getSPI();
+        try {
+            if ("path".equals(getType())) {
+                String path = resourceName;
+                if (!path.startsWith("/")) {
+                    path = "/" + path;
+                }
+                return spi.getObjectByPath(path, null, false, false);
+            } else { // object
+                String id = resourceName;
+                return spi.getProperties(spi.newObjectId(id), null, false,
+                        false);
+            }
+        } finally {
+            spi.close();
         }
     }
 
@@ -553,12 +575,14 @@
     public InputStream getMediaStream(ObjectEntry object)
             throws ResponseContextException {
         // TODO entry was fetched for mostly nothing...
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
+        SPI spi = repository.getSPI();
         try {
             ContentStream contentStream = spi.getContentStream(object, null);
             return contentStream == null ? null : contentStream.getStream();
         } catch (IOException e) {
             throw new ResponseContextException(500, e);
+        } finally {
+            spi.close();
         }
     }
 

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISParentsCollection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISParentsCollection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISParentsCollection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISParentsCollection.java
Wed Dec  2 16:44:15 2009
@@ -54,10 +54,15 @@
     @Override
     public Iterable<ObjectEntry> getEntries(RequestContext request)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        ObjectId objectId = spi.newObjectId(id);
-        Collection<ObjectEntry> parents = spi.getObjectParents(objectId, null);
-        return parents;
+        SPI spi = repository.getSPI();
+        try {
+            ObjectId objectId = spi.newObjectId(id);
+            Collection<ObjectEntry> parents = spi.getObjectParents(objectId,
+                    null);
+            return parents;
+        } finally {
+            spi.close();
+        }
     }
 
 }

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISQueryFeed.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISQueryFeed.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISQueryFeed.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISQueryFeed.java
Wed Dec  2 16:44:15 2009
@@ -92,17 +92,22 @@
     @Override
     public Iterable<ObjectEntry> getEntries(RequestContext request)
             throws ResponseContextException {
-        SPI spi = repository.getSPI(); // TODO XXX connection leak
-        boolean searchAllVersions = false;
-        boolean includeAllowableActions = false;
-        boolean includeRelationships = false;
-        boolean includeRenditions = false;
-        int maxItems = -1;
-        int skipCount = 0;
-        ListPage<ObjectEntry> results = spi.query(statement, searchAllVersions,
-                includeAllowableActions, includeRelationships,
-                includeRenditions, new Paging(maxItems, skipCount));
-        return results;
+        SPI spi = repository.getSPI();
+        try {
+            boolean searchAllVersions = false;
+            boolean includeAllowableActions = false;
+            boolean includeRelationships = false;
+            boolean includeRenditions = false;
+            int maxItems = -1;
+            int skipCount = 0;
+            ListPage<ObjectEntry> results = spi.query(statement,
+                    searchAllVersions, includeAllowableActions,
+                    includeRelationships, includeRenditions, new Paging(
+                            maxItems, skipCount));
+            return results;
+        } finally {
+            spi.close();
+        }
     }
 
 }

Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/AtomPub.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/AtomPub.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/AtomPub.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/AtomPub.java
Wed Dec  2 16:44:15 2009
@@ -43,11 +43,19 @@
 
     public static final QName ATOM_ENTRY = new QName(ATOM_NS, "entry");
 
+    public static final QName ATOM_CONTENT = new QName(ATOM_NS, "content");
+
     public static final QName ATOM_LINK = new QName(ATOM_NS, "link");
 
     public static final QName APP_COLLECTION = new QName(APP_NS, "collection");
 
     /*
+     * ----- Headers -----
+     */
+
+    public static final String HEADER_SLUG = "Slug";
+
+    /*
      * ----- Media Types -----
      */
 

Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java?rev=886201&r1=886200&r2=886201&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
(original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleConnection.java
Wed Dec  2 16:44:15 2009
@@ -617,8 +617,7 @@
     }
 
     public ObjectId deleteContentStream(ObjectId document) {
-        // TODO Auto-generated method stub
-        throw new UnsupportedOperationException();
+        return setContentStream(document, true, null);
     }
 
     public ObjectId updateProperties(ObjectId object, String changeToken,

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=886201&r1=886200&r2=886201&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
Wed Dec  2 16:44:15 2009
@@ -53,6 +53,7 @@
 import org.apache.chemistry.SPI;
 import org.apache.chemistry.Type;
 import org.apache.chemistry.impl.simple.SimpleContentStream;
+import org.apache.chemistry.impl.simple.SimpleObjectId;
 import org.apache.chemistry.util.GregorianCalendar;
 import org.apache.commons.io.IOUtils;
 
@@ -324,6 +325,37 @@
         assertEquals(array.length, cs.getLength());
     }
 
+    public void testContentStreamSPI() throws Exception {
+        // set
+        ObjectEntry ob = spi.getObjectByPath("/folder 1/doc 1", null, false,
+                false);
+        SimpleObjectId id = new SimpleObjectId(ob.getId());
+        assertFalse(spi.hasContentStream(id)); // unfetched
+        assertFalse(spi.hasContentStream(ob)); // fetched
+        byte[] blobBytes = "A file...\n".getBytes("UTF-8");
+        String filename = "doc.txt";
+        ContentStream cs = new SimpleContentStream(blobBytes,
+                "text/plain;charset=UTF-8", filename);
+        spi.setContentStream(ob, true, cs);
+
+        // refetch
+        assertTrue(spi.hasContentStream(id));
+        cs = spi.getContentStream(id, null);
+        assertNotNull(cs);
+        assertEquals(filename, cs.getFileName());
+        assertEquals("text/plain;charset=UTF-8", cs.getMimeType().replace(" ",
+                ""));
+        InputStream in = cs.getStream();
+        assertNotNull(in);
+        byte[] array = IOUtils.toByteArray(in);
+        assertEquals(blobBytes.length, array.length);
+        assertEquals(blobBytes.length, cs.getLength());
+
+        // delete
+        spi.deleteContentStream(id);
+        assertFalse(spi.hasContentStream(id));
+    }
+
     public void testNewDocument() throws Exception {
         Folder root = conn.getRootFolder();
         assertNull(getDocumentChild(root));



Mime
View raw message