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=aa6f11b25e95d67b6a0e18e8cea1046ead063dea;hb=1bb92c3e37546f02d79807a08ea6cd114161f795;hp=b770a89a802d1d330b6f90f697ac81f89f13eafd;hpb=e1465ec8281ad7eb0c04175f839cc06dc00703a4;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..aa6f11b25 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,5 +1,6 @@ package org.argeo.server.jcr; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Calendar; @@ -66,8 +67,17 @@ 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); @@ -80,11 +90,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 +108,44 @@ 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)) { + create(path, in, null); + 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); } }