]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.ui/src/org/argeo/eclipse/ui/jcr/util/JcrFileProvider.java
c82e666d1dfdd7d2a57727f359155124d894edf9
1 package org
.argeo
.eclipse
.ui
.jcr
.util
;
3 import java
.io
.InputStream
;
6 import javax
.jcr
.Property
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.nodetype
.NodeType
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
12 import org
.argeo
.eclipse
.ui
.FileProvider
;
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.
19 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
21 * Relies on common approach for JCR file handling implementation.
24 @SuppressWarnings("deprecation")
25 public class JcrFileProvider
implements FileProvider
{
27 // private Object[] rootNodes;
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
37 public void setReferenceNode(Node refNode
) {
38 // FIXME : this introduces some concurrency ISSUES.
39 this.refNode
= refNode
;
42 public byte[] getByteArrayFileFromId(String fileId
) {
43 InputStream fis
= null;
45 Node child
= getFileNodeFromId(fileId
);
47 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
).getBinary().getStream();
48 ba
= IOUtils
.toByteArray(fis
);
50 } catch (Exception e
) {
51 throw new EclipseUiException("Stream error while opening file", e
);
53 IOUtils
.closeQuietly(fis
);
58 public InputStream
getInputStreamFromFileId(String fileId
) {
60 InputStream fis
= null;
62 Node child
= getFileNodeFromId(fileId
);
63 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
).getBinary().getStream();
65 } catch (RepositoryException re
) {
66 throw new EclipseUiException("Cannot get stream from file node for Id " + fileId
, re
);
71 * Throws an exception if the node is not found in the current repository (a
72 * bit like a FileNotFoundException)
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.
79 private Node
getFileNodeFromId(String fileId
) {
81 Node result
= refNode
.getSession().getNodeByIdentifier(fileId
);
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)
89 // } // Case of a repository Node
90 // else if (rootNodes[j] instanceof RepositoryNode) {
91 // Object[] nodes = ((RepositoryNode) rootNodes[j])
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)
104 throw new EclipseUiException("File node not found for ID" + fileId
);
108 boolean isValid
= true;
109 if (!result
.isNodeType(NodeType
.NT_FILE
))
110 // useless: mandatory child node
111 // || !result.hasNode(Property.JCR_CONTENT))
114 child
= result
.getNode(Property
.JCR_CONTENT
);
115 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
.hasProperty(Property
.JCR_DATA
)))
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");
125 } catch (RepositoryException re
) {
126 throw new EclipseUiException("Erreur while getting file node of ID " + fileId
, re
);