X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jackrabbit%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fserver%2Fjcr%2FJcrResourceAdapter.java;h=44e6ecc15bb7a18712f15fd9f2ed13a24d214d06;hb=9b0c0818123d0265be3a3d35e4511077109e827a;hp=b770a89a802d1d330b6f90f697ac81f89f13eafd;hpb=5f097724cb2f64045c3e636db5804a5909db096f;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jcr/JcrResourceAdapter.java b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jcr/JcrResourceAdapter.java index b770a89a8..44e6ecc15 100644 --- a/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jcr/JcrResourceAdapter.java +++ b/server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/server/jcr/JcrResourceAdapter.java @@ -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); } }