]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/jcr/util/JcrFileProvider.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.jcr
.util
;
18 import java
.io
.InputStream
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.Property
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.nodetype
.NodeType
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
27 import org
.argeo
.eclipse
.ui
.FileProvider
;
30 * Implements a FileProvider for UI purposes. Note that it might not be very
31 * reliable as long as we have not fixed login and multi repository issues that
32 * will be addressed in the next version.
34 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
36 * Relies on common approach for JCR file handling implementation.
39 @SuppressWarnings("deprecation")
40 public class JcrFileProvider
implements FileProvider
{
42 // private Object[] rootNodes;
46 * Must be set in order for the provider to be able to get current session
47 * and thus have the ability to get the file node corresponding to a given
52 public void setReferenceNode(Node refNode
) {
53 // FIXME : this introduces some concurrency ISSUES.
54 this.refNode
= refNode
;
57 public byte[] getByteArrayFileFromId(String fileId
) {
58 InputStream fis
= null;
60 Node child
= getFileNodeFromId(fileId
);
62 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
).getBinary().getStream();
63 ba
= IOUtils
.toByteArray(fis
);
65 } catch (Exception e
) {
66 throw new EclipseUiException("Stream error while opening file", e
);
68 IOUtils
.closeQuietly(fis
);
73 public InputStream
getInputStreamFromFileId(String fileId
) {
75 InputStream fis
= null;
77 Node child
= getFileNodeFromId(fileId
);
78 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
).getBinary().getStream();
80 } catch (RepositoryException re
) {
81 throw new EclipseUiException("Cannot get stream from file node for Id " + fileId
, re
);
86 * Throws an exception if the node is not found in the current repository (a
87 * bit like a FileNotFoundException)
90 * @return Returns the child node of the nt:file node. It is the child node
91 * that have the jcr:data property where actual file is stored.
94 private Node
getFileNodeFromId(String fileId
) {
96 Node result
= refNode
.getSession().getNodeByIdentifier(fileId
);
98 // rootNodes: for (int j = 0; j < rootNodes.length; j++) {
99 // // in case we have a classic JCR Node
100 // if (rootNodes[j] instanceof Node) {
101 // Node curNode = (Node) rootNodes[j];
102 // if (result != null)
104 // } // Case of a repository Node
105 // else if (rootNodes[j] instanceof RepositoryNode) {
106 // Object[] nodes = ((RepositoryNode) rootNodes[j])
108 // for (int i = 0; i < nodes.length; i++) {
109 // Node node = (Node) nodes[i];
110 // result = node.getSession().getNodeByIdentifier(fileId);
111 // if (result != null)
119 throw new EclipseUiException("File node not found for ID" + fileId
);
123 boolean isValid
= true;
124 if (!result
.isNodeType(NodeType
.NT_FILE
))
125 // useless: mandatory child node
126 // || !result.hasNode(Property.JCR_CONTENT))
129 child
= result
.getNode(Property
.JCR_CONTENT
);
130 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
.hasProperty(Property
.JCR_DATA
)))
135 throw new EclipseUiException("ERROR: In the current implemented model, '" + NodeType
.NT_FILE
136 + "' file node must have a child node named jcr:content "
137 + "that has a BINARY Property named jcr:data " + "where the actual data is stored");
140 } catch (RepositoryException re
) {
141 throw new EclipseUiException("Erreur while getting file node of ID " + fileId
, re
);