]> git.argeo.org Git - lgpl/argeo-commons.git/blob - SingleSessionFileProvider.java
57bdf246ec6e244b212c92a2cf9f739a891a6184
[lgpl/argeo-commons.git] / 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.ArgeoException;
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 * @author bsinou
40 *
41 */
42 @SuppressWarnings("deprecation")
43 public class SingleSessionFileProvider implements FileProvider {
44
45 private Session session;
46
47 public SingleSessionFileProvider(Session session) {
48 this.session = session;
49 }
50
51 public byte[] getByteArrayFileFromId(String fileId) {
52 InputStream fis = null;
53 byte[] ba = null;
54 Node child = getFileNodeFromId(fileId);
55 try {
56 fis = (InputStream) child.getProperty(Property.JCR_DATA)
57 .getBinary().getStream();
58 ba = IOUtils.toByteArray(fis);
59
60 } catch (Exception e) {
61 throw new ArgeoException("Stream error while opening file", e);
62 } finally {
63 IOUtils.closeQuietly(fis);
64 }
65 return ba;
66 }
67
68 public InputStream getInputStreamFromFileId(String fileId) {
69 try {
70 InputStream fis = null;
71
72 Node child = getFileNodeFromId(fileId);
73 fis = (InputStream) child.getProperty(Property.JCR_DATA)
74 .getBinary().getStream();
75 return fis;
76 } catch (RepositoryException re) {
77 throw new ArgeoException("Cannot get stream from file node for Id "
78 + fileId, re);
79 }
80 }
81
82 /**
83 *
84 * @param fileId
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.
87 * never null
88 */
89 private Node getFileNodeFromId(String fileId) {
90 try {
91 Node result = null;
92 result = session.getNode(fileId);
93
94 // Sanity checks
95 if (result == null)
96 throw new ArgeoException("File node not found for ID" + fileId);
97
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.");
103
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, "
108 + NodeType.NT_FILE
109 + " file node must have one and only one child of the nt:ressource, where actual data is stored");
110 return child;
111 } catch (RepositoryException re) {
112 throw new ArgeoException("Erreur while getting file node of ID "
113 + fileId, re);
114 }
115 }
116 }