incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r591038 - in /incubator/sling/trunk/microsling/microsling-core: ./ src/main/java/org/apache/sling/microsling/slingservlets/ src/main/java/org/apache/sling/microsling/slingservlets/renderers/ src/test/java/org/apache/sling/microsling/integra...
Date Thu, 01 Nov 2007 14:59:25 GMT
Author: bdelacretaz
Date: Thu Nov  1 07:59:23 2007
New Revision: 591038

URL: http://svn.apache.org/viewvc?rev=591038&view=rev
Log:
SLING-92 - DefaultSlingServlet uses distinct rendering Servlets, preparing for JSON rendering

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
  (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/   (props changed)
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/CreateNodeTest.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java

Propchange: incubator/sling/trunk/microsling/microsling-core/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Thu Nov  1 07:59:23 2007
@@ -3,5 +3,6 @@
 .settings
 target
 *.log
+velocity.log.*
 jackrabbit-repository
 logs

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=591038&r1=591037&r2=591038&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Thu Nov  1 07:59:23 2007
@@ -19,18 +19,17 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.PrintWriter;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
-import javax.jcr.Property;
-import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
-import javax.jcr.Value;
+import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -42,17 +41,27 @@
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.microsling.slingservlets.renderers.DefaultHtmlRendererServlet;
+import org.apache.sling.microsling.slingservlets.renderers.PlainTextRendererServlet;
 
 /**
  * The default SlingServlet, used if no other SlingServlet wants to process the
  * current request.
  */
 public class DefaultSlingServlet extends SlingAllMethodsServlet {
-
+    private static final long serialVersionUID = -2259461041692895761L;
+    
+    private Map<String, Servlet> renderingServlets = new HashMap <String, Servlet>();
+    
+    public DefaultSlingServlet() {
+        renderingServlets.put("txt", new PlainTextRendererServlet());
+        renderingServlets.put("html", new DefaultHtmlRendererServlet());
+    }
+    
     @Override
+    /** Delegate rendering to one of our renderingServlets, based on the request extension
*/
     protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp)
             throws ServletException, IOException {
-        resp.setContentType("text/plain");
 
         // ensure the resource or try web app contents
         final Resource  r = req.getResource();
@@ -68,23 +77,28 @@
                 "Resource not found: " + r.getURI());
         }
 
+        // make sure we have an Item, and render it via one of our renderingServlets 
         final Object data = r.getRawData();
-        if (data instanceof Item) {
-            final PrintWriter pw = resp.getWriter();
-            try {
-                if (data instanceof Node) {
-                    dump(pw, r, (Node) data);
-                } else {
-                    dump(pw, r, (Property) data);
-                }
-            } catch (RepositoryException re) {
-                throw new ServletException("Cannot dump contents of "
-                    + req.getResource().getURI(), re);
+        if(data!=null && (data instanceof Item)) {
+            String ext = req.getRequestPathInfo().getExtension();
+            if(ext==null || ext.length() == 0) {
+                ext = "txt";
+            }
+            final Servlet s = renderingServlets.get(ext);
+            if(s!=null) {
+                s.service(req, resp);
+            } else {
+                throw new HttpStatusCodeException(
+                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                        "No default renderer found for extension='" + ext + "'"
+                        + ", use one of these extensions: " + renderingServlets.keySet()
+                );
             }
+            
         } else {
             throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_IMPLEMENTED,
                 "Not implemented: resource " + req.getResource().getURI()
-                    + " cannot be dumped by " + getClass().getSimpleName());
+                + " cannot be dumped by " + getClass().getSimpleName());
         }
     }
 
@@ -193,45 +207,6 @@
         return (parent);
     }
 
-    protected void dump(PrintWriter pw, Resource r, Node n) throws RepositoryException {
-        pw.println("** Node dumped by " + getClass().getSimpleName() + "**");
-        pw.println("Node path:" + n.getPath());
-        pw.println("Resource metadata: " + r.getResourceMetadata());
-
-        pw.println("\n** Node properties **");
-        for (PropertyIterator pi = n.getProperties(); pi.hasNext();) {
-            final Property p = pi.nextProperty();
-            printPropertyValue(pw, p);
-        }
-    }
-
-    protected void dump(PrintWriter pw, Resource r, Property p) throws RepositoryException
{
-        pw.println("** Property dumped by " + getClass().getSimpleName() + "**");
-        pw.println("Property path:" + p.getPath());
-        pw.println("Resource metadata: " + r.getResourceMetadata());
-
-        printPropertyValue(pw, p);
-    }
-
-    protected void printPropertyValue(PrintWriter pw, Property p)
-            throws RepositoryException {
-
-        pw.print(p.getName() + ": ");
-
-        if (p.getDefinition().isMultiple()) {
-            Value[] values = p.getValues();
-            pw.print('[');
-            for (int i = 0; i < values.length; i++) {
-                if (i > 0) {
-                    pw.print(", ");
-                }
-                pw.print(values[i].getString());
-            }
-            pw.println(']');
-        } else {
-            pw.println(p.getValue().getString());
-        }
-    }
 
     protected void spool(URL url, SlingHttpServletResponse res) throws IOException {
         URLConnection conn = url.openConnection();

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java?rev=591038&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
Thu Nov  1 07:59:23 2007
@@ -0,0 +1,104 @@
+/*
+ * 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.slingservlets.renderers;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+
+/** A SlingSafeMethodsServlet that renders the current Resource
+ *  as simple HTML
+ */
+public class DefaultHtmlRendererServlet extends SlingSafeMethodsServlet {
+
+    private static final long serialVersionUID = -5815904221043005085L;
+
+    @Override
+    protected void doGet(SlingHttpServletRequest req,SlingHttpServletResponse resp) 
+    throws ServletException,IOException 
+    {
+        final Resource  r = req.getResource();
+        final Object data = r.getRawData();
+        resp.setContentType("text/html; charset=UTF-8");
+        final PrintWriter pw = resp.getWriter();
+        try {
+            pw.println("<html><body>");
+            if (data instanceof Node) {
+                dump(pw, r, (Node) data);
+            } else {
+                dump(pw, r, (Property) data);
+            }
+            pw.println("</body></html>");
+        } catch (RepositoryException re) {
+            throw new ServletException("Cannot dump contents of "
+                + req.getResource().getURI(), re);
+        }
+    }
+    
+    protected void dump(PrintWriter pw, Resource r, Node n) throws RepositoryException {
+        pw.println("<h1>Node dumped by " + getClass().getSimpleName() + "</h1>");
+        pw.println("<p>Node path: <b>" + n.getPath() + "</b></p>");
+        pw.println("<p>Resource metadata: <b>" + r.getResourceMetadata() + "</b></p>");
+
+        pw.println("<h2>Node properties</h2>");
+        for (PropertyIterator pi = n.getProperties(); pi.hasNext();) {
+            final Property p = pi.nextProperty();
+            printPropertyValue(pw, p);
+        }
+    }
+
+    protected void dump(PrintWriter pw, Resource r, Property p) throws RepositoryException
{
+        pw.println("<h2>Property dumped by " + getClass().getSimpleName() + "</h1>");
+        pw.println("<p>Property path:" + p.getPath() + "</p>");
+        pw.println("<p>Resource metadata: " + r.getResourceMetadata() + "</p>");
+
+        printPropertyValue(pw, p);
+    }
+
+    protected void printPropertyValue(PrintWriter pw, Property p)
+            throws RepositoryException {
+
+        pw.print(p.getName() + ": <b>");
+
+        if (p.getDefinition().isMultiple()) {
+            Value[] values = p.getValues();
+            pw.print('[');
+            for (int i = 0; i < values.length; i++) {
+                if (i > 0) {
+                    pw.print(", ");
+                }
+                pw.print(values[i].getString());
+            }
+            pw.print(']');
+        } else {
+            pw.print(p.getValue().getString());
+        }
+        
+        pw.print("</b><br/>");
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/DefaultHtmlRendererServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java?rev=591038&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
Thu Nov  1 07:59:23 2007
@@ -0,0 +1,100 @@
+/*
+ * 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.slingservlets.renderers;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.servlet.ServletException;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+
+/** A SlingSafeMethodsServlet that renders the current Resource
+ *  as plain text.
+ */
+public class PlainTextRendererServlet extends SlingSafeMethodsServlet {
+
+    private static final long serialVersionUID = -5815904221043005085L;
+
+    @Override
+    protected void doGet(SlingHttpServletRequest req,SlingHttpServletResponse resp) 
+    throws ServletException,IOException 
+    {
+        final Resource  r = req.getResource();
+        final Object data = r.getRawData();
+        resp.setContentType("text/plain; charset=UTF-8");
+        final PrintWriter pw = resp.getWriter();
+        try {
+            if (data instanceof Node) {
+                dump(pw, r, (Node) data);
+            } else {
+                dump(pw, r, (Property) data);
+            }
+        } catch (RepositoryException re) {
+            throw new ServletException("Cannot dump contents of "
+                + req.getResource().getURI(), re);
+        }
+    }
+    
+    protected void dump(PrintWriter pw, Resource r, Node n) throws RepositoryException {
+        pw.println("** Node dumped by " + getClass().getSimpleName() + "**");
+        pw.println("Node path:" + n.getPath());
+        pw.println("Resource metadata: " + r.getResourceMetadata());
+
+        pw.println("\n** Node properties **");
+        for (PropertyIterator pi = n.getProperties(); pi.hasNext();) {
+            final Property p = pi.nextProperty();
+            printPropertyValue(pw, p);
+        }
+    }
+
+    protected void dump(PrintWriter pw, Resource r, Property p) throws RepositoryException
{
+        pw.println("** Property dumped by " + getClass().getSimpleName() + "**");
+        pw.println("Property path:" + p.getPath());
+        pw.println("Resource metadata: " + r.getResourceMetadata());
+
+        printPropertyValue(pw, p);
+    }
+
+    protected void printPropertyValue(PrintWriter pw, Property p)
+            throws RepositoryException {
+
+        pw.print(p.getName() + ": ");
+
+        if (p.getDefinition().isMultiple()) {
+            Value[] values = p.getValues();
+            pw.print('[');
+            for (int i = 0; i < values.length; i++) {
+                if (i > 0) {
+                    pw.print(", ");
+                }
+                pw.print(values[i].getString());
+            }
+            pw.println(']');
+        } else {
+            pw.println(p.getValue().getString());
+        }
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/renderers/PlainTextRendererServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/CreateNodeTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/CreateNodeTest.java?rev=591038&r1=591037&r2=591038&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/CreateNodeTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/CreateNodeTest.java
Thu Nov  1 07:59:23 2007
@@ -48,5 +48,11 @@
         final String responseBodyStr = get.getResponseBodyAsString();
         assertTrue(responseBodyStr.contains("value1"));
         assertTrue(responseBodyStr.contains("value2"));
+        
+        // test default txt and html renderings
+        getContent(urlOfNewNode, "text/plain");
+        getContent(urlOfNewNode + ".txt", "text/plain");
+        getContent(urlOfNewNode + ".html", "text/html");
+        assertHttpStatus(urlOfNewNode + ".noRendererForThisExtension", 500);
     }
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java?rev=591038&r1=591037&r2=591038&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/MicroslingHttpTestBase.java
Thu Nov  1 07:59:23 2007
@@ -23,6 +23,7 @@
 import junit.framework.TestCase;
 
 import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
@@ -82,4 +83,26 @@
     protected void assertHttpStatus(String urlString, int expectedStatusCode) throws IOException
{
         assertHttpStatus(urlString, expectedStatusCode, null);
     }
+    
+    /** retrieve the contents of given URL and assert its content type
+     * @throws IOException
+     * @throws HttpException */
+    protected String getContent(String url, String expectedContentType) throws IOException
{
+        final GetMethod get = new GetMethod(url);
+        final int status = httpClient.executeMethod(get);
+        assertEquals("Expected status 200 for " + url,200,status);
+        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();
+    }
+
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java?rev=591038&r1=591037&r2=591038&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/RenderingTestBase.java
Thu Nov  1 07:59:23 2007
@@ -52,25 +52,4 @@
         return url;
     }
 
-    /** retrieve the contents of given URL and assert its content type
-     * @throws IOException
-     * @throws HttpException */
-    protected String getContent(String url, String expectedContentType) throws IOException
{
-        final GetMethod get = new GetMethod(url);
-        final int status = httpClient.executeMethod(get);
-        assertEquals("Expected status 200 for " + url,200,status);
-        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();
-    }
-
 }



Mime
View raw message