incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r585095 - in /incubator/sling/whiteboard/microsling/src/main: java/org/apache/sling/microsling/api/ java/org/apache/sling/microsling/resource/ java/org/apache/sling/microsling/slingservlets/ webapp/
Date Tue, 16 Oct 2007 10:07:32 GMT
Author: fmeschbe
Date: Tue Oct 16 03:07:27 2007
New Revision: 585095

URL: http://svn.apache.org/viewvc?rev=585095&view=rev
Log:
SLING-57 Resource: retarget the data to be the mapped object and add Item for the addressed
repository Item

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java?rev=585095&r1=585094&r2=585095&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java
Tue Oct 16 03:07:27 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.microsling.api;
 
+import javax.jcr.Item;
+
 /** Resources are pieces of content on which Sling acts  */
 public interface Resource {
     /** This resource's URI - for now that could be a JCR path, but having
@@ -23,13 +25,22 @@
      *  really need that though).
      */
     String getURI();
-    
-    /** This resource's data - either a JCR Node, or an OCM-mapped Object */
-    Object getData();
-    
+
     /** The resource type is meant to point to rendering/processing scripts,
      *  editing dialogs, etc. It is usually a path in the repository, where
-     *  scripts and other tools definitions are found.    
+     *  scripts and other tools definitions are found.
      */
     String getResourceType();
+
+    /**
+     * The JCR item addressed by the {@link #getURI() resource URI} or null
+     * if the resource URI does not actually address an item in a repository.
+     */
+    Item getItem();
+
+    /** Returns the object mapped from the {@link #getItem() item} or null,
+     * if the item cannot be mapped or mapping is not supported by the
+     * implementation.
+     */
+    Object getData();
 }

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java?rev=585095&r1=585094&r2=585095&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/JcrNodeResource.java
Tue Oct 16 03:07:27 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.microsling.resource;
 
+import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
@@ -23,53 +24,63 @@
 
 /** A Resource that wraps a JCR Node */
 class JcrNodeResource implements Resource {
-    final Node node;
-    final String path;
-    final String resourceType;
-    
+    private final Node node;
+    private final String path;
+    private final String resourceType;
+    private Object data;
+
     /** JCR Property that defines the resource type of this node
      *  (TODO: use a sling:namespaced property name)
      */
     public static final String SLING_RESOURCE_TYPE_PROPERTY = "slingResourceType";
-    
+
     public static final String NODE_TYPE_RT_PREFIX = "NODETYPES/";
-    
+
     JcrNodeResource(javax.jcr.Session s,String path) throws RepositoryException {
         node = (Node)s.getItem(path);
         this.path = node.getPath();
         resourceType = getResourceTypeForNode(node);
     }
-    
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+
     public String toString() {
         return "JcrNodeResource, type=" + resourceType + ", path=" + path;
     }
 
-    public Object getData() {
+    public Item getItem() {
         return node;
     }
 
     public String getURI() {
         return path;
     }
-    
+
     public String getResourceType() {
         return resourceType;
     }
-    
+
+    // no object mapping yet
+    public Object getData() {
+        return data;
+    }
+
     /** Compute the resource type of the given node, using either the SLING_RESOURCE_TYPE_PROPERTY,
      *  or the node's primary type, if the property is not set
-     */ 
+     */
     public static String getResourceTypeForNode(Node node) throws RepositoryException {
         String result = null;
-        
+
         if(node.hasProperty(SLING_RESOURCE_TYPE_PROPERTY)) {
             result = node.getProperty(SLING_RESOURCE_TYPE_PROPERTY).getValue().getString().toLowerCase().trim();
         }
-        
+
         if(result==null || result.length() == 0) {
-            result = NODE_TYPE_RT_PREFIX + filterName(node.getPrimaryNodeType().getName());

+            result = NODE_TYPE_RT_PREFIX + filterName(node.getPrimaryNodeType().getName());
         }
-        
+
         return result;
     }
 

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=585095&r1=585094&r2=585095&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Tue Oct 16 03:07:27 2007
@@ -19,10 +19,12 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 
+import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
+import javax.jcr.Value;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -30,48 +32,80 @@
 import org.apache.sling.microsling.etc.AbstractSlingServlet;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 
-/** The default SlingServlet, used if no other SlingServlet
- *  wants to process the current request.
+/**
+ * The default SlingServlet, used if no other SlingServlet wants to process the
+ * current request.
  */
 public class DefaultSlingServlet extends AbstractSlingServlet {
 
     @Override
-    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
+    public void doGet(HttpServletRequest req, HttpServletResponse resp)
+            throws Exception {
         resp.setContentType("text/plain");
-        
+
         final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
-        if(ctx.getResource() == null) {
-            throw new HttpStatusCodeException(404,"Resource not found: " + req.getPathInfo());
+        if (ctx.getResource() == null) {
+            throw new HttpStatusCodeException(404, "Resource not found: "
+                + req.getPathInfo());
         }
-        
-        final Object data = ctx.getResource().getData();
-        if(data instanceof Node) {
-            final PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
-            dumpNode(pw,(Node)data);
+
+        final Item data = ctx.getResource().getItem();
+        if (data != null) {
+            final PrintWriter pw = new PrintWriter(new OutputStreamWriter(
+                resp.getOutputStream()));
+            if (data.isNode()) {
+                dump(pw, (Node) data);
+            } else {
+                dump(pw, (Property) data);
+            }
             pw.flush();
-            
         } else {
-            throw new HttpStatusCodeException(
-                    501,
-                    "Not implemented: resource of type " 
-                    + data.getClass().getName()
-                    + " cannot be dumped by " + getClass().getSimpleName()
-            );
+            throw new HttpStatusCodeException(501,
+                "Not implemented: resource "
+                    + ctx.getResource().getURI() + " cannot be dumped by "
+                    + getClass().getSimpleName());
         }
     }
 
     public boolean canProcess(HttpServletRequest req) throws Exception {
         return true;
     }
-    
-    protected void dumpNode(PrintWriter pw, Node n) throws RepositoryException {
+
+    protected void dump(PrintWriter pw, Node n) throws RepositoryException {
         pw.println("** Node dumped by " + getClass().getSimpleName() + "**");
         pw.println("Node path:" + n.getPath());
-        
+
         pw.println("\n** Node properties **");
-        for(PropertyIterator pi = n.getProperties(); pi.hasNext(); ) {
+        for (PropertyIterator pi = n.getProperties(); pi.hasNext();) {
             final Property p = pi.nextProperty();
-            pw.println(p.getName() + ": " + p.getValue().getString());
+            printPropertyValue(pw, p);
+        }
+    }
+
+    protected void dump(PrintWriter pw, Property p) throws RepositoryException {
+        pw.println("** Property dumped by " + getClass().getSimpleName() + "**");
+        pw.println("Property path:" + p.getPath());
+
+        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());
         }
     }
 

Modified: incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html?rev=585095&r1=585094&r2=585095&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html Tue Oct 16
03:07:27 2007
@@ -48,9 +48,9 @@
   The current resource URI is &lt;b>$resource.URI&lt;/b>
 &lt;/p>
 
-&lt;h2>$resource.data.getProperty("title").getString()&lt;/h2>
+&lt;h2>$resource.item.getProperty("title").getString()&lt;/h2>
 &lt;p>
-  $resource.data.getProperty("text").getString()
+  $resource.item.getProperty("text").getString()
 &lt;/p>
 
 &lt;/body>



Mime
View raw message