You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2007/10/12 12:19:04 UTC
svn commit: r584116 - in /incubator/sling/whiteboard/microsling/src/main:
java/org/apache/sling/microsling/api/
java/org/apache/sling/microsling/requestcontext/
java/org/apache/sling/microsling/resource/
java/org/apache/sling/microsling/scripting/ java...
Author: bdelacretaz
Date: Fri Oct 12 03:19:03 2007
New Revision: 584116
URL: http://svn.apache.org/viewvc?rev=584116&view=rev
Log:
SLING-47 - microsling: SlingScriptResolver rewritten, html description pages reorganized
Added:
incubator/sling/whiteboard/microsling/src/main/webapp/content-creation-forms.html
incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html
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
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/JcrNodeResource.java
incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.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/RhinoJavascriptServlet.java
incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/VelocityTemplatesServlet.java
incubator/sling/whiteboard/microsling/src/main/webapp/index.html
incubator/sling/whiteboard/microsling/src/main/webapp/microsling.css
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=584116&r1=584115&r2=584116&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 Fri Oct 12 03:19:03 2007
@@ -18,6 +18,18 @@
/** 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
+ * an URI makes it possible to use other data sources (not sure if we
+ * 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.
+ */
+ String getResourceType();
}
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=584116&r1=584115&r2=584116&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 03:19:03 2007
@@ -32,6 +32,7 @@
private final Session session;
private final String contentPath;
private final String extension;
+ private final ServletContext servletContext;
private Resource resource;
public static final String REQ_ATTR_NAME = SlingRequestContext.class.getName();
@@ -47,6 +48,8 @@
/** TODO parse the request to extract extension, selectors, etc. */
public SlingRequestContext(ServletContext sctx,HttpServletRequest req) throws Exception {
+ this.servletContext = sctx;
+
// Store this into the request attributes
req.setAttribute(REQ_ATTR_NAME,this);
@@ -90,5 +93,10 @@
public void setResource(Resource resource) {
this.resource = resource;
+ }
+
+ /** Use the ServletContext to compute the mime-type for given filename */
+ public String getMimeType(String filename) {
+ return servletContext.getMimeType(filename);
}
}
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=584116&r1=584115&r2=584116&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 Fri Oct 12 03:19:03 2007
@@ -25,10 +25,23 @@
class JcrNodeResource implements Resource {
final Node node;
final String path;
+ final String resourceType;
+
+ /** 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 String toString() {
+ return "JcrNodeResource, type=" + resourceType + ", path=" + path;
}
public Object getData() {
@@ -39,5 +52,29 @@
return path;
}
+ public String getResourceType() {
+ return resourceType;
+ }
+ /** 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());
+ }
+
+ return result;
+ }
+
+ /** Filter a node type name so that it can be used in a resource type value */
+ public static String filterName(String name) {
+ return name.toLowerCase().replaceAll("\\:","_");
+ }
}
Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java?rev=584116&r1=584115&r2=584116&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java (original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java Fri Oct 12 03:19:03 2007
@@ -16,69 +16,79 @@
*/
package org.apache.sling.microsling.scripting;
+import javax.jcr.Item;
import javax.jcr.Node;
-import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
+import org.apache.sling.microsling.api.Resource;
import org.apache.sling.microsling.requestcontext.SlingRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/** Find scripts in the repository, based on the current resource URI.
- * For now, a simple resolving algorithm is used, for example to find
- * a "vlt" script for request "content/testing/1192116498928.html":
- *
+/** Find scripts in the repository, based on the current Resource type.
+ * The script filename is built using the current HTTP request method name,
+ * followed by the extension of the current request and the desired script
+ * extension.
+ * For example, a "js" script for a GET request on a Resource of type some/type
+ * with request extension "html" should be stored as
* <pre>
- * SlingScriptResolver: Looking for script '/sling/scripts/content/testing/1192116498928/get.vlt'
- * SlingScriptResolver: Looking for script '/sling/scripts/content/testing/get.vlt'
- * SlingScriptResolver: Looking for script '/sling/scripts/content/get.vlt'
- * SlingScriptResolver: Found script '/sling/scripts/content/get.vlt' for resource path '/content/testing/1192116498928'
+ * /sling/scripts/some/type/get.html.js
* </pre>
- *
- * The SlingScriptResolver goes up the hierarchy under /sling/scripts, starting from the content path,
- * until a suitable script is found. In this case, the name of the script is "get" as Microsling is
- * currently processing an HTTP GET request.
+ * in the repository.
*/
public class SlingScriptResolver {
private static final Logger log = LoggerFactory.getLogger(SlingScriptResolver.class);
public static final String SCRIPT_BASE_PATH = "/sling/scripts";
-
- /** Find a Node that contains a Script to handle the current resource, with the
- * specified script extension
+
+ /** Try to find a script Node that can process the given request, based on the
+ * rules defined above.
+ * @return null if not found.
*/
- public Node resolveScript(HttpServletRequest req,String extension) throws Exception {
+ public Node resolveScript(HttpServletRequest req,String scriptExtension) throws Exception {
+
final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
+ final Resource r = ctx.getResource();
final Session s = ctx.getSession();
- String scriptPath = ctx.getContentPath();
Node result = null;
- while(result == null && scriptPath.length() > 0) {
- final String scriptName = SCRIPT_BASE_PATH + scriptPath + "/" + req.getMethod().toLowerCase() + "." + extension;
- log.debug("Looking for script '" + scriptName + "'");
- try {
- // do we have a script which is an nt:file at that path?
- result = (Node)s.getItem(scriptName);
- if(!result.hasNode("jcr:content")) {
- result = null;
- }
- log.info("Found script '" + result.getPath() + "' for resource path '" + ctx.getContentPath() + "'");
-
- } catch(RepositoryException re) {
- // ignore: script not found
+ if(r==null) {
+ return null;
+ }
+
+ String extensionPart = "";
+ if(ctx.getExtension() != null && ctx.getExtension().length() > 0) {
+ extensionPart = "." + ctx.getExtension();
+ }
+
+ final String scriptPath =
+ SCRIPT_BASE_PATH
+ + "/"
+ + r.getResourceType()
+ + "/"
+ + req.getMethod().toLowerCase()
+ + extensionPart
+ + "." + scriptExtension
+ ;
+
+ if(s.itemExists(scriptPath)) {
+ final Item i = s.getItem(scriptPath);
+ if(i instanceof Node) {
+ result = (Node)i;
}
- if(result==null) {
- // go up the hierarchy to find a script
- final int lastSlash = scriptPath.lastIndexOf('/');
- if(lastSlash < 0) {
- scriptPath = "";
- } else {
- scriptPath = scriptPath.substring(0,lastSlash);
- }
+ // TODO is this the right way of checking for an nt:file node?
+ if(!"nt:file".equals(result.getPrimaryNodeType().getName())) {
+ result = null;
}
+ }
+
+ if(result!=null) {
+ log.info("Found nt:file script node " + result.getPath() + " for Resource=" + r);
+ } else {
+ log.debug("nt:file script node not found at path=" + scriptPath + " for Resource=" + r);
}
return result;
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=584116&r1=584115&r2=584116&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 03:19:03 2007
@@ -74,7 +74,7 @@
throws ServletException, IOException {
try {
- final SlingRequestContext ctx = new SlingRequestContext(getServletContext(),req);
+ new SlingRequestContext(getServletContext(),req);
// let our RequestFilters enhance the request information
// (select a Resource to act on, etc)
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=584116&r1=584115&r2=584116&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 03:19:03 2007
@@ -39,7 +39,7 @@
final SlingScriptResolver scriptResolver = new SlingScriptResolver();
public final static String JS_SCRIPT_EXTENSION = "js";
- /** True if we find a js script to handle the current request */
+ /** True if we have a js script to handle the current request */
public boolean canProcess(HttpServletRequest req) throws Exception {
boolean result = false;
@@ -51,9 +51,9 @@
return result;
}
-
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+
+ /** Called by the doX methods to handle HTTP request method X */
+ protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
// get the script that was revolved by canProcess
@@ -69,8 +69,11 @@
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)
- resp.setContentType("text/html; charset=utf-8");
+ // set the output content type
+ final String fakeFilename = "fake." + ctx.getExtension();
+ resp.setContentType(ctx.getMimeType(fakeFilename) + "; charset=utf-8");
+
+ // evaluate the script, assuming it writes something to the output
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
@@ -85,5 +88,34 @@
Context.exit();
}
}
-
+
+ @Override
+ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
+
+ @Override
+ public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
+
+ @Override
+ public void doHead(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
+
+ @Override
+ public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
+
+ @Override
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
+
+ @Override
+ public void doPut(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ processRequest(req, resp);
+ }
}
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=584116&r1=584115&r2=584116&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 03:19:03 2007
@@ -25,6 +25,7 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.microsling.etc.AbstractSlingServlet;
+import org.apache.sling.microsling.etc.HttpConstants;
import org.apache.sling.microsling.requestcontext.SlingRequestContext;
import org.apache.sling.microsling.scripting.FileNodeRequestAttribute;
import org.apache.sling.microsling.scripting.SlingScriptResolver;
@@ -45,17 +46,17 @@
velocity.init();
}
- /** True if the request has an html extension,
- * and if we find a Velocity templates script to handle it in
- * the repository.
- */
+ /** True we have a Velocity templates script to handle the request */
public boolean canProcess(HttpServletRequest req) throws Exception {
boolean result = false;
- final Node script = scriptResolver.resolveScript(req,VELOCITY_SCRIPT_EXTENSION);
- if(script!=null) {
- new FileNodeRequestAttribute(script,req);
- result = true;
+ // This SlingServlet only handles GET requests
+ if(HttpConstants.METHOD_GET.equals(req.getMethod())) {
+ final Node script = scriptResolver.resolveScript(req,VELOCITY_SCRIPT_EXTENSION);
+ if(script!=null) {
+ new FileNodeRequestAttribute(script,req);
+ result = true;
+ }
}
return result;
@@ -74,8 +75,11 @@
c.put("resource", ctx.getResource());
c.put("slingContext", SlingRequestContext.getFromRequest(req));
+ // set the output content type
+ final String fakeFilename = "fake." + ctx.getExtension();
+ resp.setContentType(ctx.getMimeType(fakeFilename) + "; charset=utf-8");
+
// let Velocity evaluate the script, and send the output to the browser
- resp.setContentType("text/html; charset=utf-8");
final Writer w = new OutputStreamWriter(resp.getOutputStream());
final String logTag = getClass().getSimpleName();
velocity.evaluate(c, w, logTag, scriptReader);
Added: incubator/sling/whiteboard/microsling/src/main/webapp/content-creation-forms.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/content-creation-forms.html?rev=584116&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/content-creation-forms.html (added)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/content-creation-forms.html Fri Oct 12 03:19:03 2007
@@ -0,0 +1,58 @@
+<html>
+<head>
+ <title>microsling content creation forms</title>
+ <link rel="stylesheet" href="microsling.css"/>
+</head>
+<body>
+ <h1>microsling content creation forms</h1>
+
+ <div class="note">
+ <a href="../">back to homepage</a>
+ </div>
+
+ <p>
+ Use these forms to test the microsling request processing and rendering SlingServlets.
+ </p>
+
+ <div>
+ <form class="testbox" action="microsling/content/testing.sling" method="POST">
+ <h1>SlingPostServlet content creation form</h1>
+ <p>
+ Submitting this form creates an <em>nt:unstructured</em> node under content/testing,
+ and displays it using an <em>html</em> extension.
+ </p>
+ <p>
+ See the <em>Velocity scripts</em> and <em>server-side Javascript</em> pages for how to
+ render the content using templates and scripts.
+ </p>
+ <p>
+ Title:
+ <br/>
+ <input name="title" type="text" size="40" value="Default title"/>
+ <br/>
+ Text:
+ <br/>
+ <input name="text" type="text" size="80" value="Default text of the test node"/>
+ <br/>
+ slingResourceType:
+ <span class="note">(leave this empty to use the JCR node type for script resolution)</span>
+ <br/>
+ <input name="slingResourceType" type="text" size="80" value="microsling/example"/>
+ <br/>
+ <input type="submit" value="Create and display node"/>
+ </p>
+ </form>
+ </div>
+
+ <div>
+ <form class="testbox" action="microsling/whatever" method="POST">
+ <h1>Non-Sling POST method form</h1>
+ <p>
+ Submitting this form should cause an error 501: <em>POST method is not supported by DefaultSlingServlet</em>.
+ </p>
+ <input type="submit"/>
+ </form>
+ </div>
+
+</body>
+</html>
\ No newline at end of file
Modified: incubator/sling/whiteboard/microsling/src/main/webapp/index.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/index.html?rev=584116&r1=584115&r2=584116&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/index.html (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/index.html Fri Oct 12 03:19:03 2007
@@ -1,10 +1,10 @@
<html>
<head>
- <title>Microsling homepage</title>
+ <title>microsling homepage</title>
<link rel="stylesheet" href="microsling.css"/>
</head>
<body>
- <h1>Microsling homepage</h1>
+ <h1>microsling homepage</h1>
<p>
<em>Sling request processing, reduced to the max!</em>
@@ -18,164 +18,103 @@
module.
</p>
- <h2>No OSGi</h2>
- <p>
- To keep things simple, microsling does <em>not</em> use OSGi.
- We will of course continue to use OSGi in the real Sling, and there are many
- places in the code which say <em>TODO - use OSGi plugins here</em>. Keeping things
- "static" for this example makes it easier to concentrate on the basics.
- </p>
-
- <h2>Status</h2>
- <p>
- The basic request filtering, SlingServlet and JCR Resource loading mechanisms are in place.
- </p>
- <p>
- The SlingPostServlet allows nt:unstructured nodes to be created in the repository, for testing,
- by POSTing to URLs ending in <em>.sling</em>. See the <em>Content creation form</em> below.
- </p>
- <p>
- The SlingScriptResolver and VelocityTemplatesServlet are used to render content in HTML, using
- Velocity scripts (we'll add more script languages soon) found in the repository under /sling/scripts.
- See below, <em>Velocity Templates</em> for more info.
- </p>
+ <h2>microsling test links</h2>
+ <ul>
+ <li>
+ <a href="content-creation-forms.html">
+ Content creation forms
+ </a>: use this page to create content and test the rendering mechanisms.
+ </li>
+ <li>
+ <a href="velocity-scripts.html">
+ Velocity templates
+ </a>: explains how to use Velocity templates to render content.
+ </li>
+ <li>
+ <a href="server-side-javascript.html">
+ Server-side javascript
+ </a>: explains how to use server-side javascript to process requests.
+ </li>
+ </ul>
- <h2>Microsling test links and forms</h2>
+ <h2>Show me the code</h2>
<p>
- The <a href="microsling/testing-one-two.notfound">microsling/testing-one-two.notfound</a> URL
- should return an error 404.
+ Here's a brief description of how microsling processes HTTP requests. Follow the links to the source code
+ for more details.
+ <ul>
+ <li>
+ The main <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java?view=markup">MicroSlingServlet</a>
+ handles HTTP requests. That's probably where you want to
+ start studying the code.
+ </li>
+ <li>
+ <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestFilter.java?view=markup">RequestFilter</a>
+ objects process the incoming requests before passing them on to SlingServlet objects
+ which do the actual processing. For now, the only RequestFilter is the
+ <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java?view=markup">ResourceResolverFilter</a> which
+ selects the <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/Resource.java?view=markup">Resource</a> (currently a JCR Node, OCM is not used yet) to process. Other filters would include
+ Locale selection, client capabilities analysis, etc.
+ </li>
+ <li>
+ After applying the RequestFilters, the MicroSlingServlet selects a
+ <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingServlet.java?view=markup">SlingServlet</a> to process the request.
+ The first SlingServlet where canProcess(...) returns true is used.
+ </li>
+ <li>
+ The <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java?view=markup">SlingRequestContext</a> is
+ stored as a request attribute, and gives access to Sling-specific objects (Resource, repository Session, etc.) used for request processing.
+ </li>
+ <li>
+ A few SlingServlet classes are implemented (source code <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/">here</a>):
+ <ul>
+ <li>
+ The SlingPostServlet allows Nodes to be created by POSTing to URLs ending
+ with ".sling".
+ </li>
+ <li>
+ The VelocityTemplatesServlet and RhinoJavascriptServlet execute server-side scripts in those
+ languages to process requests.
+ </li>
+ <li>
+ The DefaultSlingServlet is used when no other SlingServlet wants the request.
+ </li>
+ </ul>
+ </li>
+ <li>
+ The <a href="http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java?view=markup">SlingScriptResolver</a>
+ is used by the scripting SlingServlet classes to locate scripts in the repository.
+ See comments in that class for how script paths are computed based on the Resource's resourceType and the
+ request method and extension.
+ </li>
+ <li>
+ Rendering scripts can generate various output formats, for example a Velocity script named "get.xml.vlt" will
+ cause a page with <em>Content-Type=text/xml</em> to be generated by the VelocityTemplatesServlet.
+ </li>
+ <li>
+ The microsling architecture allows
+ additional scripting engines (JSP, JRuby, BSF,...) to be plugged in easily - and this would
+ of course be much easier with OSGi.
+ </li>
+ <li>
+ We'll probably need a ResponseFilter interface at some point, but for now it's not needed. It could be useful
+ to apply "rendering standards" to the output, adding header/footers/glitter to HTML pages for example.
+ </li>
+ </ul>
+ <p>
+ If you have followed the links to source code in the above descriptions, you've seen most or all of the interesting
+ microsling source code. What's remaining are a few simple support classes.
+ </p>
</p>
-
- <p>
-
- <form class="testbox" action="microsling/content/testing.sling" method="POST">
- <h1>Content creation form</h1>
- <p>
- Submitting this form should create a node under content/testing, and display it using
- an <em>html</em> extension.
- </p>
- <p>
- See below, <em>Velocity templates</em> and <em>Server-side javascript</em>, for how to create
- a rendering template for the resulting content.
- </p>
- <p>
- Title:
- <br/>
- <input name="title" type="text" size="40" value="Default title"/>
- <br/>
- Text:
- <br/>
- <input name="text" type="text" size="80" value="Default text of the test node"/>
- <br/>
- <input type="submit" value="Create and display node"/>
- </p>
- </form>
- <form class="testbox" action="microsling/whatever" method="POST">
- <h1>Non-Sling POST method form</h1>
- <p>
- Submitting this form should cause an error 501: <em>POST method is not supported by DefaultSlingServlet</em>.
- </p>
- <input type="submit"/>
- </form>
-
- </p>
-
- <h2>Velocity templates</h2>
- <p>
- When rendering content, the <em>VelocityTemplatesServlet</em> looks for scripts with the
- <em>.vlt</em> extension under <em>/sling/scripts</em> in the JCR repository. If a script
- is found, it used as a Velocity template to render the content.
- </p>
- <p>
- Server-side javascript is supported in a similar way by the <em>RhinoJavascriptServlet</em>
- described below.
- </p>
- <p>
- The Microsling architecture allows
- additional scripting engines (JSP, JRuby, BSF,...) to be plugged in easily - and this would
- of course be much easier with OSGi.
- </p>
- <p>
- To test this, try storing (via WebDAV, see mount point URL below) the following script in your
- repository under <em>/sling/scripts/content/get.vlt</em>. Doing so will cause the script to be used
- to render content found under <em>/content</em>, unless a more specific script is
- found.
- </p>
- <p>
- For example, when rendering the <em>/content/testing/xyz</em> resource, the
- VelocityTemplatesServlet looks for scripts in this order:
- <ul>
- <li><em>/sling/scripts/content/testing/get.vlt</em></li>
- <li><em>/sling/scripts/content/get.vlt</em></li>
- </ul>
- The name <em>get.vlt</em> indicates that this script is meant to process HTTP GET requests.
- GET is probably the only request method that makes sense to process with Velocity, but other
- script types might be useful to process other HTTP methods.
- </p>
- <p>
- Here's the example script. See the <a href="http://velocity.apache.org">Velocity website</a> for
- more info about the syntax. After activating it, content created using the <em>Content creation form</em>
- above should be displayed in HTML with a title that says <em>This is generated from a Velocity template</em>.
- <pre>
-<html>
-<body>
-<h1>This is generated from a Velocity template</h1>
-<p>
- The current resource URI is <b>$resource.URI</b>
-</p>
-
-<h2>$resource.data.getProperty("title").getString()</h2>
-<p>
- $resource.data.getProperty("text").getString()
-</p>
-
-</body>
-</html></pre>
- We could provide wrappers to allow simpler expressions to access data, but that's out of scope
- for this simple example.
- </p>
- <p>
- Removing or renaming the Velocity scripts found under <em>/sling/scripts/content/testing</em>
- will cause the <em>content/testing</em> pages to be rendered by the <em>DefaultSlingServlet</em>.
- </p>
-
- <h2>Server-side Javascript</h2>
- <p>
- To show how easy it is to add new scripting languages for processing requests, the
- <em>RhinoJavascriptServlet</em> uses the <a href="http://www.mozilla.org/rhino/">Rhino</a> javascript
- interpreter to allow Microsling requests to be handled with server-side javascript.
- </p>
- <p>
- To test this, remove or rename the Velocity script of the above example, and store the script shown
- below under <em>/sling/scripts/content/get.js</em> in the repository.
- </p>
- <p>
- Content nodes created with the <em>Content creation form</em> above should then be displayed in HTML,
- with a <em>This page is generated from a rhino script</em> note at the top of the page.
- </p>
- <p>
- The <em>RhinoJavascriptServlet</em> uses the same <em>SlingScriptResolver</em> as the <em>VelocityTemplatesServlet</em>,
- only with a different extension.
- </p>
- <p>
- Raw server-side javascript is probably more suited for handling POST and PUT requests (that would be
- the <em>post.js</em> and <em>put.js</em> scripts), unless a suitable templating library is used.
- </p>
+ <h2>No OSGi</h2>
<p>
- <pre>
-// Not the best way of rendering output, of course...
-out.println("<html><body>");
-out.println("<p>This page is generated from a rhino script</p>");
-out.println("<h1>" + resource.getURI() + "</h1>");
-out.println("<p>Title: " + resource.getData().getProperty('title').getString() + "</p>");
-out.println("<p>Text: " + resource.getData().getProperty('text').getString() + "</p>");
-out.println("</body></html>");
- </pre>
+ To keep things simple, microsling does <em>not</em> use OSGi.
+ We will of course continue to use OSGi in the real Sling, and there are many
+ places in the code which say <em>TODO - use OSGi plugins here</em>. Keeping things
+ "static" for this example makes it easier to concentrate on the basics.
</p>
-
- <h2>More test links</h2>
+ <h2>More test links</h2>
<p>
<ul>
<li>
@@ -183,11 +122,10 @@
properties.
</li>
<li>
- <a href="repository/default">WebDAV mount point</a>, use this URL to mount
- the repository via WebDAV.
+ <a href="repository/default">WebDAV mount point</a>: use this URL to mount
+ the repository via WebDAV (login = admin/admin).
</li>
</ul>
</p>
-
</body>
</html>
Modified: incubator/sling/whiteboard/microsling/src/main/webapp/microsling.css
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/microsling.css?rev=584116&r1=584115&r2=584116&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/microsling.css (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/microsling.css Fri Oct 12 03:19:03 2007
@@ -41,9 +41,17 @@
margin-bottom: 0.5em;
}
+li {
+ margin-bottom: 0.2em;
+}
+
pre {
font-size: 120%;
background-color: #FFFFCC;
border: solid #FF9900 1px;
padding: 0.5em;
}
+
+.note {
+ font-size:80%;
+}
Added: incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html?rev=584116&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html (added)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html Fri Oct 12 03:19:03 2007
@@ -0,0 +1,53 @@
+<html>
+<head>
+ <title>microsling server-side javascript</title>
+ <link rel="stylesheet" href="microsling.css"/>
+</head>
+<body>
+ <h1>microsling server-side javascript</h1>
+
+ <div class="note">
+ <a href="../">back to homepage</a>
+ </div>
+
+ <p>
+ Server-side javascript can be used by microsling to process requests. The scripts are located by the
+ <em>SlingScriptResolver</em>, as used by the <a href="velocity-scripts.html">Velocity templates</a> (see that
+ page for more details about this).
+ </p>
+ <p>
+ See the (very simple) <em>RhinoJavascriptServlet</em> for implementation details.
+ </p>
+ <p>
+ To test this, remove or rename <a href="velocity-scripts.html">Velocity templates</a>
+ if you played with them before (Velocity has priority due to the order in which the SlingServlet classes
+ are setup), and store the script shown
+ below under <em> /sling/scripts/microsling/example/get.html.js</em> in the repository.
+ </p>
+ <p>
+ Content nodes created with the <a href="content-creation-forms.html">Content creation forms</a>
+ should then be displayed in HTML,with a <em>This page is generated from a rhino script</em> note at the top of the
+ page.
+ </p>
+ <p>
+ As for the Velocity templates, other output formats can be generated, for example by renaming the script
+ to <em>get.txt.js</em> and modifying it to output plain text.
+ </p>
+ <p>
+ Raw server-side javascript is probably more suited for handling POST and PUT requests (that would be
+ the <em>post.js</em> and <em>put.js</em> scripts), unless a suitable templating library is used.
+ </p>
+ <p>
+ <pre>
+// Not the best way of rendering output, of course...
+out.println("<html><body>");
+out.println("<p>This page is generated from a rhino script</p>");
+out.println("<h1>" + resource.getURI() + "</h1>");
+out.println("<p>Title: " + resource.getData().getProperty('title').getString() + "</p>");
+out.println("<p>Text: " + resource.getData().getProperty('text').getString() + "</p>");
+out.println("</body></html>");
+ </pre>
+ </p>
+
+</body>
+</html>
\ No newline at end of file
Added: 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=584116&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html (added)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/velocity-scripts.html Fri Oct 12 03:19:03 2007
@@ -0,0 +1,66 @@
+<html>
+<head>
+ <title>microsling Velocity templates</title>
+ <link rel="stylesheet" href="microsling.css"/>
+</head>
+<body>
+ <h1>microsling Velocity templates</h1>
+
+ <div class="note">
+ <a href="../">back to homepage</a>
+ </div>
+ <p>
+ When processing requests, the <em>VelocityTemplatesServlet</em> ask the <em>SlingScriptResolver</em>
+ for scripts with the <em>.vlt</em> extension.
+ </p>
+ <p>
+ If a script
+ is found, it used as a Velocity template to render the content.
+ </p>
+ <p>
+ To test this, try storing (via WebDAV, see mount point URL on the microsling homepage) the following script in your
+ repository under <em> /sling/scripts/microsling/example/get.html.vlt</em>.
+ Doing so will cause that script to be used
+ to render nodes having <em>slingResourceType=microsling/example</em>.
+ </p>
+ <p>
+ To try that rendering, use the <a href="content-creation-forms.html">content creation</a> page to create
+ and node and display it.
+ </p>
+ <p>
+ The name <em>get.html.vlt</em> indicates that this script is meant to process HTTP GET requests with
+ an URL having an <em>html</em> extension.
+ GET is probably the only request method that makes sense to process with Velocity, but other
+ script types might want to handle other HTTP methods.
+ </p>
+ <p>
+ To generate XML instead, rename the script to <em>get.xml.vlt</em>, modify it to generate
+ the XML of your choice, and replace <em>.html</em> with <em>.xml</em> at the end of the request URL.
+ </p>
+ <p>
+ Here's the example HTML template. See the <a href="http://velocity.apache.org">Velocity website</a> for
+ more info about the syntax.
+ <pre>
+<html>
+<body>
+<h1>This is generated from a Velocity template</h1>
+<p>
+ The current resource URI is <b>$resource.URI</b>
+</p>
+
+<h2>$resource.data.getProperty("title").getString()</h2>
+<p>
+ $resource.data.getProperty("text").getString()
+</p>
+
+</body>
+</html></pre>
+ We could provide wrappers to allow simpler expressions to access data, but that's out of scope
+ for this simple example.
+ </p>
+ <p>
+ To deactivate the Velocity-based rendering of nodes, rename or delete the corresponding rendering
+ scripts.
+ </p>
+</body>
+</html>
\ No newline at end of file