1 package org
.argeo
.eclipse
.ui
.jcr
.utils
;
3 import java
.io
.InputStream
;
6 import javax
.jcr
.Property
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
9 import javax
.jcr
.nodetype
.NodeType
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.argeo
.ArgeoException
;
13 import org
.argeo
.eclipse
.ui
.specific
.FileProvider
;
16 * Implements a FileProvider for UI purposes. Unlike the
17 * <code> JcrFileProvider </code>, it relies on a single session and manages
18 * nodes with path only.
20 * Note that considered id is the JCR path
22 * Relies on common approach for JCR file handling implementation.
28 public class SingleSessionFileProvider
implements FileProvider
{
30 private Session session
;
32 public SingleSessionFileProvider(Session session
) {
33 this.session
= session
;
36 public byte[] getByteArrayFileFromId(String fileId
) {
37 InputStream fis
= null;
39 Node child
= getFileNodeFromId(fileId
);
41 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
42 .getBinary().getStream();
43 ba
= IOUtils
.toByteArray(fis
);
45 } catch (Exception e
) {
46 throw new ArgeoException("Stream error while opening file", e
);
48 IOUtils
.closeQuietly(fis
);
53 public InputStream
getInputStreamFromFileId(String fileId
) {
55 InputStream fis
= null;
57 Node child
= getFileNodeFromId(fileId
);
58 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
59 .getBinary().getStream();
61 } catch (RepositoryException re
) {
62 throw new ArgeoException("Cannot get stream from file node for Id "
70 * @return Returns the child node of the nt:file node. It is the child node
71 * that have the jcr:data property where actual file is stored.
74 private Node
getFileNodeFromId(String fileId
) {
77 result
= session
.getNode(fileId
);
81 throw new ArgeoException("File node not found for ID" + fileId
);
83 // Ensure that the node have the correct type.
84 if (!result
.isNodeType(NodeType
.NT_FILE
))
85 throw new ArgeoException(
86 "Cannot open file children Node that are not of "
87 + NodeType
.NT_RESOURCE
+ " type.");
89 Node child
= result
.getNodes().nextNode();
90 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
91 throw new ArgeoException(
92 "ERROR: IN the current implemented model, "
94 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
96 } catch (RepositoryException re
) {
97 throw new ArgeoException("Erreur while getting file node of ID "