incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r590115 - in /incubator/sling/whiteboard/microsling/src: main/java/org/apache/sling/microsling/slingservlets/ test/java/org/apache/sling/microsling/integration/ test/resources/integration-test/
Date Tue, 30 Oct 2007 15:19:14 GMT
Author: bdelacretaz
Date: Tue Oct 30 08:19:13 2007
New Revision: 590115

URL: http://svn.apache.org/viewvc?rev=590115&view=rev
Log:
SLING-70, added workaround in StreamServlet for missing primary item path, and added StreamServletTest
to test that

Added:
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
  (with props)
    incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
    incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.txt

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java?rev=590115&r1=590114&r2=590115&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
Tue Oct 30 08:19:13 2007
@@ -30,7 +30,7 @@
 
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.microsling.api.SlingRequestContext;
-import org.apache.sling.microsling.api.exceptions.SlingException;
+import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
 import org.apache.sling.microsling.helpers.constants.HttpConstants;
 import org.apache.sling.microsling.helpers.servlets.SlingSafeMethodsServlet;
 
@@ -99,13 +99,10 @@
             // just spool, the property to which the item resolves through
             // the primary item trail
             // the item is a property, spool and forget
-            spool(response, findDefaultProperty(item), null, null, -1);
-
-        } catch (ItemNotFoundException infe) {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            spool(response, findDataProperty(item), null, null, -1);
 
         } catch (RepositoryException re) {
-            throw new SlingException(re.getMessage(), re);
+            throw new HttpStatusCodeException(500, "RepositoryException in StreamServlet.doGet():
" + re.getMessage());
         }
     }
 
@@ -161,14 +158,38 @@
         }
     }
 
-    /** Follow the primary item trail of item until a property is found */
-    Property findDefaultProperty(Item item) throws ItemNotFoundException,
-            RepositoryException {
-        while (item.isNode()) {
-            // will throw if there is none defined or existsing
-            item = ((Node) item).getPrimaryItem();
+    /** Find the Property that contains the data to spool, under parent */ 
+    private Property findDataProperty(final Item parent) throws RepositoryException, HttpStatusCodeException
{
+        Property result = null;
+        
+        // Following the path of primary items until we find a property
+        // should provide us with the file data of the parent
+        try {
+            Item item = parent;
+            while(item!=null && item.isNode()) {
+                item = ((Node) item).getPrimaryItem();
+            }
+            result = (Property)item;
+        } catch(ItemNotFoundException ignored) {
+            // TODO: for now we use an alternate method if this fails,
+            // there might be a better way (see jackrabbit WebDAV server code?)
+        }
+        
+        if(result==null && parent.isNode()) {
+            // primary path didn't work, try the "usual" path to the data Property
+            try {
+                final Node parentNode = (Node)parent;
+                result = parentNode.getNode("jcr:content").getProperty("jcr:data");
+            } catch(ItemNotFoundException e) {
+                throw new HttpStatusCodeException(404,parent.getPath() + "/jcr:content" +
"/jcr:data");
+            }
+        }
+        
+        if(result==null) {
+            throw new HttpStatusCodeException(500, "Unable to find data property for parent
item " + parent.getPath());
         }
-        return (Property) item;
+        
+        return result;
     }
 
     /** return the jcr:lastModified property value or null if property is missing */

Modified: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java?rev=590115&r1=590114&r2=590115&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
(original)
+++ incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
Tue Oct 30 08:19:13 2007
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.GetMethod;
 
@@ -52,10 +53,17 @@
         final GetMethod get = new GetMethod(url);
         final int status = httpClient.executeMethod(get);
         assertEquals("Expected status 200 for " + url,200,status);
-        assertTrue(
-            "Expected Content-Type '" + expectedContentType + "' for " + url,
-            get.getResponseHeader("Content-Type").getValue().startsWith(expectedContentType)
-        );
+        final Header h = get.getResponseHeader("Content-Type");
+        if(expectedContentType == null) {
+            if(h!=null) {
+                fail("Expected null Content-Type, got " + h.getValue());
+            }
+        } else {
+            assertTrue(
+                "Expected Content-Type '" + expectedContentType + "' for " + url,
+                h.getValue().startsWith(expectedContentType)
+            );
+        }
         return get.getResponseBodyAsString();
     }
     

Added: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java?rev=590115&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
(added)
+++ incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
Tue Oct 30 08:19:13 2007
@@ -0,0 +1,64 @@
+/*
+ * 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.microsling.integration;
+
+
+/** Test the StreamServlet by reading an uploaded file with a GET
+ */
+public class StreamServletTest extends RenderingTestBase {
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        scriptPath = TEST_PATH + "/StreamServletTest." + System.currentTimeMillis();
+        displayUrl = HTTP_BASE_URL + scriptPath;
+        testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
+    }
+
+    public void testPlainTextFile() throws Exception {
+        final String filename = "testfile.txt";
+        final String toDelete = uploadTestScript(filename,filename);
+        try {
+            final String url = displayUrl + "/" + filename;
+            // TODO why don't we get a content-type here?
+            final String content = getContent(url, null);
+            assertTrue(
+                    "Content at " + url + " must include expected marker, got " + content,
+                    content.contains("This is just some text in an ASCII file.")
+            );
+        } finally {
+            // TODO testClient.delete(toDelete);
+        }
+    }
+
+    public void testHtmlTextFile() throws Exception {
+        final String filename = "testfile.html";
+        final String toDelete = uploadTestScript(filename,filename);
+        try {
+            // TODO this should really be text/html, not sure why it is not
+            final String url = displayUrl + "/" + filename;
+            // TODO why don't we get a content-type here?
+            final String content = getContent(url, null);
+            assertTrue(
+                    "Content at " + url + " must include expected marker, got " + content,
+                    content.contains("This is <em>testfile.html</em>.")
+            );
+        } finally {
+            // TODO testClient.delete(toDelete);
+        }
+    }
+}

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/integration/StreamServletTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html?rev=590115&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html
(added)
+++ incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html
Tue Oct 30 08:19:13 2007
@@ -0,0 +1,5 @@
+<html>
+<body>
+This is <em>testfile.html</em>.
+</body>
+</html>
\ No newline at end of file

Propchange: incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.html
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.txt
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.txt?rev=590115&r1=590114&r2=590115&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.txt
(original)
+++ incubator/sling/whiteboard/microsling/src/test/resources/integration-test/testfile.txt
Tue Oct 30 08:19:13 2007
@@ -1 +1 @@
-This is hust some text in an ASCII file.
\ No newline at end of file
+This is just some text in an ASCII file.
\ No newline at end of file



Mime
View raw message