incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r584084 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling: etc/ request/ requestcontext/ resource/ scripting/ servlet/ slingservlets/
Date Fri, 12 Oct 2007 07:23:12 GMT
Author: bdelacretaz
Date: Fri Oct 12 00:23:08 2007
New Revision: 584084

URL: http://svn.apache.org/viewvc?rev=584084&view=rev
Log:
SLING-47 - microsling: simplify request attributes storage, and refactor script nodes data
access

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/MissingRequestAttributeException.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/FileNodeRequestAttribute.java
Removed:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/AbstractSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/AbstractSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/AbstractSlingServlet.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/AbstractSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/AbstractSlingServlet.java
Fri Oct 12 00:23:08 2007
@@ -20,9 +20,7 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.microsling.api.HttpStatusCodeException;
-import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.SlingServlet;
-import org.apache.sling.microsling.request.RequestAttributeNames;
 
 /** Base class for SlingServlets, fails with "not implemented"
  *  for all HTTP methods supported by SlingServlet
@@ -56,9 +54,5 @@
 
     public void doPut(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
         throwMethodNotImplementedException(req);
-    }
-    
-    protected Resource getCurrentResource(HttpServletRequest req) {
-        return (Resource)req.getAttribute(RequestAttributeNames.REQUEST_ATTR_RESOURCE);
     }
 }

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/MissingRequestAttributeException.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/MissingRequestAttributeException.java?rev=584084&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/MissingRequestAttributeException.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/etc/MissingRequestAttributeException.java
Fri Oct 12 00:23:08 2007
@@ -0,0 +1,12 @@
+package org.apache.sling.microsling.etc;
+
+import org.apache.sling.microsling.api.SlingException;
+
+/** Indicates a missing Request attribute */
+public class MissingRequestAttributeException extends SlingException {
+    private static final long serialVersionUID = 1L;
+
+    public MissingRequestAttributeException(String attributeName) {
+        super("Missing request attribute '" + attributeName + "'");
+    }
+}

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
Fri Oct 12 00:23:08 2007
@@ -22,8 +22,9 @@
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.SlingException;
-import org.apache.sling.microsling.request.RequestAttributeNames;
+import org.apache.sling.microsling.etc.MissingRequestAttributeException;
 
 /** Additional Request-based info used by for SlingServlets */
 public class SlingRequestContext {
@@ -31,18 +32,24 @@
     private final Session session;
     private final String contentPath;
     private final String extension;
+    private Resource resource;
+    
+    public static final String REQ_ATTR_NAME = SlingRequestContext.class.getName();
     
     /** Get the SlingRequestContext from req */
-    public static SlingRequestContext getFromRequest(HttpServletRequest req) throws SlingException
{
-        final SlingRequestContext ctx = (SlingRequestContext)req.getAttribute(RequestAttributeNames.REQUEST_ATTR_CONTEXT);
+    public static SlingRequestContext getFromRequest(HttpServletRequest req) throws MissingRequestAttributeException
{
+        final SlingRequestContext ctx = (SlingRequestContext)req.getAttribute(REQ_ATTR_NAME);
         if(ctx == null) {
-            throw new SlingException("SlingRequestContext not found in HttpServletRequest
attributes");
+            throw new MissingRequestAttributeException(REQ_ATTR_NAME);
         }
         return ctx;
     }
 
     /** TODO parse the request to extract extension, selectors, etc. */
     public SlingRequestContext(ServletContext sctx,HttpServletRequest req) throws Exception
{
+        // Store this into the request attributes
+        req.setAttribute(REQ_ATTR_NAME,this);
+        
         // Access our Repository and get a Session
         final String repoAttr = Repository.class.getName();
         final Repository rep = (Repository)sctx.getAttribute(repoAttr);
@@ -75,5 +82,13 @@
 
     public String getExtension() {
         return extension;
+    }
+
+    public Resource getResource() {
+        return resource;
+    }
+
+    public void setResource(Resource resource) {
+        this.resource = resource;
     }
 }

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
Fri Oct 12 00:23:08 2007
@@ -20,7 +20,6 @@
 
 import org.apache.sling.microsling.api.RequestFilter;
 import org.apache.sling.microsling.api.Resource;
-import org.apache.sling.microsling.request.RequestAttributeNames;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 
 /** A RequestFilter that selects the Resource to process */
@@ -37,7 +36,7 @@
         }
         
         if(r != null) {
-            req.setAttribute(RequestAttributeNames.REQUEST_ATTR_RESOURCE, r);
+            ctx.setResource(r);
         }
     }
     

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/FileNodeRequestAttribute.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/FileNodeRequestAttribute.java?rev=584084&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/FileNodeRequestAttribute.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/FileNodeRequestAttribute.java
Fri Oct 12 00:23:08 2007
@@ -0,0 +1,71 @@
+/*
+ * 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.scripting;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.sling.microsling.etc.MissingRequestAttributeException;
+
+/** Stores an nt:file Node in a request attribute,
+ *  and allows the file's inputStream or reader to
+ *  be accessed easily.
+ *  Used by scripting SlingServlets to store resolved script nodes
+ *  in between canProcess and doX method calls.
+ */ 
+public class FileNodeRequestAttribute {
+    
+    private final Node node;
+    public static final String REQ_ATTR_NAME = FileNodeRequestAttribute.class.getName();
+    
+    /** Store this as an attribute of req */
+    public FileNodeRequestAttribute(Node n,HttpServletRequest req) {
+        node = n;
+        req.setAttribute(REQ_ATTR_NAME,this);
+    }
+    
+    /** Retrieve a FileNodeRequestAttribute from given request */
+    public static FileNodeRequestAttribute getFromRequest(HttpServletRequest req) throws
MissingRequestAttributeException {
+        final FileNodeRequestAttribute result = 
+            (FileNodeRequestAttribute)req.getAttribute(REQ_ATTR_NAME);
+        if(result==null) {
+            throw new MissingRequestAttributeException(REQ_ATTR_NAME);
+        }
+        return result;
+    }
+    
+    /** Return our nt:file node */
+    public Node getNode() {
+        return node;
+    }
+
+    /** Return an InputStream that provides our node's file content */
+    public InputStream getInputStream() throws RepositoryException {
+        // TODO need more robust checks
+        return node.getNode("jcr:content").getProperty("jcr:data").getStream();
+    }
+    
+    /** Return a Reader that provides our node's file content */
+    public Reader getReader() throws RepositoryException {
+        return new InputStreamReader(getInputStream());
+    }
+}

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
Fri Oct 12 00:23:08 2007
@@ -31,7 +31,6 @@
 import org.apache.sling.microsling.api.RequestFilter;
 import org.apache.sling.microsling.api.SlingServlet;
 import org.apache.sling.microsling.etc.HttpConstants;
-import org.apache.sling.microsling.request.RequestAttributeNames;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 import org.apache.sling.microsling.resource.ResourceResolverFilter;
 import org.apache.sling.microsling.slingservlets.DefaultSlingServlet;
@@ -76,7 +75,7 @@
         
         try {
             final SlingRequestContext ctx = new SlingRequestContext(getServletContext(),req);
-            req.setAttribute(RequestAttributeNames.REQUEST_ATTR_CONTEXT, ctx);
+            
             // let our RequestFilters enhance the request information
             // (select a Resource to act on, etc)
             for(RequestFilter rf : requestFilters) {

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=584084&r1=584083&r2=584084&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
Fri Oct 12 00:23:08 2007
@@ -27,8 +27,8 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.microsling.api.HttpStatusCodeException;
-import org.apache.sling.microsling.api.Resource;
 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.
@@ -39,12 +39,12 @@
     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
         resp.setContentType("text/plain");
         
-        final Resource r = getCurrentResource(req);
-        if(r==null) {
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
+        if(ctx.getResource() == null) {
             throw new HttpStatusCodeException(404,"Resource not found: " + req.getPathInfo());
         }
         
-        final Object data = r.getData();
+        final Object data = ctx.getResource().getData();
         if(data instanceof Node) {
             final PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
             dumpNode(pw,(Node)data);

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
Fri Oct 12 00:23:08 2007
@@ -16,8 +16,6 @@
  */
 package org.apache.sling.microsling.slingservlets;
 
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.Reader;
@@ -26,9 +24,9 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.microsling.api.SlingException;
 import org.apache.sling.microsling.etc.AbstractSlingServlet;
-import org.apache.sling.microsling.request.RequestAttributeNames;
+import org.apache.sling.microsling.requestcontext.SlingRequestContext;
+import org.apache.sling.microsling.scripting.FileNodeRequestAttribute;
 import org.apache.sling.microsling.scripting.SlingScriptResolver;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.ScriptableObject;
@@ -47,7 +45,7 @@
 
         final Node script = scriptResolver.resolveScript(req,JS_SCRIPT_EXTENSION);
         if(script!=null) {
-            req.setAttribute(RequestAttributeNames.REQUEST_ATTR_RESOLVED_SCRIPT_NODE,script);
+            new FileNodeRequestAttribute(script,req);
             result = true;
         }
         
@@ -56,16 +54,11 @@
 
     @Override
     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
-        // grab the script node stored by canProcess
-        final Node scriptNode = (Node)req.getAttribute(RequestAttributeNames.REQUEST_ATTR_RESOLVED_SCRIPT_NODE);
-        if(scriptNode==null) {
-            throw new SlingException("Unexpected: scriptNode not found in request attributes");
-        }
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
         
-        // access the script itself
-        // TODO move this to a utility class
-        final InputStream scriptStream = scriptNode.getNode("jcr:content").getProperty("jcr:data").getStream();
-        final Reader scriptReader = new InputStreamReader(scriptStream);
+        // get the script that was revolved by canProcess
+        final FileNodeRequestAttribute script = FileNodeRequestAttribute.getFromRequest(req);
+        final Reader scriptReader = script.getReader(); 
 
         // create a rhino Context and execute the script
         try {
@@ -73,7 +66,7 @@
             
             // put useful values in scope for the JS script
             final ScriptableObject scope = rhinoContext.initStandardObjects();
-            final Object wrappedResource = Context.javaToJS(getCurrentResource(req),scope);
+            final Object wrappedResource = Context.javaToJS(ctx.getResource(),scope);
             ScriptableObject.putProperty(scope, "resource", wrappedResource);
             
             // evaluate the script (assuming it outputs HTML, we could ask it instead)
@@ -81,7 +74,7 @@
             final PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
             ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
             
-            final String sourceName = scriptNode.getPath();
+            final String sourceName = script.getNode().getPath();
             final int lineNumber = 1;
             final Object securityDomain = null;
             rhinoContext.evaluateReader(scope, scriptReader, sourceName, lineNumber, securityDomain);

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java?rev=584084&r1=584083&r2=584084&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java
Fri Oct 12 00:23:08 2007
@@ -16,8 +16,6 @@
  */
 package org.apache.sling.microsling.slingservlets;
 
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
@@ -26,10 +24,9 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.microsling.api.SlingException;
 import org.apache.sling.microsling.etc.AbstractSlingServlet;
-import org.apache.sling.microsling.request.RequestAttributeNames;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
+import org.apache.sling.microsling.scripting.FileNodeRequestAttribute;
 import org.apache.sling.microsling.scripting.SlingScriptResolver;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -57,7 +54,7 @@
 
         final Node script = scriptResolver.resolveScript(req,VELOCITY_SCRIPT_EXTENSION);
         if(script!=null) {
-            req.setAttribute(RequestAttributeNames.REQUEST_ATTR_RESOLVED_SCRIPT_NODE,script);
+            new FileNodeRequestAttribute(script,req);
             result = true;
         }
         
@@ -66,19 +63,15 @@
 
     @Override
     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
-        // grab the script node stored by canProcess
-        final Node scriptNode = (Node)req.getAttribute(RequestAttributeNames.REQUEST_ATTR_RESOLVED_SCRIPT_NODE);
-        if(scriptNode==null) {
-            throw new SlingException("Unexpected: scriptNode not found in request attributes");
-        }
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
         
-        // access the script itself
-        final InputStream scriptStream = scriptNode.getNode("jcr:content").getProperty("jcr:data").getStream();
-        final Reader scriptReader = new InputStreamReader(scriptStream);
+        // get the script that was revolved by canProcess
+        final FileNodeRequestAttribute script = FileNodeRequestAttribute.getFromRequest(req);
+        final Reader scriptReader = script.getReader(); 
         
         // initialize the Velocity context
         final VelocityContext c = new VelocityContext();
-        c.put("resource", getCurrentResource(req));
+        c.put("resource", ctx.getResource());
         c.put("slingContext", SlingRequestContext.getFromRequest(req));
         
         // let Velocity evaluate the script, and send the output to the browser



Mime
View raw message