]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java
Add a logger
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / utils / JcrFileProvider.java
1 package org.argeo.eclipse.ui.jcr.utils;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.jcr.Node;
9 import javax.jcr.Property;
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.nodetype.NodeType;
13
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;
19
20 /**
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.
24 *
25 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
26 *
27 * Relies on common approach for JCR file handling implementation.
28 *
29 * @author bsinou
30 *
31 */
32
33 public class JcrFileProvider implements FileProvider {
34
35 private Object[] rootNodes;
36
37 /**
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
40 * time being.
41 *
42 * @param repositoryNode
43 */
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) {
49 tmpNodes.add(obj);
50 } else if (obj instanceof RepositoryRegister) {
51 RepositoryRegister repositoryRegister = (RepositoryRegister) obj;
52 Map<String, Repository> repositories = repositoryRegister
53 .getRepositories();
54 for (String name : repositories.keySet()) {
55 tmpNodes.add(new RepositoryNode(name, repositories
56 .get(name)));
57 }
58
59 }
60 }
61 this.rootNodes = tmpNodes.toArray();
62 }
63
64 public byte[] getByteArrayFileFromId(String fileId) {
65 InputStream fis = null;
66 byte[] ba = null;
67 Node child = getFileNodeFromId(fileId);
68 try {
69 fis = (InputStream) child.getProperty(Property.JCR_DATA)
70 .getBinary().getStream();
71 ba = IOUtils.toByteArray(fis);
72
73 } catch (Exception e) {
74 throw new ArgeoException("Stream error while opening file", e);
75 } finally {
76 IOUtils.closeQuietly(fis);
77 }
78 return ba;
79 }
80
81 public InputStream getInputStreamFromFileId(String fileId) {
82 try {
83 InputStream fis = null;
84
85 Node child = getFileNodeFromId(fileId);
86 fis = (InputStream) child.getProperty(Property.JCR_DATA)
87 .getBinary().getStream();
88 return fis;
89 } catch (RepositoryException re) {
90 throw new ArgeoException("Cannot get stream from file node for Id "
91 + fileId, re);
92 }
93 }
94
95 /**
96 * Throws an exception if the node is not found in the current repository (a
97 * bit like a FileNotFoundException)
98 *
99 * @param fileId
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.
102 * never null
103 */
104 private Node getFileNodeFromId(String fileId) {
105 try {
106 Node result = null;
107
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);
113 if (result != null)
114 break rootNodes;
115 } // Case of a repository Node
116 else if (rootNodes[j] instanceof RepositoryNode) {
117 Object[] nodes = ((RepositoryNode) rootNodes[j])
118 .getChildren();
119 for (int i = 0; i < nodes.length; i++) {
120 Node node = (Node) nodes[i];
121 result = node.getSession().getNodeByIdentifier(fileId);
122 if (result != null)
123 break rootNodes;
124 }
125 }
126 }
127
128 // Sanity checks
129 if (result == null)
130 throw new ArgeoException("File node not found for ID" + fileId);
131
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.");
137
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, '"
143 + NodeType.NT_FILE
144 + "' file node must have one and only one child of the nt:ressource, where actual data is stored");
145 return child;
146
147 } catch (RepositoryException re) {
148 throw new ArgeoException("Erreur while getting file node of ID "
149 + fileId, re);
150 }
151 }
152 }