X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=jcr%2Forg.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrContent.java;h=b32ae302085a059bf67c67ddfcca25c173bc9f4a;hb=eb4cc3db3bf141c229f0f7ff929daff108bee6d2;hp=94fe7cf58ef9f8b2ffd6c3a53f70af265f4a7107;hpb=c615307d7b87bcb260d8a9f402c6e0a880862f38;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 94fe7cf58..b32ae3020 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,10 @@ 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; import java.util.Calendar; import java.util.HashSet; @@ -7,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; @@ -17,50 +23,63 @@ import javax.jcr.RepositoryException; 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.NamespaceUtils; -import org.argeo.api.acr.spi.AbstractContent; +import org.argeo.api.acr.spi.ContentProvider; import org.argeo.api.acr.spi.ProvidedSession; +import org.argeo.api.cms.CmsConstants; +import org.argeo.cms.acr.AbstractContent; +import org.argeo.cms.acr.ContentUtils; 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 { - private Node jcrNode; +// private Node jcrNode; private JcrContentProvider provider; private ProvidedSession session; - protected JcrContent(ProvidedSession session, JcrContentProvider provider, Node node) { + private String jcrWorkspace; + private String jcrPath; + + protected JcrContent(ProvidedSession session, JcrContentProvider provider, String jcrWorkspace, String jcrPath) { this.session = session; this.provider = provider; - this.jcrNode = node; + this.jcrWorkspace = jcrWorkspace; + this.jcrPath = jcrPath; } @Override public QName getName() { - String name = Jcr.getName(jcrNode); + String name = Jcr.getName(getJcrNode()); if (name.equals("")) {// root - name = Jcr.getWorkspaceName(jcrNode); + String mountPath = provider.getMountPath(); + name = ContentUtils.getParentPath(mountPath)[1]; + // name = Jcr.getWorkspaceName(getJcrNode()); } return NamespaceUtils.parsePrefixedName(provider, name); } + @SuppressWarnings("unchecked") @Override public Optional get(QName key, Class clss) { if (isDefaultAttrTypeRequested(clss)) { - return Optional.of((A) get(jcrNode, key.toString())); + return Optional.of((A) get(getJcrNode(), key.toString())); } - return Optional.of((A) Jcr.get(jcrNode, key.toString())); + return Optional.of((A) Jcr.get(getJcrNode(), key.toString())); } @Override public Iterator iterator() { try { - return new JcrContentIterator(jcrNode.getNodes()); + return new JcrContentIterator(getJcrNode().getNodes()); } catch (RepositoryException e) { - throw new JcrException("Cannot list children of " + jcrNode, e); + throw new JcrException("Cannot list children of " + getJcrNode(), e); } } @@ -68,7 +87,7 @@ public class JcrContent extends AbstractContent { protected Iterable keys() { try { Set keys = new HashSet<>(); - properties: for (PropertyIterator propertyIterator = jcrNode.getProperties(); propertyIterator.hasNext();) { + for (PropertyIterator propertyIterator = getJcrNode().getProperties(); propertyIterator.hasNext();) { Property property = propertyIterator.nextProperty(); // TODO convert standard names // TODO skip technical properties @@ -77,25 +96,17 @@ public class JcrContent extends AbstractContent { } return keys; } catch (RepositoryException e) { - throw new JcrException("Cannot list properties of " + jcrNode, e); + throw new JcrException("Cannot list properties of " + getJcrNode(), e); } - -// return new Iterable() { -// -// @Override -// public Iterator iterator() { -// try { -// PropertyIterator propertyIterator = jcrNode.getProperties(); -// return new JcrKeyIterator(provider, propertyIterator); -// } catch (RepositoryException e) { -// throw new JcrException("Cannot retrive properties from " + jcrNode, e); -// } -// } -// }; } public Node getJcrNode() { - return jcrNode; + try { + // TODO caching? + return provider.getJcrSession(session, jcrWorkspace).getNode(jcrPath); + } catch (RepositoryException e) { + throw new JcrException("Cannot retrieve " + jcrPath + " from workspace " + jcrWorkspace, e); + } } /** Cast to a standard Java object. */ @@ -154,7 +165,7 @@ public class JcrContent extends AbstractContent { @Override public Content next() { - current = new JcrContent(session, provider, nodeIterator.nextNode()); + current = new JcrContent(session, provider, jcrWorkspace, Jcr.getPath(nodeIterator.nextNode())); return current; } @@ -169,7 +180,7 @@ public class JcrContent extends AbstractContent { @Override public Content getParent() { - return new JcrContent(session, provider, Jcr.getParent(getJcrNode())); + return new JcrContent(session, provider, jcrWorkspace, Jcr.getParentPath(getJcrNode())); } @Override @@ -209,31 +220,73 @@ public class JcrContent extends AbstractContent { } - class JcrKeyIterator implements Iterator { - private final JcrContentProvider contentSession; - private final PropertyIterator propertyIterator; + /* + * ADAPTERS + */ + public A adapt(Class clss) { + if (Source.class.isAssignableFrom(clss)) { +// try { + PipedInputStream in = new PipedInputStream(); - protected JcrKeyIterator(JcrContentProvider contentSession, PropertyIterator propertyIterator) { - this.contentSession = contentSession; - this.propertyIterator = propertyIterator; - } + 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); + } - @Override - public boolean hasNext() { - return propertyIterator.hasNext(); - } + }); + return (A) new StreamSource(in); +// } catch (IOException e) { +// throw new RuntimeException("Cannot adapt " + JcrContent.this + " to " + clss, e); +// } + } else - @Override - public QName next() { - Property property = null; - try { - property = propertyIterator.nextProperty(); - // TODO map standard property names - return NamespaceUtils.parsePrefixedName(provider, property.getName()); - } catch (RepositoryException e) { - throw new JcrException("Cannot retrieve property " + property, null); + 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); + } + + @Override + public ProvidedSession getSession() { + return session; + } + + @Override + public ContentProvider getProvider() { + return provider; + } + /* + * 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); + } } + }