]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/utils/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
.utils
;
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
.ArgeoException
;
27 import org
.argeo
.eclipse
.ui
.specific
.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 & 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.
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
50 * FIXME : this introduces some concurrences ISSUES.
52 * @param repositoryNode
54 public void setReferenceNode(Node refNode
) {
55 this.refNode
= refNode
;
59 * Must be set in order for the provider to be able to search the repository
60 * Provided object might be either JCR Nodes or UI RepositoryNode for the
63 * @param repositoryNode
65 // public void setRootNodes(Object[] rootNodes) {
66 // List<Object> tmpNodes = new ArrayList<Object>();
67 // for (int i = 0; i < rootNodes.length; i++) {
68 // Object obj = rootNodes[i];
69 // if (obj instanceof Node) {
71 // } else if (obj instanceof RepositoryRegister) {
72 // RepositoryRegister repositoryRegister = (RepositoryRegister) obj;
73 // Map<String, Repository> repositories = repositoryRegister
74 // .getRepositories();
75 // for (String name : repositories.keySet()) {
76 // // tmpNodes.add(new RepositoryNode(name, repositories
82 // this.rootNodes = tmpNodes.toArray();
85 public byte[] getByteArrayFileFromId(String fileId
) {
86 InputStream fis
= null;
88 Node child
= getFileNodeFromId(fileId
);
90 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
91 .getBinary().getStream();
92 ba
= IOUtils
.toByteArray(fis
);
94 } catch (Exception e
) {
95 throw new ArgeoException("Stream error while opening file", e
);
97 IOUtils
.closeQuietly(fis
);
102 public InputStream
getInputStreamFromFileId(String fileId
) {
104 InputStream fis
= null;
106 Node child
= getFileNodeFromId(fileId
);
107 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
108 .getBinary().getStream();
110 } catch (RepositoryException re
) {
111 throw new ArgeoException("Cannot get stream from file node for Id "
117 * Throws an exception if the node is not found in the current repository (a
118 * bit like a FileNotFoundException)
121 * @return Returns the child node of the nt:file node. It is the child node
122 * that have the jcr:data property where actual file is stored.
125 private Node
getFileNodeFromId(String fileId
) {
127 Node result
= refNode
.getSession().getNodeByIdentifier(fileId
);
129 // rootNodes: for (int j = 0; j < rootNodes.length; j++) {
130 // // in case we have a classic JCR Node
131 // if (rootNodes[j] instanceof Node) {
132 // Node curNode = (Node) rootNodes[j];
133 // if (result != null)
135 // } // Case of a repository Node
136 // else if (rootNodes[j] instanceof RepositoryNode) {
137 // Object[] nodes = ((RepositoryNode) rootNodes[j])
139 // for (int i = 0; i < nodes.length; i++) {
140 // Node node = (Node) nodes[i];
141 // result = node.getSession().getNodeByIdentifier(fileId);
142 // if (result != null)
150 throw new ArgeoException("File node not found for ID" + fileId
);
154 boolean isValid
= true;
155 if (!result
.isNodeType(NodeType
.NT_FILE
))
156 // useless: mandatory child node
157 // || !result.hasNode(Property.JCR_CONTENT))
160 child
= result
.getNode(Property
.JCR_CONTENT
);
161 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
162 .hasProperty(Property
.JCR_DATA
)))
167 throw new ArgeoException(
168 "ERROR: In the current implemented model, '"
170 + "' file node must have a child node named jcr:content "
171 + "that has a BINARY Property named jcr:data "
172 + "where the actual data is stored");
175 } catch (RepositoryException re
) {
176 throw new ArgeoException("Erreur while getting file node of ID "