]> git.argeo.org Git - lgpl/argeo-commons.git/blob - utils/SingleSessionFileProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / utils / SingleSessionFileProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.jcr.utils;
17
18 import java.io.InputStream;
19
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;
25
26 import org.apache.commons.io.IOUtils;
27 import org.argeo.eclipse.ui.EclipseUiException;
28 import org.argeo.eclipse.ui.FileProvider;
29
30 /**
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.
34 *
35 * Note that considered id is the JCR path
36 *
37 * Relies on common approach for JCR file handling implementation.
38 */
39 @SuppressWarnings("deprecation")
40 public class SingleSessionFileProvider implements FileProvider {
41
42 private Session session;
43
44 public SingleSessionFileProvider(Session session) {
45 this.session = session;
46 }
47
48 public byte[] getByteArrayFileFromId(String fileId) {
49 InputStream fis = null;
50 byte[] ba = null;
51 Node child = getFileNodeFromId(fileId);
52 try {
53 fis = (InputStream) child.getProperty(Property.JCR_DATA)
54 .getBinary().getStream();
55 ba = IOUtils.toByteArray(fis);
56
57 } catch (Exception e) {
58 throw new EclipseUiException("Stream error while opening file", e);
59 } finally {
60 IOUtils.closeQuietly(fis);
61 }
62 return ba;
63 }
64
65 public InputStream getInputStreamFromFileId(String fileId) {
66 try {
67 InputStream fis = null;
68
69 Node child = getFileNodeFromId(fileId);
70 fis = (InputStream) child.getProperty(Property.JCR_DATA)
71 .getBinary().getStream();
72 return fis;
73 } catch (RepositoryException re) {
74 throw new EclipseUiException("Cannot get stream from file node for Id "
75 + fileId, re);
76 }
77 }
78
79 /**
80 *
81 * @param fileId
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.
84 * never null
85 */
86 private Node getFileNodeFromId(String fileId) {
87 try {
88 Node result = null;
89 result = session.getNode(fileId);
90
91 // Sanity checks
92 if (result == null)
93 throw new EclipseUiException("File node not found for ID" + fileId);
94
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.");
100
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, "
105 + NodeType.NT_FILE
106 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
107 return child;
108 } catch (RepositoryException re) {
109 throw new EclipseUiException("Erreur while getting file node of ID "
110 + fileId, re);
111 }
112 }
113 }