]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/eclipse/ui/jcr/util/SingleSessionFileProvider.java
Move JCR utilities from API to CMS JCR
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / eclipse / ui / jcr / util / SingleSessionFileProvider.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.Session;
9 import javax.jcr.nodetype.NodeType;
10
11 import org.apache.commons.io.IOUtils;
12 import org.argeo.eclipse.ui.EclipseUiException;
13 import org.argeo.eclipse.ui.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 @SuppressWarnings("deprecation")
25 public class SingleSessionFileProvider implements FileProvider {
26
27 private Session session;
28
29 public SingleSessionFileProvider(Session session) {
30 this.session = session;
31 }
32
33 public byte[] getByteArrayFileFromId(String fileId) {
34 InputStream fis = null;
35 byte[] ba = null;
36 Node child = getFileNodeFromId(fileId);
37 try {
38 fis = (InputStream) child.getProperty(Property.JCR_DATA)
39 .getBinary().getStream();
40 ba = IOUtils.toByteArray(fis);
41
42 } catch (Exception e) {
43 throw new EclipseUiException("Stream error while opening file", e);
44 } finally {
45 IOUtils.closeQuietly(fis);
46 }
47 return ba;
48 }
49
50 public InputStream getInputStreamFromFileId(String fileId) {
51 try {
52 InputStream fis = null;
53
54 Node child = getFileNodeFromId(fileId);
55 fis = (InputStream) child.getProperty(Property.JCR_DATA)
56 .getBinary().getStream();
57 return fis;
58 } catch (RepositoryException re) {
59 throw new EclipseUiException("Cannot get stream from file node for Id "
60 + fileId, re);
61 }
62 }
63
64 /**
65 *
66 * @param fileId
67 * @return Returns the child node of the nt:file node. It is the child node
68 * that have the jcr:data property where actual file is stored.
69 * never null
70 */
71 private Node getFileNodeFromId(String fileId) {
72 try {
73 Node result = null;
74 result = session.getNode(fileId);
75
76 // Sanity checks
77 if (result == null)
78 throw new EclipseUiException("File node not found for ID" + fileId);
79
80 // Ensure that the node have the correct type.
81 if (!result.isNodeType(NodeType.NT_FILE))
82 throw new EclipseUiException(
83 "Cannot open file children Node that are not of "
84 + NodeType.NT_RESOURCE + " type.");
85
86 Node child = result.getNodes().nextNode();
87 if (child == null || !child.isNodeType(NodeType.NT_RESOURCE))
88 throw new EclipseUiException(
89 "ERROR: IN the current implemented model, "
90 + NodeType.NT_FILE
91 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
92 return child;
93 } catch (RepositoryException re) {
94 throw new EclipseUiException("Erreur while getting file node of ID "
95 + fileId, re);
96 }
97 }
98 }