]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/utils/JcrFileProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / utils / JcrFileProvider.java
1 package org.argeo.eclipse.ui.jcr.utils;
2
3 import java.io.InputStream;
4
5 import javax.jcr.Node;
6 import javax.jcr.Property;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.nodetype.NodeType;
9
10 import org.apache.commons.io.IOUtils;
11 import org.argeo.ArgeoException;
12 import org.argeo.eclipse.ui.specific.FileProvider;
13
14 /**
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.
18 *
19 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
20 *
21 * Relies on common approach for JCR file handling implementation.
22 *
23 */
24
25 public class JcrFileProvider implements FileProvider {
26
27 // private Object[] rootNodes;
28 private Node refNode;
29
30 /**
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
33 * file ID
34 *
35 * FIXME : this introduces some concurrences ISSUES.
36 *
37 * @param repositoryNode
38 */
39 public void setReferenceNode(Node refNode) {
40 this.refNode = refNode;
41 }
42
43 /**
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
46 * time being.
47 *
48 * @param repositoryNode
49 */
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) {
55 // tmpNodes.add(obj);
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
62 // // .get(name)));
63 // }
64 //
65 // }
66 // }
67 // this.rootNodes = tmpNodes.toArray();
68 // }
69
70 public byte[] getByteArrayFileFromId(String fileId) {
71 InputStream fis = null;
72 byte[] ba = null;
73 Node child = getFileNodeFromId(fileId);
74 try {
75 fis = (InputStream) child.getProperty(Property.JCR_DATA)
76 .getBinary().getStream();
77 ba = IOUtils.toByteArray(fis);
78
79 } catch (Exception e) {
80 throw new ArgeoException("Stream error while opening file", e);
81 } finally {
82 IOUtils.closeQuietly(fis);
83 }
84 return ba;
85 }
86
87 public InputStream getInputStreamFromFileId(String fileId) {
88 try {
89 InputStream fis = null;
90
91 Node child = getFileNodeFromId(fileId);
92 fis = (InputStream) child.getProperty(Property.JCR_DATA)
93 .getBinary().getStream();
94 return fis;
95 } catch (RepositoryException re) {
96 throw new ArgeoException("Cannot get stream from file node for Id "
97 + fileId, re);
98 }
99 }
100
101 /**
102 * Throws an exception if the node is not found in the current repository (a
103 * bit like a FileNotFoundException)
104 *
105 * @param fileId
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.
108 * never null
109 */
110 private Node getFileNodeFromId(String fileId) {
111 try {
112 Node result = refNode.getSession().getNodeByIdentifier(fileId);
113
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)
119 // break rootNodes;
120 // } // Case of a repository Node
121 // else if (rootNodes[j] instanceof RepositoryNode) {
122 // Object[] nodes = ((RepositoryNode) rootNodes[j])
123 // .getChildren();
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)
128 // break rootNodes;
129 // }
130 // }
131 // }
132
133 // Sanity checks
134 if (result == null)
135 throw new ArgeoException("File node not found for ID" + fileId);
136
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.");
142
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, '"
148 + NodeType.NT_FILE
149 + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
150 return child;
151
152 } catch (RepositoryException re) {
153 throw new ArgeoException("Erreur while getting file node of ID "
154 + fileId, re);
155 }
156 }
157 }