sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r616222 - in /incubator/sling/trunk/launchpad: launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/ launchpad-webapp/src/main/webapp/WEB-INF/ launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/...
Date Tue, 29 Jan 2008 09:10:15 GMT
Author: bdelacretaz
Date: Tue Jan 29 01:10:14 2008
New Revision: 616222

URL: http://svn.apache.org/viewvc?rev=616222&view=rev
Log:
SLING-149 - serving static files works, corresponding tests activated

Added:
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js
  (with props)
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.json
Modified:
    incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/LaunchpadDefaultServlet.java
    incubator/sling/trunk/launchpad/launchpad-webapp/src/main/webapp/WEB-INF/web.xml
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StaticContentTest.java
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StreamServletTest.java
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.html

Modified: incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/LaunchpadDefaultServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/LaunchpadDefaultServlet.java?rev=616222&r1=616221&r2=616222&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/LaunchpadDefaultServlet.java
(original)
+++ incubator/sling/trunk/launchpad/launchpad-servlets/src/main/java/org/apache/sling/launchpad/servlets/LaunchpadDefaultServlet.java
Tue Jan 29 01:10:14 2008
@@ -19,6 +19,8 @@
 package org.apache.sling.launchpad.servlets;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -31,6 +33,7 @@
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.launchpad.renderers.DefaultHtmlRendererServlet;
 import org.apache.sling.launchpad.renderers.JsonRendererServlet;
@@ -105,27 +108,77 @@
             return;
         }
         
-        // use default renderer servlet if no extension, else lookup our getServlets 
+        // render using a servlet or binary streaming
         Servlet s = defaultGetServlet;
+        InputStream stream = null;
         final String ext = request.getRequestPathInfo().getExtension();
         if(ext!=null && ext.length() > 0) {
-            s = getServlets.get(ext);
+            // if there is an extension, lookup our getServlets 
+            s = getServlets.get(ext); 
+        } else {
+            // no extension means we're addressing a static file directly
+            // check whether the resource adapts to a stream, spool then
+            stream = resource.adaptTo(InputStream.class);
         }
 
-        // render using s, or fail
-        if(s==null) {
+        // render using stream, s, or fail
+        if(stream != null) {
+            stream(response, resource, stream);
+        } else if(s!=null) {
+            s.service(request, response);
+        } else {
             response.sendError(
                     HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                     "No default renderer found for extension='" + ext + "'"
             );
-        } else {
-            s.service(request, response);
         }
     }
 
-
     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

     throws ServletException,IOException {
         postServlet.service(request, response);
+    }
+    
+    /** Stream the Resource to response */
+    private void stream(HttpServletResponse response, Resource resource,
+            InputStream stream) throws IOException {
+        
+        ResourceMetadata meta = resource.getResourceMetadata();
+        
+        final String defaultContentType = "application/octet-stream";
+        String contentType = (String) meta.get(ResourceMetadata.CONTENT_TYPE);
+        if (contentType == null || defaultContentType.equals(contentType)) {
+            // if repository doesn't provide a content-type, or provides the default one,
+            // try to do better using our servlet context
+            final String ct = getServletContext().getMimeType(resource.getPath());
+            if(ct!=null) {
+                contentType = ct;
+            }
+        }
+        if (contentType != null) {
+            response.setContentType(contentType);
+        }
+        
+        String encoding = (String) meta.get(ResourceMetadata.CHARACTER_ENCODING);
+        if (encoding != null) {
+            response.setCharacterEncoding(encoding);
+        }
+        
+        try {
+            OutputStream out = response.getOutputStream();
+            
+            byte[] buf = new byte[1024];
+            int rd;
+            while ( (rd=stream.read(buf)) >= 0) {
+                out.write(buf, 0, rd);
+            }
+            
+        } finally {
+            try {
+                stream.close();
+            } catch (IOException ignore) {
+                // don't care
+            }
+        }
     }
 }

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/main/webapp/WEB-INF/web.xml?rev=616222&r1=616221&r2=616222&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/main/webapp/WEB-INF/web.xml (original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/main/webapp/WEB-INF/web.xml Tue Jan
29 01:10:14 2008
@@ -29,20 +29,6 @@
         <servlet-class>
             org.apache.sling.launcher.webapp.SlingServlet
         </servlet-class>
-
-        <!-- TODO com.day stuff??
-        <init-param>
-            <param-name>java.naming.provider.url</param-name>
-            <param-value>http://jcr.day.com</param-value>
-        </init-param>
-        <init-param>
-            <param-name>java.naming.factory.initial</param-name>
-            <param-value>
-                com.day.util.jndi.provider.MemoryInitialContextFactory
-            </param-value>
-        </init-param>
-         -->
-
         <load-on-startup>100</load-on-startup>
     </servlet>
 
@@ -57,5 +43,125 @@
         <servlet-name>sling</servlet-name>
         <url-pattern>*.jsp</url-pattern>
     </servlet-mapping>
+    
+      <!-- ====================================================================== -->
+  <!-- M I M E T Y P E   M A P P I N G                                          -->
+  <!-- ====================================================================== -->
+  <mime-mapping>
+    <extension>doc</extension>
+    <mime-type>application/msword</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>bin</extension>
+    <mime-type>application/octet-stream</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>pdf</extension>
+    <mime-type>application/pdf</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>ai</extension>
+    <mime-type>application/postscript</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>eps</extension>
+    <mime-type>application/postscript</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>ps</extension>
+    <mime-type>application/postscript</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>rtf</extension>
+    <mime-type>application/rtf</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>mif</extension>
+    <mime-type>application/vnd.mif</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>ppt</extension>
+    <mime-type>application/vnd.ms-powerpoint</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>xls</extension>
+    <mime-type>application/vnd.ms-excel</mime-type>
+  </mime-mapping>  
+  <mime-mapping>
+    <extension>vcd</extension>
+    <mime-type>application/x-cdlink</mime-type>
+  </mime-mapping>
+    <mime-mapping>
+    <extension>js</extension>
+    <mime-type>application/x-javascript</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>mp3</extension>
+    <mime-type>audio/mpeg</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>ram</extension>
+    <mime-type>audio/x-pn-realaudio</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>rm</extension>
+    <mime-type>audio/x-pn-realaudio</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>ra</extension>
+    <mime-type>audio/x-realaudio</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>gif</extension>
+    <mime-type>image/gif</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>jpeg</extension>
+    <mime-type>image/jpeg</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>jpg</extension>
+    <mime-type>image/jpeg</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>png</extension>
+    <mime-type>image/png</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>tiff</extension>
+    <mime-type>image/tiff</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>tif</extension>
+    <mime-type>image/tiff</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>css</extension>
+    <mime-type>text/css</mime-type>
+  </mime-mapping>
+    <mime-mapping>
+    <extension>asc</extension>
+    <mime-type>text/plain</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>txt</extension>
+    <mime-type>text/plain</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>xml</extension>
+    <mime-type>text/xml</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>html</extension>
+    <mime-type>text/html</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>htm</extension>
+    <mime-type>text/html</mime-type>
+  </mime-mapping>
+  <mime-mapping>
+    <extension>json</extension>
+    <mime-type>application/json</mime-type>
+  </mime-mapping>
 
 </web-app>

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StaticContentTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StaticContentTest.java?rev=616222&r1=616221&r2=616222&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StaticContentTest.java
(original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StaticContentTest.java
Tue Jan 29 01:10:14 2008
@@ -20,19 +20,8 @@
 
 import javax.servlet.http.HttpServletResponse;
 
-/** Test static content served from the microsling webapp resources */
+/** Some tests about static content */
 public class StaticContentTest extends HttpTestBase {
-    public void TODO_FAILS_testContentTypeHtml() throws IOException {
-        getContent(HTTP_BASE_URL + "/index.html", CONTENT_TYPE_HTML);
-    }
-    
-    public void TODO_FAILS_testContentTypeCss() throws IOException {
-        getContent(HTTP_BASE_URL + "/launchpad.css", CONTENT_TYPE_CSS);
-    }
-    
-    public void TODO_FAILS_testContentTypeJs() throws IOException {
-        getContent(HTTP_BASE_URL + "/ujax/ujax.js", CONTENT_TYPE_JS);
-    }
     
     public void testWebInfForbidden() throws IOException {
         // if DefaultSlingServlet handled this we'd get an SC_FORBIDDEN, but it looks

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StreamServletTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StreamServletTest.java?rev=616222&r1=616221&r2=616222&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StreamServletTest.java
(original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/StreamServletTest.java
Tue Jan 29 01:10:14 2008
@@ -17,8 +17,7 @@
 package org.apache.sling.launchpad.webapp.integrationtest;
 
 
-/** Test the StreamServlet by reading an uploaded file with a GET
- */
+/** Test the streaming of static files uploaded to the repository */
 public class StreamServletTest extends RenderingTestBase {
     
     @Override
@@ -29,42 +28,30 @@
         testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
     }
     
-    public void testNothing() {
-        // TODO remove this all TODO_FAILS_ are gone 
-    }
-
-    /** TODO fails as files are currently served by the default renderer, as plain text */
-    public void TODO_FAILS_testPlainTextFile() throws Exception {
-        final String filename = "testfile.txt";
+    private void runTest(String filename, String expectedContentType, String expectedContent)
throws Exception {
         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.")
-            );
+            final String content = getContent(url, expectedContentType);
+            assertEquals(expectedContent, content);
         } finally {
             testClient.delete(toDelete);
         }
     }
+    
+    public void testPlainTextFile() throws Exception {
+        runTest("testfile.txt", CONTENT_TYPE_PLAIN, "This is just some text in an ASCII file.");
+    }
 
-    /** TODO fails as files are currently served by the default renderer, as plain text */
-    public void TODO_FAILS_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 {
-            testClient.delete(toDelete);
-        }
+    public void testHtmlFile() throws Exception {
+        runTest("testfile.html", CONTENT_TYPE_HTML, "This is <em>testfile.html</em>");
+    }
+    
+    public void testJavascriptFile() throws Exception {
+        runTest("testfile.js", CONTENT_TYPE_JS, "// This is testfile.js");
+    }
+    
+    public void testJsonFile() throws Exception {
+        runTest("testfile.json", CONTENT_TYPE_JSON, "This is testfile.json");
     }
 }

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.html
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.html?rev=616222&r1=616221&r2=616222&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.html
(original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.html
Tue Jan 29 01:10:14 2008
@@ -1,5 +1 @@
-<html>
-<body>
-This is <em>testfile.html</em>.
-</body>
-</html>
\ No newline at end of file
+This is <em>testfile.html</em>
\ No newline at end of file

Added: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js?rev=616222&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js
(added)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js
Tue Jan 29 01:10:14 2008
@@ -0,0 +1 @@
+// This is testfile.js
\ No newline at end of file

Propchange: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.js
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.json
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.json?rev=616222&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.json
(added)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/testfile.json
Tue Jan 29 01:10:14 2008
@@ -0,0 +1 @@
+This is testfile.json
\ No newline at end of file



Mime
View raw message