X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=inline;f=jcr%2Forg.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrContent.java;h=dab41979498957a91a3906eb0402acfe4c85b57d;hb=279880f110272df643cee670aa976ae5ab9ec396;hp=6d274c60c039a0a00b8939035cef5d257b02fae2;hpb=0e2b79a501ec1f8932840ed401dd8ce93602a9fd;p=lgpl%2Fargeo-commons.git
diff --git a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
index 6d274c60c..dab419794 100644
--- a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
+++ b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
@@ -1,5 +1,8 @@
package org.argeo.cms.jcr.acr;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.ArrayList;
@@ -9,6 +12,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
+import java.util.concurrent.ForkJoinPool;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
@@ -20,13 +24,17 @@ import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
import org.argeo.api.acr.Content;
+import org.argeo.api.acr.ContentUtils;
import org.argeo.api.acr.NamespaceUtils;
import org.argeo.api.acr.spi.AbstractContent;
import org.argeo.api.acr.spi.ProvidedSession;
+import org.argeo.api.cms.CmsConstants;
import org.argeo.jcr.Jcr;
import org.argeo.jcr.JcrException;
+import org.argeo.jcr.JcrUtils;
/** A JCR {@link Node} accessed as {@link Content}. */
public class JcrContent extends AbstractContent {
@@ -49,7 +57,9 @@ public class JcrContent extends AbstractContent {
public QName getName() {
String name = Jcr.getName(getJcrNode());
if (name.equals("")) {// root
- name = Jcr.getWorkspaceName(getJcrNode());
+ String mountPath = provider.getMountPath();
+ name = ContentUtils.getParentPath(mountPath)[1];
+ // name = Jcr.getWorkspaceName(getJcrNode());
}
return NamespaceUtils.parsePrefixedName(provider, name);
}
@@ -214,12 +224,40 @@ public class JcrContent extends AbstractContent {
*/
public A adapt(Class clss) {
if (Source.class.isAssignableFrom(clss)) {
+// try {
PipedInputStream in = new PipedInputStream();
- PipedOutputStream out = new PipedOutputStream();
- }
-// provider.getJcrSession(session, jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
- return super.adapt(clss);
+ ForkJoinPool.commonPool().execute(() -> {
+ try (PipedOutputStream out = new PipedOutputStream(in)) {
+ provider.getJcrSession(session, jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
+ out.flush();
+ } catch (IOException | RepositoryException e) {
+ throw new RuntimeException("Cannot export " + jcrPath + " in workspace " + jcrWorkspace, e);
+ }
+
+ });
+ return (A) new StreamSource(in);
+// } catch (IOException e) {
+// throw new RuntimeException("Cannot adapt " + JcrContent.this + " to " + clss, e);
+// }
+ } else
+
+ return super.adapt(clss);
+ }
+
+ @Override
+ public C open(Class clss) throws IOException, IllegalArgumentException {
+ if (InputStream.class.isAssignableFrom(clss)) {
+ Node node = getJcrNode();
+ if (Jcr.isNodeType(node, NodeType.NT_FILE)) {
+ try {
+ return (C) JcrUtils.getFileAsStream(node);
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot open " + jcrPath + " in workspace " + jcrWorkspace, e);
+ }
+ }
+ }
+ return super.open(clss);
}
// class JcrKeyIterator implements Iterator {
@@ -247,4 +285,22 @@ public class JcrContent extends AbstractContent {
// }
//
// }
+ /*
+ * STATIC UTLITIES
+ */
+ public static Content nodeToContent(Node node) {
+ if (node == null)
+ return null;
+ try {
+ ProvidedSession contentSession = (ProvidedSession) node.getSession()
+ .getAttribute(ProvidedSession.class.getName());
+ if (contentSession == null)
+ throw new IllegalArgumentException(
+ "Cannot adapt " + node + " to content, because it was not loaded from a content session");
+ return contentSession.get(CmsConstants.SYS_WORKSPACE + node.getPath());
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot adapt " + node + " to a content", e);
+ }
+ }
+
}