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
.eclipse
.ui
.EclipseUiException
;
28 import org
.argeo
.eclipse
.ui
.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.
39 @SuppressWarnings("deprecation")
40 public class SingleSessionFileProvider
implements FileProvider
{
42 private Session session
;
44 public SingleSessionFileProvider(Session session
) {
45 this.session
= session
;
48 public byte[] getByteArrayFileFromId(String fileId
) {
49 InputStream fis
= null;
51 Node child
= getFileNodeFromId(fileId
);
53 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
54 .getBinary().getStream();
55 ba
= IOUtils
.toByteArray(fis
);
57 } catch (Exception e
) {
58 throw new EclipseUiException("Stream error while opening file", e
);
60 IOUtils
.closeQuietly(fis
);
65 public InputStream
getInputStreamFromFileId(String fileId
) {
67 InputStream fis
= null;
69 Node child
= getFileNodeFromId(fileId
);
70 fis
= (InputStream
) child
.getProperty(Property
.JCR_DATA
)
71 .getBinary().getStream();
73 } catch (RepositoryException re
) {
74 throw new EclipseUiException("Cannot get stream from file node for Id "
82 * @return Returns the child node of the nt:file node. It is the child node
83 * that have the jcr:data property where actual file is stored.
86 private Node
getFileNodeFromId(String fileId
) {
89 result
= session
.getNode(fileId
);
93 throw new EclipseUiException("File node not found for ID" + fileId
);
95 // Ensure that the node have the correct type.
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.");
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, "
106 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
108 } catch (RepositoryException re
) {
109 throw new EclipseUiException("Erreur while getting file node of ID "