1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.eclipse.ui.jcr.util;
17
18 import java.io.InputStream;
19
20 import javax.jcr.Node;
21 import javax.jcr.Property;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24 import javax.jcr.nodetype.NodeType;
25
26 import org.apache.commons.io.IOUtils;
27 import org.argeo.eclipse.ui.EclipseUiException;
28 import org.argeo.eclipse.ui.FileProvider;
29
30
31
32
33
34
35
36
37
38
39 @SuppressWarnings("deprecation")
40 public class SingleSessionFileProvider implements FileProvider {
41
42 private Session session;
43
44 public SingleSessionFileProvider(Session session) {
45 this.session = session;
46 }
47
48 public byte[] getByteArrayFileFromId(String fileId) {
49 InputStream fis = null;
50 byte[] ba = null;
51 Node child = getFileNodeFromId(fileId);
52 try {
53 fis = (InputStream) child.getProperty(Property.JCR_DATA)
54 .getBinary().getStream();
55 ba = IOUtils.toByteArray(fis);
56
57 } catch (Exception e) {
58 throw new EclipseUiException("Stream error while opening file", e);
59 } finally {
60 IOUtils.closeQuietly(fis);
61 }
62 return ba;
63 }
64
65 public InputStream getInputStreamFromFileId(String fileId) {
66 try {
67 InputStream fis = null;
68
69 Node child = getFileNodeFromId(fileId);
70 fis = (InputStream) child.getProperty(Property.JCR_DATA)
71 .getBinary().getStream();
72 return fis;
73 } catch (RepositoryException re) {
74 throw new EclipseUiException("Cannot get stream from file node for Id "
75 + fileId, re);
76 }
77 }
78
79
80
81
82
83
84
85
86 private Node getFileNodeFromId(String fileId) {
87 try {
88 Node result = null;
89 result = session.getNode(fileId);
90
91
92 if (result == null)
93 throw new EclipseUiException("File node not found for ID" + fileId);
94
95
96 if (!result.isNodeType(NodeType.NT_FILE))
97 throw new EclipseUiException(
98 "Cannot open file children Node that are not of "
99 + NodeType.NT_RESOURCE + " type.");
100
101 Node child = result.getNodes().nextNode();
102 if (child == null || !child.isNodeType(NodeType.NT_RESOURCE))
103 throw new EclipseUiException(
104 "ERROR: IN the current implemented model, "
105 + NodeType.NT_FILE
106 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
107 return child;
108 } catch (RepositoryException re) {
109 throw new EclipseUiException("Erreur while getting file node of ID "
110 + fileId, re);
111 }
112 }
113 }