- Object[] nodes = repositoryNode.getChildren();
-
- repos: for (int i = 0; i < nodes.length; i++) {
- WorkspaceNode wNode = (WorkspaceNode) nodes[i];
- Node node = null;
- node = wNode.getSession().getNodeByIdentifier(fileId);
-
- if (node == null)
- continue repos;
-
- if (!node.isNodeType("nt:file"))
- throw new ArgeoException(
- "Cannot open file children Node that are not of 'nt:resource' type.");
-
- Node child = node.getNodes().nextNode();
- if (!child.isNodeType("nt:resource"))
- throw new ArgeoException(
- "Cannot open file children Node that are not of 'nt:resource' type.");
-
- InputStream fis = null;
- byte[] ba = null;
- try {
- fis = (InputStream) child.getProperty("jcr:data")
- .getBinary().getStream();
- ba = IOUtils.toByteArray(fis);
-
- } catch (Exception e) {
- throw new ArgeoException("Stream error while opening file",
- e);
- } finally {
- IOUtils.closeQuietly(fis);
+ InputStream fis = null;
+
+ Node child = getFileNodeFromId(fileId);
+ fis = (InputStream) child.getProperty(Property.JCR_DATA)
+ .getBinary().getStream();
+ return fis;
+ } catch (RepositoryException re) {
+ throw new ArgeoException("Cannot get stream from file node for Id "
+ + fileId, re);
+ }
+ }
+
+ /**
+ * Throws an exception if the node is not found in the current repository (a
+ * bit like a FileNotFoundException)
+ *
+ * @param fileId
+ * @return Returns the child node of the nt:file node. It is the child node
+ * that have the jcr:data property where actual file is stored.
+ * never null
+ */
+ private Node getFileNodeFromId(String fileId) {
+ try {
+ Node result = null;
+
+ rootNodes: for (int j = 0; j < rootNodes.length; j++) {
+ // in case we have a classic JCR Node
+ if (rootNodes[j] instanceof Node) {
+ Node curNode = (Node) rootNodes[j];
+ result = curNode.getSession().getNodeByIdentifier(fileId);
+ if (result != null)
+ break rootNodes;
+ } // Case of a repository Node
+ else if (rootNodes[j] instanceof RepositoryNode) {
+ Object[] nodes = ((RepositoryNode) rootNodes[j])
+ .getChildren();
+ for (int i = 0; i < nodes.length; i++) {
+ Node node = (Node) nodes[i];
+ result = node.getSession().getNodeByIdentifier(fileId);
+ if (result != null)
+ break rootNodes;
+ }