1 package org
.argeo
.eclipse
.ui
.jcr
.utils
;
3 import java
.io
.InputStream
;
6 import javax
.jcr
.RepositoryException
;
8 import org
.apache
.commons
.io
.IOUtils
;
9 import org
.argeo
.ArgeoException
;
10 import org
.argeo
.eclipse
.ui
.jcr
.browser
.RepositoryNode
;
11 import org
.argeo
.eclipse
.ui
.jcr
.browser
.WorkspaceNode
;
12 import org
.argeo
.eclipse
.ui
.specific
.FileProvider
;
15 * Implements a FileProvider for UI purposes. Note that it might not be very
16 * reliable as long as we have not fixed login & multi repository issues that
17 * will be addressed in the next version.
19 * We are also very dependant of the repository architecture for file nodes. We
20 * assume the content of the file is stored in a nt:resource child node of the
21 * nt:file in the jcr:data property
27 public class JcrFileProvider
implements FileProvider
{
29 private RepositoryNode repositoryNode
;
32 * Must be set in order for the provider to be able to search the repository
34 * @param repositoryNode
36 public void setRepositoryNode(RepositoryNode repositoryNode
) {
37 this.repositoryNode
= repositoryNode
;
40 public byte[] getByteArrayFileFromId(String fileId
) {
41 InputStream fis
= null;
43 Node child
= getFileNodeFromId(fileId
);
45 fis
= (InputStream
) child
.getProperty("jcr:data").getBinary()
47 ba
= IOUtils
.toByteArray(fis
);
49 } catch (Exception e
) {
50 throw new ArgeoException("Stream error while opening file", e
);
52 IOUtils
.closeQuietly(fis
);
57 public InputStream
getInputStreamFromFileId(String fileId
) {
59 InputStream fis
= null;
61 Node child
= getFileNodeFromId(fileId
);
62 fis
= (InputStream
) child
.getProperty("jcr:data").getBinary()
65 } catch (RepositoryException re
) {
66 throw new ArgeoException("Cannot get stream from file node for Id "
72 * Throws an exception if the node is not found in the current repository (a
73 * bit like a FileNotFoundException)
76 * @return Returns the child node of the nt:file node. It is the child node
77 * that have the jcr:data property where actual file is stored.
80 private Node
getFileNodeFromId(String fileId
) {
81 Object
[] nodes
= repositoryNode
.getChildren();
85 repos
: for (int i
= 0; i
< nodes
.length
; i
++) {
86 WorkspaceNode wNode
= (WorkspaceNode
) nodes
[i
];
87 result
= wNode
.getSession().getNodeByIdentifier(fileId
);
92 // Ensure that the node have the correct type.
93 if (!result
.isNodeType("nt:file"))
94 throw new ArgeoException(
95 "Cannot open file children Node that are not of 'nt:resource' type.");
97 Node child
= result
.getNodes().nextNode();
98 if (child
== null || !child
.isNodeType("nt:resource"))
99 throw new ArgeoException(
100 "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");
104 } catch (RepositoryException re
) {
105 throw new ArgeoException("Erreur while getting file node of ID "
109 throw new ArgeoException("File node not found for ID" + fileId
);