X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fjcr%2Futils%2FJcrFileProvider.java;fp=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fjcr%2Futils%2FJcrFileProvider.java;h=6cf4d29c58ba058d0b5a6181de67eeab2d8cda33;hb=a99d11e0c2696ff1a4a258adaa94d137440c8d6c;hp=f8de5f3b54a8ecee9da1900b86d1de0d4d8b1d1c;hpb=e582d16d370908f4ca326ecd04334c301c463072;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java index f8de5f3b5..6cf4d29c5 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java @@ -38,48 +38,74 @@ public class JcrFileProvider implements FileProvider { } public byte[] getByteArrayFileFromId(String fileId) { + InputStream fis = null; + byte[] ba = null; + Node child = getFileNodeFromId(fileId); try { - Object[] nodes = repositoryNode.getChildren(); + fis = (InputStream) child.getProperty("jcr:data").getBinary() + .getStream(); + ba = IOUtils.toByteArray(fis); + + } catch (Exception e) { + throw new ArgeoException("Stream error while opening file", e); + } finally { + IOUtils.closeQuietly(fis); + } + return ba; + } + + public InputStream getInputStreamFromFileId(String fileId) { + try { + InputStream fis = null; + + Node child = getFileNodeFromId(fileId); + fis = (InputStream) child.getProperty("jcr:data").getBinary() + .getStream(); + return fis; + } catch (RepositoryException re) { + throw new ArgeoException("Cannot get stream from file node for Id " + + fileId, re); + } + } + + /** + * Throws an exception if the node is not found in the current repository (a + * bit like a FileNotFoundException) + * + * @param fileId + * @return Returns the child node of the nt:file node. It is the child node + * that have the jcr:data property where actual file is stored. + * never null + */ + private Node getFileNodeFromId(String fileId) { + Object[] nodes = repositoryNode.getChildren(); + try { + Node result = null; repos: for (int i = 0; i < nodes.length; i++) { WorkspaceNode wNode = (WorkspaceNode) nodes[i]; - Node node = null; - node = wNode.getSession().getNodeByIdentifier(fileId); + result = wNode.getSession().getNodeByIdentifier(fileId); - if (node == null) + if (result == null) continue repos; - if (!node.isNodeType("nt:file")) + // Ensure that the node have the correct type. + if (!result.isNodeType("nt:file")) throw new ArgeoException( "Cannot open file children Node that are not of 'nt:resource' type."); - Node child = node.getNodes().nextNode(); - if (!child.isNodeType("nt:resource")) + Node child = result.getNodes().nextNode(); + if (child == null || !child.isNodeType("nt:resource")) throw new ArgeoException( - "Cannot open file children Node that are not of 'nt:resource' type."); + "ERROR: IN the current implemented model, nt:file file node must have one and only one child of the nt:ressource, where actual data is stored"); - InputStream fis = null; - byte[] ba = null; - try { - fis = (InputStream) child.getProperty("jcr:data") - .getBinary().getStream(); - ba = IOUtils.toByteArray(fis); - - } catch (Exception e) { - throw new ArgeoException("Stream error while opening file", - e); - } finally { - IOUtils.closeQuietly(fis); - } - if (ba != null) - return ba; + return child; } - } catch (RepositoryException re) { - throw new ArgeoException("RepositoryException while reading file ", - re); + throw new ArgeoException("Erreur while getting file node of ID " + + fileId, re); } - throw new ArgeoException("File not found for ID " + fileId); + throw new ArgeoException("File node not found for ID" + fileId); } }