1 package org
.argeo
.eclipse
.ui
.jcr
.utils
;
3 import java
.io
.InputStream
;
4 import java
.util
.ArrayList
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.nodetype
.NodeType
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.argeo
.ArgeoException
;
16 import org
.argeo
.eclipse
.ui
.jcr
.browser
.RepositoryNode
;
17 import org
.argeo
.eclipse
.ui
.specific
.FileProvider
;
18 import org
.argeo
.jcr
.RepositoryRegister
;
21 * Implements a FileProvider for UI purposes. Note that it might not be very
22 * reliable as long as we have not fixed login & multi repository issues that
23 * will be addressed in the next version.
25 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
27 * Relies on common approach for JCR file handling implementation.
33 public class JcrFileProvider
implements FileProvider
{
35 private Object
[] rootNodes
;
38 * Must be set in order for the provider to be able to search the repository
39 * Provided object might be either JCR Nodes or UI RepositoryNode for the
42 * @param repositoryNode
44 public void setRootNodes(Object
[] rootNodes
) {
45 List
<Object
> tmpNodes
= new ArrayList
<Object
>();
46 for (int i
= 0; i
< rootNodes
.length
; i
++) {
47 Object obj
= rootNodes
[i
];
48 if (obj
instanceof Node
) {
50 } else if (obj
instanceof RepositoryRegister
) {
51 RepositoryRegister repositoryRegister
= (RepositoryRegister
) obj
;
52 Map
<String
, Repository
> repositories
= repositoryRegister
54 for (String name
: repositories
.keySet()) {
55 tmpNodes
.add(new RepositoryNode(name
, repositories
61 this.rootNodes
= tmpNodes
.toArray();
64 public byte[] getByteArrayFileFromId(String fileId
) {
65 InputStream fis
= null;
67 Node child
= getFileNodeFromId(fileId
);
69 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
70 .getBinary().getStream();
71 ba
= IOUtils
.toByteArray(fis
);
73 } catch (Exception e
) {
74 throw new ArgeoException("Stream error while opening file", e
);
76 IOUtils
.closeQuietly(fis
);
81 public InputStream
getInputStreamFromFileId(String fileId
) {
83 InputStream fis
= null;
85 Node child
= getFileNodeFromId(fileId
);
86 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
87 .getBinary().getStream();
89 } catch (RepositoryException re
) {
90 throw new ArgeoException("Cannot get stream from file node for Id "
96 * Throws an exception if the node is not found in the current repository (a
97 * bit like a FileNotFoundException)
100 * @return Returns the child node of the nt:file node. It is the child node
101 * that have the jcr:data property where actual file is stored.
104 private Node
getFileNodeFromId(String fileId
) {
108 rootNodes
: for (int j
= 0; j
< rootNodes
.length
; j
++) {
109 // in case we have a classic JCR Node
110 if (rootNodes
[j
] instanceof Node
) {
111 Node curNode
= (Node
) rootNodes
[j
];
112 result
= curNode
.getSession().getNodeByIdentifier(fileId
);
115 } // Case of a repository Node
116 else if (rootNodes
[j
] instanceof RepositoryNode
) {
117 Object
[] nodes
= ((RepositoryNode
) rootNodes
[j
])
119 for (int i
= 0; i
< nodes
.length
; i
++) {
120 Node node
= (Node
) nodes
[i
];
121 result
= node
.getSession().getNodeByIdentifier(fileId
);
130 throw new ArgeoException("File node not found for ID" + fileId
);
132 // Ensure that the node have the correct type.
133 if (!result
.isNodeType(NodeType
.NT_FILE
))
134 throw new ArgeoException(
135 "Cannot open file children Node that are not of '"
136 + NodeType
.NT_RESOURCE
+ "' type.");
138 // Get the usefull part of the Node
139 Node child
= result
.getNodes().nextNode();
140 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
141 throw new ArgeoException(
142 "ERROR: IN the current implemented model, '"
144 + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
147 } catch (RepositoryException re
) {
148 throw new ArgeoException("Erreur while getting file node of ID "