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.
31 public class JcrFileProvider
implements FileProvider
{
33 private Object
[] rootNodes
;
36 * Must be set in order for the provider to be able to search the repository
37 * Provided object might be either JCR Nodes or UI RepositoryNode for the
40 * @param repositoryNode
42 public void setRootNodes(Object
[] rootNodes
) {
43 List
<Object
> tmpNodes
= new ArrayList
<Object
>();
44 for (int i
= 0; i
< rootNodes
.length
; i
++) {
45 Object obj
= rootNodes
[i
];
46 if (obj
instanceof Node
) {
48 } else if (obj
instanceof RepositoryRegister
) {
49 RepositoryRegister repositoryRegister
= (RepositoryRegister
) obj
;
50 Map
<String
, Repository
> repositories
= repositoryRegister
52 for (String name
: repositories
.keySet()) {
53 tmpNodes
.add(new RepositoryNode(name
, repositories
59 this.rootNodes
= tmpNodes
.toArray();
62 public byte[] getByteArrayFileFromId(String fileId
) {
63 InputStream fis
= null;
65 Node child
= getFileNodeFromId(fileId
);
67 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
68 .getBinary().getStream();
69 ba
= IOUtils
.toByteArray(fis
);
71 } catch (Exception e
) {
72 throw new ArgeoException("Stream error while opening file", e
);
74 IOUtils
.closeQuietly(fis
);
79 public InputStream
getInputStreamFromFileId(String fileId
) {
81 InputStream fis
= null;
83 Node child
= getFileNodeFromId(fileId
);
84 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
85 .getBinary().getStream();
87 } catch (RepositoryException re
) {
88 throw new ArgeoException("Cannot get stream from file node for Id "
94 * Throws an exception if the node is not found in the current repository (a
95 * bit like a FileNotFoundException)
98 * @return Returns the child node of the nt:file node. It is the child node
99 * that have the jcr:data property where actual file is stored.
102 private Node
getFileNodeFromId(String fileId
) {
106 rootNodes
: for (int j
= 0; j
< rootNodes
.length
; j
++) {
107 // in case we have a classic JCR Node
108 if (rootNodes
[j
] instanceof Node
) {
109 Node curNode
= (Node
) rootNodes
[j
];
110 result
= curNode
.getSession().getNodeByIdentifier(fileId
);
113 } // Case of a repository Node
114 else if (rootNodes
[j
] instanceof RepositoryNode
) {
115 Object
[] nodes
= ((RepositoryNode
) rootNodes
[j
])
117 for (int i
= 0; i
< nodes
.length
; i
++) {
118 Node node
= (Node
) nodes
[i
];
119 result
= node
.getSession().getNodeByIdentifier(fileId
);
128 throw new ArgeoException("File node not found for ID" + fileId
);
130 // Ensure that the node have the correct type.
131 if (!result
.isNodeType(NodeType
.NT_FILE
))
132 throw new ArgeoException(
133 "Cannot open file children Node that are not of '"
134 + NodeType
.NT_RESOURCE
+ "' type.");
136 // Get the usefull part of the Node
137 Node child
= result
.getNodes().nextNode();
138 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
139 throw new ArgeoException(
140 "ERROR: IN the current implemented model, '"
142 + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
145 } catch (RepositoryException re
) {
146 throw new ArgeoException("Erreur while getting file node of ID "