You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/01/29 19:35:35 UTC
svn commit: r616471 - in /incubator/sling/trunk/jcr/resource/src:
main/java/org/apache/sling/jcr/resource/internal/helper/jcr/
test/java/org/apache/sling/jcr/resource/internal/helper/jcr/
Author: fmeschbe
Date: Tue Jan 29 10:35:33 2008
New Revision: 616471
URL: http://svn.apache.org/viewvc?rev=616471&view=rev
Log:
SLING-211 Generalize support for file and resource nodes
Added:
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java?rev=616471&r1=616470&r2=616471&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java Tue Jan 29 10:35:33 2008
@@ -35,7 +35,9 @@
import java.util.Iterator;
import javax.jcr.Item;
+import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
+import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.net.URLFactory;
@@ -54,9 +56,6 @@
/** default log */
private final Logger log = LoggerFactory.getLogger(getClass());
- /** The relative path name of the data property of an nt:file node */
- private static final String FILE_DATA_PROP = JCR_CONTENT + "/" + JCR_DATA;
-
private final ResourceProvider resourceProvider;
private final Node node;
@@ -67,18 +66,6 @@
private final ResourceMetadata metadata;
-// JcrNodeResource(JcrResourceProvider resourceProvider, String path)
-// throws RepositoryException {
-// this.resourceProvider = resourceProvider;
-// node = (Node) resourceProvider.getSession().getItem(path);
-// this.path = node.getPath();
-// metadata = new ResourceMetadata();
-// resourceType = getResourceTypeForNode(node);
-//
-// // check for nt:file metadata
-// setMetaData(node, metadata);
-// }
-
JcrNodeResource(ResourceProvider resourceProvider, Node node)
throws RepositoryException {
this.resourceProvider = resourceProvider;
@@ -141,10 +128,28 @@
// implement this for nt:file only
if (node != null) {
try {
- if (node.isNodeType(NT_FILE)
- && node.hasProperty(FILE_DATA_PROP)) {
- return node.getProperty(FILE_DATA_PROP).getStream();
+ // find the content node: for nt:file it is jcr:content
+ // otherwise it is the node of this resource
+ Node content = node.isNodeType(NT_FILE) ? node.getNode(JCR_CONTENT) : node;
+
+ // if the node has a jcr:data property, use that property
+ if (content.hasProperty(JCR_DATA)) {
+ return content.getProperty(JCR_DATA).getStream();
+ }
+
+ // otherwise try to follow default item trail
+ try {
+ Item item = content.getPrimaryItem();
+ while (item.isNode()) {
+ item = ((Node) item).getPrimaryItem();
+ }
+ return ((Property) item).getStream();
+ } catch (ItemNotFoundException infe) {
+ // we don't actually care, but log for completeness
+ log.debug("getInputStream: No primary items for "
+ + toString(), infe);
}
+
} catch (RepositoryException re) {
log.error("getInputStream: Cannot get InputStream for " + this,
re);
@@ -211,32 +216,38 @@
return result;
}
- private static void setMetaData(Node node, ResourceMetadata metadata) {
+ private void setMetaData(Node node, ResourceMetadata metadata) {
try {
+
+ // check stuff for nt:file nodes
if (node.isNodeType(NT_FILE)) {
metadata.put(CREATION_TIME,
node.getProperty(JCR_CREATED).getLong());
- if (node.hasNode(JCR_CONTENT)) {
- Node content = node.getNode(JCR_CONTENT);
- if (content.hasProperty(JCR_MIMETYPE)) {
- metadata.put(CONTENT_TYPE, content.getProperty(
- JCR_MIMETYPE).getString());
- }
+ // continue our stuff with the jcr:content node
+ // which might be nt:resource, which we support below
+ node = node.getNode(JCR_CONTENT);
+ }
+
+ // check stuff for nt:resource (or similar) nodes
+ if (node.hasProperty(JCR_MIMETYPE)) {
+ metadata.put(CONTENT_TYPE,
+ node.getProperty(JCR_MIMETYPE).getString());
+ }
- if (content.hasProperty(JCR_ENCODING)) {
- metadata.put(CHARACTER_ENCODING, content.getProperty(
- JCR_ENCODING).getString());
- }
+ if (node.hasProperty(JCR_ENCODING)) {
+ metadata.put(CHARACTER_ENCODING,
+ node.getProperty(JCR_ENCODING).getString());
+ }
- if (content.hasProperty(JCR_LASTMODIFIED)) {
- metadata.put(MODIFICATION_TIME, content.getProperty(
- JCR_LASTMODIFIED).getLong());
- }
- }
+ if (node.hasProperty(JCR_LASTMODIFIED)) {
+ metadata.put(MODIFICATION_TIME, node.getProperty(
+ JCR_LASTMODIFIED).getLong());
}
} catch (RepositoryException re) {
- // TODO: should log
+ log.info(
+ "setMetaData: Problem extracting metadata information for "
+ + getPath(), re);
}
}
Added: incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java?rev=616471&view=auto
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java (added)
+++ incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java Tue Jan 29 10:35:33 2008
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper.jcr;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
+import org.apache.sling.jcr.resource.JcrResourceConstants;
+
+public class JcrNodeResourceTest extends RepositoryTestBase {
+
+ private static final long TEST_MODIFIED = System.currentTimeMillis();
+
+ private static final String TEST_TYPE = "text/gurk";
+
+ private static final String TEST_ENCODING = "ISO-8859-1";
+
+ private static final byte[] TEST_DATA = { 'S', 'o', 'm', 'e', ' ', 'T',
+ 'e', 's', 't' };
+
+ private String rootPath;
+
+ private Node rootNode;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ getSession();
+
+ try {
+ NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
+ nsr.registerNamespace(SlingConstants.NAMESPACE_PREFIX,
+ JcrResourceConstants.SLING_NAMESPACE_URI);
+ } catch (Exception e) {
+ // don't care for now
+ }
+
+ rootPath = "/test" + System.currentTimeMillis();
+ rootNode = getSession().getRootNode().addNode(rootPath.substring(1),
+ "nt:unstructured");
+ getSession().save();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ if (rootNode != null) {
+ rootNode.remove();
+ getSession().save();
+ }
+ }
+
+ public void testNtFileNtResource() throws Exception {
+
+ String name = "file";
+ Node file = rootNode.addNode(name, JcrConstants.NT_FILE);
+ Node res = file.addNode(JcrConstants.JCR_CONTENT,
+ JcrConstants.NT_RESOURCE);
+ setupResource(res);
+ getSession().save();
+
+ file = rootNode.getNode(name);
+ JcrNodeResource jnr = new JcrNodeResource(null, file);
+
+ assertEquals(file.getPath(), jnr.getPath());
+
+ assertResourceMetaData(jnr.getResourceMetadata());
+ assertEquals(TEST_DATA, jnr.adaptTo(InputStream.class));
+ }
+
+ public void testNtFileNtUnstructured() throws Exception {
+
+ String name = "fileunstructured";
+ Node file = rootNode.addNode(name, JcrConstants.NT_FILE);
+ Node res = file.addNode(JcrConstants.JCR_CONTENT,
+ JcrConstants.NT_UNSTRUCTURED);
+ setupResource(res);
+ getSession().save();
+
+ file = rootNode.getNode(name);
+ JcrNodeResource jnr = new JcrNodeResource(null, file);
+
+ assertEquals(file.getPath(), jnr.getPath());
+
+ assertResourceMetaData(jnr.getResourceMetadata());
+ assertEquals(TEST_DATA, jnr.adaptTo(InputStream.class));
+ }
+
+ public void testNtResource() throws Exception {
+
+ String name = "resource";
+ Node res = rootNode.addNode(name, JcrConstants.NT_RESOURCE);
+ setupResource(res);
+ getSession().save();
+
+ res = rootNode.getNode(name);
+ JcrNodeResource jnr = new JcrNodeResource(null, res);
+
+ assertEquals(res.getPath(), jnr.getPath());
+
+ assertResourceMetaData(jnr.getResourceMetadata());
+ assertEquals(TEST_DATA, jnr.adaptTo(InputStream.class));
+ }
+
+ public void testNtUnstructured() throws Exception {
+
+ String name = "unstructured";
+ Node res = rootNode.addNode(name, JcrConstants.NT_UNSTRUCTURED);
+ setupResource(res);
+ getSession().save();
+
+ res = rootNode.getNode(name);
+ JcrNodeResource jnr = new JcrNodeResource(null, res);
+
+ assertEquals(res.getPath(), jnr.getPath());
+
+ assertResourceMetaData(jnr.getResourceMetadata());
+ assertEquals(TEST_DATA, jnr.adaptTo(InputStream.class));
+ }
+
+ private void setupResource(Node res) throws RepositoryException {
+ res.setProperty(JcrConstants.JCR_LASTMODIFIED, TEST_MODIFIED);
+ res.setProperty(JcrConstants.JCR_MIMETYPE, TEST_TYPE);
+ res.setProperty(JcrConstants.JCR_ENCODING, TEST_ENCODING);
+ res.setProperty(JcrConstants.JCR_DATA, new ByteArrayInputStream(
+ TEST_DATA));
+ }
+
+ private void assertResourceMetaData(ResourceMetadata rm) {
+ assertNotNull(rm);
+
+ assertEquals(new Long(TEST_MODIFIED),
+ rm.get(ResourceMetadata.MODIFICATION_TIME));
+ assertEquals(TEST_TYPE, rm.get(ResourceMetadata.CONTENT_TYPE));
+ assertEquals(TEST_ENCODING, rm.get(ResourceMetadata.CHARACTER_ENCODING));
+ }
+
+ private void assertEquals(byte[] expected, InputStream actual)
+ throws IOException {
+ if (actual == null) {
+ fail("Rsource stream is null");
+ } else {
+ try {
+ for (byte b : expected) {
+ assertEquals(b, actual.read());
+ }
+ } finally {
+ try {
+ actual.close();
+ } catch (IOException grok) {
+ }
+ }
+ }
+ }
+}