]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/eclipse/ui/jcr/util/JcrFileProvider.java
Move JCR utilities from API to CMS JCR
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / eclipse / ui / jcr / util / JcrFileProvider.java
1 package org.argeo.eclipse.ui.jcr.util;
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.eclipse.ui.EclipseUiException;
12 import org.argeo.eclipse.ui.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 and 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 @SuppressWarnings("deprecation")
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 * @param refNode
36 */
37 public void setReferenceNode(Node refNode) {
38 // FIXME : this introduces some concurrency ISSUES.
39 this.refNode = refNode;
40 }
41
42 public byte[] getByteArrayFileFromId(String fileId) {
43 InputStream fis = null;
44 byte[] ba = null;
45 Node child = getFileNodeFromId(fileId);
46 try {
47 fis = (InputStream) child.getProperty(Property.JCR_DATA).getBinary().getStream();
48 ba = IOUtils.toByteArray(fis);
49
50 } catch (Exception e) {
51 throw new EclipseUiException("Stream error while opening file", e);
52 } finally {
53 IOUtils.closeQuietly(fis);
54 }
55 return ba;
56 }
57
58 public InputStream getInputStreamFromFileId(String fileId) {
59 try {
60 InputStream fis = null;
61
62 Node child = getFileNodeFromId(fileId);
63 fis = (InputStream) child.getProperty(Property.JCR_DATA).getBinary().getStream();
64 return fis;
65 } catch (RepositoryException re) {
66 throw new EclipseUiException("Cannot get stream from file node for Id " + fileId, re);
67 }
68 }
69
70 /**
71 * Throws an exception if the node is not found in the current repository (a
72 * bit like a FileNotFoundException)
73 *
74 * @param fileId
75 * @return Returns the child node of the nt:file node. It is the child node
76 * that have the jcr:data property where actual file is stored.
77 * never null
78 */
79 private Node getFileNodeFromId(String fileId) {
80 try {
81 Node result = refNode.getSession().getNodeByIdentifier(fileId);
82
83 // rootNodes: for (int j = 0; j < rootNodes.length; j++) {
84 // // in case we have a classic JCR Node
85 // if (rootNodes[j] instanceof Node) {
86 // Node curNode = (Node) rootNodes[j];
87 // if (result != null)
88 // break rootNodes;
89 // } // Case of a repository Node
90 // else if (rootNodes[j] instanceof RepositoryNode) {
91 // Object[] nodes = ((RepositoryNode) rootNodes[j])
92 // .getChildren();
93 // for (int i = 0; i < nodes.length; i++) {
94 // Node node = (Node) nodes[i];
95 // result = node.getSession().getNodeByIdentifier(fileId);
96 // if (result != null)
97 // break rootNodes;
98 // }
99 // }
100 // }
101
102 // Sanity checks
103 if (result == null)
104 throw new EclipseUiException("File node not found for ID" + fileId);
105
106 Node child = null;
107
108 boolean isValid = true;
109 if (!result.isNodeType(NodeType.NT_FILE))
110 // useless: mandatory child node
111 // || !result.hasNode(Property.JCR_CONTENT))
112 isValid = false;
113 else {
114 child = result.getNode(Property.JCR_CONTENT);
115 if (!(child.isNodeType(NodeType.NT_RESOURCE) || child.hasProperty(Property.JCR_DATA)))
116 isValid = false;
117 }
118
119 if (!isValid)
120 throw new EclipseUiException("ERROR: In the current implemented model, '" + NodeType.NT_FILE
121 + "' file node must have a child node named jcr:content "
122 + "that has a BINARY Property named jcr:data " + "where the actual data is stored");
123 return child;
124
125 } catch (RepositoryException re) {
126 throw new EclipseUiException("Erreur while getting file node of ID " + fileId, re);
127 }
128 }
129 }