1 package org
.argeo
.eclipse
.ui
.jcr
.util
;
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
.eclipse
.ui
.EclipseUiException
;
13 import org
.argeo
.eclipse
.ui
.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.
24 @SuppressWarnings("deprecation")
25 public class SingleSessionFileProvider
implements FileProvider
{
27 private Session session
;
29 public SingleSessionFileProvider(Session session
) {
30 this.session
= session
;
33 public byte[] getByteArrayFileFromId(String fileId
) {
34 InputStream fis
= null;
36 Node child
= getFileNodeFromId(fileId
);
38 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
39 .getBinary().getStream();
40 ba
= IOUtils
.toByteArray(fis
);
42 } catch (Exception e
) {
43 throw new EclipseUiException("Stream error while opening file", e
);
45 IOUtils
.closeQuietly(fis
);
50 public InputStream
getInputStreamFromFileId(String fileId
) {
52 InputStream fis
= null;
54 Node child
= getFileNodeFromId(fileId
);
55 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
56 .getBinary().getStream();
58 } catch (RepositoryException re
) {
59 throw new EclipseUiException("Cannot get stream from file node for Id "
67 * @return Returns the child node of the nt:file node. It is the child node
68 * that have the jcr:data property where actual file is stored.
71 private Node
getFileNodeFromId(String fileId
) {
74 result
= session
.getNode(fileId
);
78 throw new EclipseUiException("File node not found for ID" + fileId
);
80 // Ensure that the node have the correct type.
81 if (!result
.isNodeType(NodeType
.NT_FILE
))
82 throw new EclipseUiException(
83 "Cannot open file children Node that are not of "
84 + NodeType
.NT_RESOURCE
+ " type.");
86 Node child
= result
.getNodes().nextNode();
87 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
88 throw new EclipseUiException(
89 "ERROR: IN the current implemented model, "
91 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
93 } catch (RepositoryException re
) {
94 throw new EclipseUiException("Erreur while getting file node of ID "