]> 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/SingleSessionFileProvider.java
Add a logger
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / utils / SingleSessionFileProvider.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.Session;
9 import javax.jcr.nodetype.NodeType;
10
11 import org.apache.commons.io.IOUtils;
12 import org.argeo.ArgeoException;
13 import org.argeo.eclipse.ui.specific.FileProvider;
14
15 /**
16 * Implements a FileProvider for UI purposes. Unlike the
17 * <code> JcrFileProvider </code>, it relies on a single session and manages
18 * nodes with path only.
19 *
20 * Note that considered id is the JCR path
21 *
22 * Relies on common approach for JCR file handling implementation.
23 *
24 * @author bsinou
25 *
26 */
27
28 public class SingleSessionFileProvider implements FileProvider {
29
30 private Session session;
31
32 public SingleSessionFileProvider(Session session) {
33 this.session = session;
34 }
35
36 public byte[] getByteArrayFileFromId(String fileId) {
37 InputStream fis = null;
38 byte[] ba = null;
39 Node child = getFileNodeFromId(fileId);
40 try {
41 fis = (InputStream) child.getProperty(Property.JCR_DATA)
42 .getBinary().getStream();
43 ba = IOUtils.toByteArray(fis);
44
45 } catch (Exception e) {
46 throw new ArgeoException("Stream error while opening file", e);
47 } finally {
48 IOUtils.closeQuietly(fis);
49 }
50 return ba;
51 }
52
53 public InputStream getInputStreamFromFileId(String fileId) {
54 try {
55 InputStream fis = null;
56
57 Node child = getFileNodeFromId(fileId);
58 fis = (InputStream) child.getProperty(Property.JCR_DATA)
59 .getBinary().getStream();
60 return fis;
61 } catch (RepositoryException re) {
62 throw new ArgeoException("Cannot get stream from file node for Id "
63 + fileId, re);
64 }
65 }
66
67 /**
68 *
69 * @param fileId
70 * @return Returns the child node of the nt:file node. It is the child node
71 * that have the jcr:data property where actual file is stored.
72 * never null
73 */
74 private Node getFileNodeFromId(String fileId) {
75 try {
76 Node result = null;
77 result = session.getNode(fileId);
78
79 // Sanity checks
80 if (result == null)
81 throw new ArgeoException("File node not found for ID" + fileId);
82
83 // Ensure that the node have the correct type.
84 if (!result.isNodeType(NodeType.NT_FILE))
85 throw new ArgeoException(
86 "Cannot open file children Node that are not of "
87 + NodeType.NT_RESOURCE + " type.");
88
89 Node child = result.getNodes().nextNode();
90 if (child == null || !child.isNodeType(NodeType.NT_RESOURCE))
91 throw new ArgeoException(
92 "ERROR: IN the current implemented model, "
93 + NodeType.NT_FILE
94 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
95 return child;
96 } catch (RepositoryException re) {
97 throw new ArgeoException("Erreur while getting file node of ID "
98 + fileId, re);
99 }
100 }
101 }