]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/jcr/utils/JcrFileProvider.java
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
.nodetype
.NodeType
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.ArgeoException
;
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 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
21 * Relies on common approach for JCR file handling implementation.
25 public class JcrFileProvider
implements FileProvider
{
27 // private Object[] rootNodes;
31 * Must be set in order for the provider to be able to get current session
32 * and thus have the ability to get the file node corresponding to a given
35 * FIXME : this introduces some concurrences ISSUES.
37 * @param repositoryNode
39 public void setReferenceNode(Node refNode
) {
40 this.refNode
= refNode
;
44 * Must be set in order for the provider to be able to search the repository
45 * Provided object might be either JCR Nodes or UI RepositoryNode for the
48 * @param repositoryNode
50 // public void setRootNodes(Object[] rootNodes) {
51 // List<Object> tmpNodes = new ArrayList<Object>();
52 // for (int i = 0; i < rootNodes.length; i++) {
53 // Object obj = rootNodes[i];
54 // if (obj instanceof Node) {
56 // } else if (obj instanceof RepositoryRegister) {
57 // RepositoryRegister repositoryRegister = (RepositoryRegister) obj;
58 // Map<String, Repository> repositories = repositoryRegister
59 // .getRepositories();
60 // for (String name : repositories.keySet()) {
61 // // tmpNodes.add(new RepositoryNode(name, repositories
67 // this.rootNodes = tmpNodes.toArray();
70 public byte[] getByteArrayFileFromId(String fileId
) {
71 InputStream fis
= null;
73 Node child
= getFileNodeFromId(fileId
);
75 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
76 .getBinary().getStream();
77 ba
= IOUtils
.toByteArray(fis
);
79 } catch (Exception e
) {
80 throw new ArgeoException("Stream error while opening file", e
);
82 IOUtils
.closeQuietly(fis
);
87 public InputStream
getInputStreamFromFileId(String fileId
) {
89 InputStream fis
= null;
91 Node child
= getFileNodeFromId(fileId
);
92 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
93 .getBinary().getStream();
95 } catch (RepositoryException re
) {
96 throw new ArgeoException("Cannot get stream from file node for Id "
102 * Throws an exception if the node is not found in the current repository (a
103 * bit like a FileNotFoundException)
106 * @return Returns the child node of the nt:file node. It is the child node
107 * that have the jcr:data property where actual file is stored.
110 private Node
getFileNodeFromId(String fileId
) {
112 Node result
= refNode
.getSession().getNodeByIdentifier(fileId
);
114 // rootNodes: for (int j = 0; j < rootNodes.length; j++) {
115 // // in case we have a classic JCR Node
116 // if (rootNodes[j] instanceof Node) {
117 // Node curNode = (Node) rootNodes[j];
118 // if (result != null)
120 // } // Case of a repository Node
121 // else if (rootNodes[j] instanceof RepositoryNode) {
122 // Object[] nodes = ((RepositoryNode) rootNodes[j])
124 // for (int i = 0; i < nodes.length; i++) {
125 // Node node = (Node) nodes[i];
126 // result = node.getSession().getNodeByIdentifier(fileId);
127 // if (result != null)
135 throw new ArgeoException("File node not found for ID" + fileId
);
137 // Ensure that the node have the correct type.
138 if (!result
.isNodeType(NodeType
.NT_FILE
))
139 throw new ArgeoException(
140 "Cannot open file children Node that are not of '"
141 + NodeType
.NT_RESOURCE
+ "' type.");
143 // Get the usefull part of the Node
144 Node child
= result
.getNodes().nextNode();
145 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
146 throw new ArgeoException(
147 "ERROR: IN the current implemented model, '"
149 + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
152 } catch (RepositoryException re
) {
153 throw new ArgeoException("Erreur while getting file node of ID "