incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1038961 - in /sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources: JcrFileResource.java PlatformFileResource.java SlingCollectionResource.java SlingResource.java
Date Thu, 25 Nov 2010 08:58:48 GMT
Author: fmeschbe
Date: Thu Nov 25 08:58:48 2010
New Revision: 1038961

URL: http://svn.apache.org/viewvc?rev=1038961&view=rev
Log:
Add (simple) lock and write support

Modified:
    sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/JcrFileResource.java
    sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/PlatformFileResource.java
    sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingCollectionResource.java
    sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingResource.java

Modified: sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/JcrFileResource.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/JcrFileResource.java?rev=1038961&r1=1038960&r2=1038961&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/JcrFileResource.java
(original)
+++ sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/JcrFileResource.java
Thu Nov 25 08:58:48 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.whiteboard.fmeschbe.miltondav.impl.resources;
 
+import java.io.InputStream;
+import java.util.Calendar;
 import java.util.Map;
 
 import javax.jcr.Node;
@@ -28,11 +30,13 @@ import org.apache.sling.api.resource.Res
 
 import com.bradmcevoy.http.CollectionResource;
 import com.bradmcevoy.http.FileItem;
+import com.bradmcevoy.http.ReplaceableResource;
 import com.bradmcevoy.http.exceptions.BadRequestException;
 import com.bradmcevoy.http.exceptions.ConflictException;
 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
 
-public class JcrFileResource extends SlingFileResource {
+public class JcrFileResource extends SlingFileResource implements
+        ReplaceableResource {
 
     JcrFileResource(final Resource slingResource) {
         super(slingResource);
@@ -100,4 +104,22 @@ public class JcrFileResource extends Sli
         return null;
     }
 
+    // ---------- ReplaceableResource
+
+    public void replaceContent(InputStream in, Long length) {
+        Node node = getNode();
+        try {
+            Node content = node.getNode("jcr:content");
+            content.setProperty("jcr:data", in);
+            content.setProperty("jcr:lastModified", Calendar.getInstance());
+            node.getSession().save();
+        } catch (RepositoryException re) {
+        } finally {
+            try {
+                node.getSession().refresh(false);
+            } catch (RepositoryException re) {
+            }
+        }
+    }
+
 }

Modified: sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/PlatformFileResource.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/PlatformFileResource.java?rev=1038961&r1=1038960&r2=1038961&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/PlatformFileResource.java
(original)
+++ sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/PlatformFileResource.java
Thu Nov 25 08:58:48 2010
@@ -31,11 +31,11 @@ import org.apache.sling.api.resource.Res
 
 import com.bradmcevoy.http.CollectionResource;
 import com.bradmcevoy.http.FileItem;
-import com.bradmcevoy.http.exceptions.BadRequestException;
+import com.bradmcevoy.http.ReplaceableResource;
 import com.bradmcevoy.http.exceptions.ConflictException;
-import com.bradmcevoy.http.exceptions.NotAuthorizedException;
 
-public class PlatformFileResource extends SlingFileResource {
+public class PlatformFileResource extends SlingFileResource implements
+        ReplaceableResource {
 
     PlatformFileResource(final Resource slingResource) {
         super(slingResource);
@@ -64,8 +64,7 @@ public class PlatformFileResource extend
         // throw new ConflictException(this);
     }
 
-    public void delete() throws NotAuthorizedException, ConflictException,
-            BadRequestException {
+    public void delete() throws ConflictException {
         if (!getFile().delete()) {
             throw new ConflictException(this);
         }
@@ -88,9 +87,23 @@ public class PlatformFileResource extend
     }
 
     public String processForm(Map<String, String> parameters,
-            Map<String, FileItem> files) throws BadRequestException,
-            NotAuthorizedException, ConflictException {
+            Map<String, FileItem> files) throws ConflictException {
         throw new ConflictException(this);
     }
 
+    // ---------- ReplaceableResource
+
+    public void replaceContent(InputStream in, Long length) {
+        FileOutputStream fos = null;
+        try {
+            fos = new FileOutputStream(getFile());
+            IOUtils.copyLarge(in, fos);
+        } catch (IOException ignore) {
+            // TODO: should actually fail here !
+        } finally {
+            IOUtils.closeQuietly(fos);
+        }
+        return;
+    }
+
 }

Modified: sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingCollectionResource.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingCollectionResource.java?rev=1038961&r1=1038960&r2=1038961&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingCollectionResource.java
(original)
+++ sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingCollectionResource.java
Thu Nov 25 08:58:48 2010
@@ -18,16 +18,29 @@
  */
 package org.apache.sling.whiteboard.fmeschbe.miltondav.impl.resources;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
 
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.io.IOUtils;
+
 import com.bradmcevoy.http.CollectionResource;
 import com.bradmcevoy.http.FolderResource;
+import com.bradmcevoy.http.PutableResource;
 import com.bradmcevoy.http.Resource;
+import com.bradmcevoy.http.exceptions.ConflictException;
 
 public abstract class SlingCollectionResource extends SlingResource implements
-        CollectionResource, FolderResource {
+        CollectionResource, FolderResource, PutableResource {
 
     public SlingCollectionResource(
             org.apache.sling.api.resource.Resource slingResource) {
@@ -47,4 +60,73 @@ public abstract class SlingCollectionRes
         }
         return childList;
     }
+
+    // ---------- PutableResource
+
+    public Resource createNew(String newName, InputStream inputStream,
+            Long length, String contentType) throws IOException,
+            ConflictException {
+        File file = getFile();
+        if (file != null) {
+            FileOutputStream fos = null;
+            try {
+                fos = new FileOutputStream(new File(file, newName));
+                IOUtils.copyLarge(inputStream, fos);
+            } finally {
+                IOUtils.closeQuietly(fos);
+            }
+        }
+
+        Node node = getNode();
+        if (node == null) {
+            try {
+                final Session s = getSlingResource().getResourceResolver().adaptTo(
+                    Session.class);
+                final String path = getSlingResource().getPath();
+                if (s.itemExists(path)) {
+                    node = (Node) s.getItem(path);
+                } else {
+                    String[] segs = path.split("/");
+                    node = s.getRootNode();
+                    for (String seg : segs) {
+                        if (node.hasProperty(seg)) {
+                            // yikes, bail out, fail
+                            throw new ConflictException(this);
+                        } else if (node.hasNode(seg)) {
+                            node = node.getNode(seg);
+                        } else {
+                            node = node.addNode(seg, "sling:Folder");
+                        }
+                    }
+                }
+            } catch (ClassCastException cce) {
+                // item is a property not a node
+                throw new IOException("Cannot create child for " + newName);
+            } catch (RepositoryException re) {
+                // general repository problem
+                throw new IOException("Cannot create child for " + newName
+                    + " (" + re + ")");
+            }
+        }
+
+        try {
+            Node fileNode = node.addNode(newName, "nt:file");
+            Node content = fileNode.addNode("jcr:content", "nt:unstructured");
+            content.setProperty("jcr:data", inputStream);
+            content.setProperty("jcr:lastModified", Calendar.getInstance());
+            content.setProperty("jcr:mimeType", contentType);
+            node.getSession().save();
+        } catch (RepositoryException re) {
+            throw new IOException(re.toString());
+        } finally {
+            try {
+                node.getSession().refresh(false);
+            } catch (RepositoryException re) {
+            }
+        }
+
+        org.apache.sling.api.resource.Resource slingResource = getSlingResource().getChild(
+            newName);
+        return SlingResourceFactory.createResource(slingResource);
+    }
 }

Modified: sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingResource.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingResource.java?rev=1038961&r1=1038960&r2=1038961&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingResource.java
(original)
+++ sling/whiteboard/fmeschbe/milton/src/main/java/org/apache/sling/whiteboard/fmeschbe/miltondav/impl/resources/SlingResource.java
Thu Nov 25 08:58:48 2010
@@ -33,12 +33,23 @@ import org.apache.sling.api.resource.Res
 
 import com.bradmcevoy.http.Auth;
 import com.bradmcevoy.http.GetableResource;
+import com.bradmcevoy.http.LockInfo;
+import com.bradmcevoy.http.LockResult;
+import com.bradmcevoy.http.LockTimeout;
+import com.bradmcevoy.http.LockToken;
+import com.bradmcevoy.http.LockableResource;
 import com.bradmcevoy.http.PropFindableResource;
 import com.bradmcevoy.http.Range;
 import com.bradmcevoy.http.Request;
 import com.bradmcevoy.http.Request.Method;
+import com.bradmcevoy.http.exceptions.NotAuthorizedException;
+import com.ettrema.http.fs.LockManager;
+import com.ettrema.http.fs.SimpleLockManager;
 
-public class SlingResource implements PropFindableResource, GetableResource {
+public class SlingResource implements PropFindableResource, GetableResource,
+        LockableResource {
+
+    private static final LockManager lockManager = new SimpleLockManager();
 
     private final Resource slingResource;
 
@@ -130,4 +141,24 @@ public class SlingResource implements Pr
             }
         }
     }
+
+    // ---------- LockableResource
+
+    public LockResult lock(LockTimeout timeout, LockInfo lockInfo)
+            throws NotAuthorizedException {
+        return lockManager.lock(timeout, lockInfo, this);
+    }
+
+    public LockResult refreshLock(String token) throws NotAuthorizedException {
+        return lockManager.refresh(token, this);
+    }
+
+    public void unlock(String tokenId) throws NotAuthorizedException {
+        lockManager.unlock(tokenId, this);
+    }
+
+    public LockToken getCurrentLock() {
+        return lockManager.getCurrentToken(this);
+    }
+
 }



Mime
View raw message