Jackrabbit
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / server / jcr / JcrResourceAdapter.java
index b770a89a802d1d330b6f90f697ac81f89f13eafd..44e6ecc15bb7a18712f15fd9f2ed13a24d214d06 100644 (file)
@@ -1,11 +1,13 @@
 package org.argeo.server.jcr;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.List;
 import java.util.StringTokenizer;
 
+import javax.activation.MimetypesFileTypeMap;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
@@ -18,6 +20,7 @@ import javax.jcr.version.Version;
 import javax.jcr.version.VersionHistory;
 import javax.jcr.version.VersionIterator;
 
+import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
@@ -66,11 +69,22 @@ public class JcrResourceAdapter implements InitializingBean, DisposableBean {
 
        public void create(String path, Resource file, String mimeType) {
                try {
-                       if (session().itemExists(path))
+                       create(path, file.getInputStream(), mimeType);
+               } catch (IOException e) {
+                       throw new ArgeoException("Cannot read " + file, e);
+               }
+       }
+
+       public void create(String path, InputStream in, String mimeType) {
+               try {
+                       if (session().itemExists(path)) {
                                throw new ArgeoException("Node " + path + " already exists.");
+                       }
 
                        int index = path.lastIndexOf('/');
                        String parentPath = path.substring(0, index);
+                       if (parentPath.equals(""))
+                               parentPath = "/";
                        String fileName = path.substring(index + 1);
                        if (!session().itemExists(parentPath))
                                throw new ArgeoException("Parent folder of node " + path
@@ -80,11 +94,12 @@ public class JcrResourceAdapter implements InitializingBean, DisposableBean {
                        Node fileNode = folderNode.addNode(fileName, "nt:file");
 
                        Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
-                       contentNode.setProperty("jcr:mimeType", mimeType);
+                       if (mimeType != null)
+                               contentNode.setProperty("jcr:mimeType", mimeType);
                        contentNode.setProperty("jcr:encoding", defaultEncoding);
-                       contentNode.setProperty("jcr:data", file.getInputStream());
+                       contentNode.setProperty("jcr:data", in);
                        Calendar lastModified = Calendar.getInstance();
-                       lastModified.setTimeInMillis(file.lastModified());
+                       // lastModified.setTimeInMillis(file.lastModified());
                        contentNode.setProperty("jcr:lastModified", lastModified);
                        // resNode.addMixin("mix:referenceable");
 
@@ -97,32 +112,46 @@ public class JcrResourceAdapter implements InitializingBean, DisposableBean {
                                fileNode.checkin();
 
                        if (log.isDebugEnabled())
-                               log.debug("Created " + path + " from " + file);
+                               log.debug("Created " + path);
                } catch (Exception e) {
-                       throw new ArgeoException("Cannot create node from resource " + file
-                                       + " under " + path, e);
+                       throw new ArgeoException("Cannot create node for " + path, e);
                }
 
        }
 
        public void update(String path, Resource file) {
                try {
+                       update(path, file.getInputStream());
+               } catch (IOException e) {
+                       throw new ArgeoException("Cannot read " + file, e);
+               }
+       }
+
+       public void update(String path, InputStream in) {
+               try {
+
+                       if (!session().itemExists(path)) {
+                               String type = new MimetypesFileTypeMap()
+                                               .getContentType(FilenameUtils.getName(path));
+                               create(path, in, type);
+                               return;
+                       }
+
                        Node fileNode = (Node) session().getItem(path);
                        Node contentNode = fileNode.getNode("jcr:content");
                        fileNode.checkout();
-                       contentNode.setProperty("jcr:data", file.getInputStream());
+                       contentNode.setProperty("jcr:data", in);
                        Calendar lastModified = Calendar.getInstance();
-                       lastModified.setTimeInMillis(file.lastModified());
+                       // lastModified.setTimeInMillis(file.lastModified());
                        contentNode.setProperty("jcr:lastModified", lastModified);
 
                        session().save();
                        fileNode.checkin();
 
                        if (log.isDebugEnabled())
-                               log.debug("Updated " + path + " from " + file);
+                               log.debug("Updated " + path);
                } catch (Exception e) {
-                       throw new ArgeoException("Cannot update node " + path
-                                       + " from resource" + file, e);
+                       throw new ArgeoException("Cannot update node " + path, e);
                }
        }