]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/jcr/utils/JcrFileProvider.java
b2e6455c880bdf92211415bafe76c1b1661c5fcc
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / jcr / utils / JcrFileProvider.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.nodetype.NodeType;
24
25 import org.apache.commons.io.IOUtils;
26 import org.argeo.ArgeoException;
27 import org.argeo.eclipse.ui.FileProvider;
28
29 /**
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.
33 *
34 * NOTE: id used here is the real id of the JCR Node, not the JCR Path
35 *
36 * Relies on common approach for JCR file handling implementation.
37 *
38 */
39
40 public class JcrFileProvider implements FileProvider {
41
42 // private Object[] rootNodes;
43 private Node refNode;
44
45 /**
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
48 * file ID
49 *
50 * FIXME : this introduces some concurrences ISSUES.
51 *
52 * @param repositoryNode
53 */
54 public void setReferenceNode(Node refNode) {
55 this.refNode = refNode;
56 }
57
58 /**
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
61 * time being.
62 *
63 * @param repositoryNode
64 */
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) {
70 // tmpNodes.add(obj);
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
77 // // .get(name)));
78 // }
79 //
80 // }
81 // }
82 // this.rootNodes = tmpNodes.toArray();
83 // }
84
85 public byte[] getByteArrayFileFromId(String fileId) {
86 InputStream fis = null;
87 byte[] ba = null;
88 Node child = getFileNodeFromId(fileId);
89 try {
90 fis = (InputStream) child.getProperty(Property.JCR_DATA)
91 .getBinary().getStream();
92 ba = IOUtils.toByteArray(fis);
93
94 } catch (Exception e) {
95 throw new ArgeoException("Stream error while opening file", e);
96 } finally {
97 IOUtils.closeQuietly(fis);
98 }
99 return ba;
100 }
101
102 public InputStream getInputStreamFromFileId(String fileId) {
103 try {
104 InputStream fis = null;
105
106 Node child = getFileNodeFromId(fileId);
107 fis = (InputStream) child.getProperty(Property.JCR_DATA)
108 .getBinary().getStream();
109 return fis;
110 } catch (RepositoryException re) {
111 throw new ArgeoException("Cannot get stream from file node for Id "
112 + fileId, re);
113 }
114 }
115
116 /**
117 * Throws an exception if the node is not found in the current repository (a
118 * bit like a FileNotFoundException)
119 *
120 * @param fileId
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.
123 * never null
124 */
125 private Node getFileNodeFromId(String fileId) {
126 try {
127 Node result = refNode.getSession().getNodeByIdentifier(fileId);
128
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)
134 // break rootNodes;
135 // } // Case of a repository Node
136 // else if (rootNodes[j] instanceof RepositoryNode) {
137 // Object[] nodes = ((RepositoryNode) rootNodes[j])
138 // .getChildren();
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)
143 // break rootNodes;
144 // }
145 // }
146 // }
147
148 // Sanity checks
149 if (result == null)
150 throw new ArgeoException("File node not found for ID" + fileId);
151
152 Node child = null;
153
154 boolean isValid = true;
155 if (!result.isNodeType(NodeType.NT_FILE))
156 // useless: mandatory child node
157 // || !result.hasNode(Property.JCR_CONTENT))
158 isValid = false;
159 else {
160 child = result.getNode(Property.JCR_CONTENT);
161 if (!(child.isNodeType(NodeType.NT_RESOURCE) || child
162 .hasProperty(Property.JCR_DATA)))
163 isValid = false;
164 }
165
166 if (!isValid)
167 throw new ArgeoException(
168 "ERROR: In the current implemented model, '"
169 + NodeType.NT_FILE
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");
173 return child;
174
175 } catch (RepositoryException re) {
176 throw new ArgeoException("Erreur while getting file node of ID "
177 + fileId, re);
178 }
179 }
180 }