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
.Session
;
24 import javax
.jcr
.nodetype
.NodeType
;
26 import org
.apache
.commons
.io
.IOUtils
;
27 import org
.argeo
.ArgeoException
;
28 import org
.argeo
.eclipse
.ui
.specific
.FileProvider
;
31 * Implements a FileProvider for UI purposes. Unlike the
32 * <code> JcrFileProvider </code>, it relies on a single session and manages
33 * nodes with path only.
35 * Note that considered id is the JCR path
37 * Relies on common approach for JCR file handling implementation.
43 public class SingleSessionFileProvider
implements FileProvider
{
45 private Session session
;
47 public SingleSessionFileProvider(Session session
) {
48 this.session
= session
;
51 public byte[] getByteArrayFileFromId(String fileId
) {
52 InputStream fis
= null;
54 Node child
= getFileNodeFromId(fileId
);
56 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
57 .getBinary().getStream();
58 ba
= IOUtils
.toByteArray(fis
);
60 } catch (Exception e
) {
61 throw new ArgeoException("Stream error while opening file", e
);
63 IOUtils
.closeQuietly(fis
);
68 public InputStream
getInputStreamFromFileId(String fileId
) {
70 InputStream fis
= null;
72 Node child
= getFileNodeFromId(fileId
);
73 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
74 .getBinary().getStream();
76 } catch (RepositoryException re
) {
77 throw new ArgeoException("Cannot get stream from file node for Id "
85 * @return Returns the child node of the nt:file node. It is the child node
86 * that have the jcr:data property where actual file is stored.
89 private Node
getFileNodeFromId(String fileId
) {
92 result
= session
.getNode(fileId
);
96 throw new ArgeoException("File node not found for ID" + fileId
);
98 // Ensure that the node have the correct type.
99 if (!result
.isNodeType(NodeType
.NT_FILE
))
100 throw new ArgeoException(
101 "Cannot open file children Node that are not of "
102 + NodeType
.NT_RESOURCE
+ " type.");
104 Node child
= result
.getNodes().nextNode();
105 if (child
== null || !child
.isNodeType(NodeType
.NT_RESOURCE
))
106 throw new ArgeoException(
107 "ERROR: IN the current implemented model, "
109 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
111 } catch (RepositoryException re
) {
112 throw new ArgeoException("Erreur while getting file node of ID "