]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrFileProvider.java
6cf4d29c58ba058d0b5a6181de67eeab2d8cda33
[lgpl/argeo-commons.git] / 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.RepositoryException;
7
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;
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 * 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
22 *
23 * @author bsinou
24 *
25 */
26
27 public class JcrFileProvider implements FileProvider {
28
29 private RepositoryNode repositoryNode;
30
31 /**
32 * Must be set in order for the provider to be able to search the repository
33 *
34 * @param repositoryNode
35 */
36 public void setRepositoryNode(RepositoryNode repositoryNode) {
37 this.repositoryNode = repositoryNode;
38 }
39
40 public byte[] getByteArrayFileFromId(String fileId) {
41 InputStream fis = null;
42 byte[] ba = null;
43 Node child = getFileNodeFromId(fileId);
44 try {
45 fis = (InputStream) child.getProperty("jcr:data").getBinary()
46 .getStream();
47 ba = IOUtils.toByteArray(fis);
48
49 } catch (Exception e) {
50 throw new ArgeoException("Stream error while opening file", e);
51 } finally {
52 IOUtils.closeQuietly(fis);
53 }
54 return ba;
55 }
56
57 public InputStream getInputStreamFromFileId(String fileId) {
58 try {
59 InputStream fis = null;
60
61 Node child = getFileNodeFromId(fileId);
62 fis = (InputStream) child.getProperty("jcr:data").getBinary()
63 .getStream();
64 return fis;
65 } catch (RepositoryException re) {
66 throw new ArgeoException("Cannot get stream from file node for Id "
67 + fileId, re);
68 }
69 }
70
71 /**
72 * Throws an exception if the node is not found in the current repository (a
73 * bit like a FileNotFoundException)
74 *
75 * @param fileId
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.
78 * never null
79 */
80 private Node getFileNodeFromId(String fileId) {
81 Object[] nodes = repositoryNode.getChildren();
82 try {
83 Node result = null;
84
85 repos: for (int i = 0; i < nodes.length; i++) {
86 WorkspaceNode wNode = (WorkspaceNode) nodes[i];
87 result = wNode.getSession().getNodeByIdentifier(fileId);
88
89 if (result == null)
90 continue repos;
91
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.");
96
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");
101
102 return child;
103 }
104 } catch (RepositoryException re) {
105 throw new ArgeoException("Erreur while getting file node of ID "
106 + fileId, re);
107 }
108
109 throw new ArgeoException("File node not found for ID" + fileId);
110 }
111 }