incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r583869 - in /incubator/sling/whiteboard/microsling: ./ src/main/java/org/apache/sling/microsling/servlet/ src/main/java/org/apache/sling/microsling/slingservlets/ src/main/webapp/
Date Thu, 11 Oct 2007 16:28:10 GMT
Author: bdelacretaz
Date: Thu Oct 11 09:28:03 2007
New Revision: 583869

URL: http://svn.apache.org/viewvc?rev=583869&view=rev
Log:
SLING-47 - RhinoJavascriptServlet handles server-side Javascript

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/pom.xml
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/webapp/index.html

Modified: incubator/sling/whiteboard/microsling/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/pom.xml?rev=583869&r1=583868&r2=583869&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/pom.xml (original)
+++ incubator/sling/whiteboard/microsling/pom.xml Thu Oct 11 09:28:03 2007
@@ -72,6 +72,11 @@
       <artifactId>velocity</artifactId>
       <version>1.5</version>
     </dependency>
+    <dependency>
+      <groupId>rhino</groupId>
+      <artifactId>js</artifactId>
+      <version>1.6R7</version>
+    </dependency>
   </dependencies>
 
   <build>

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=583869&r1=583868&r2=583869&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
Thu Oct 11 09:28:03 2007
@@ -35,6 +35,7 @@
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 import org.apache.sling.microsling.resource.ResourceResolverFilter;
 import org.apache.sling.microsling.slingservlets.DefaultSlingServlet;
+import org.apache.sling.microsling.slingservlets.RhinoJavascriptServlet;
 import org.apache.sling.microsling.slingservlets.SlingPostServlet;
 import org.apache.sling.microsling.slingservlets.VelocityTemplatesServlet;
 
@@ -59,6 +60,7 @@
             // TODO use OSGi to setup this list 
             servlets.add(new SlingPostServlet());
             servlets.add(new VelocityTemplatesServlet());
+            servlets.add(new RhinoJavascriptServlet());
             servlets.add(new DefaultSlingServlet());
             
         } catch(Exception e) {

Added: 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=583869&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
Thu Oct 11 09:28:03 2007
@@ -0,0 +1,96 @@
+/*
+ * 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;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+import javax.jcr.Node;
+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.scripting.SlingScriptResolver;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptableObject;
+
+/** A SlingServlet that uses the Rhino interpreter to process
+ *  Sling request with server-side javascript.
+ */
+public class RhinoJavascriptServlet extends AbstractSlingServlet {
+
+    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 */
+    public boolean canProcess(HttpServletRequest req) throws Exception {
+        boolean result = false;
+
+        final Node script = scriptResolver.resolveScript(req,JS_SCRIPT_EXTENSION);
+        if(script!=null) {
+            req.setAttribute(RequestAttributeNames.REQUEST_ATTR_RESOLVED_SCRIPT_NODE,script);
+            result = true;
+        }
+        
+        return result;
+    }
+
+    @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");
+        }
+        
+        // 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);
+
+        // create a rhino Context and execute the script
+        try {
+            final Context rhinoContext = Context.enter();
+            
+            // put useful values in scope for the JS script
+            final ScriptableObject scope = rhinoContext.initStandardObjects();
+            final Object wrappedResource = Context.javaToJS(getCurrentResource(req),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");
+            final PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
+            ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
+            
+            final String sourceName = scriptNode.getPath();
+            final int lineNumber = 1;
+            final Object securityDomain = null;
+            rhinoContext.evaluateReader(scope, scriptReader, sourceName, lineNumber, securityDomain);
+            
+            pw.flush();
+            
+        } finally {
+            Context.exit();
+        }
+    }
+    
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/RhinoJavascriptServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

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=583869&r1=583868&r2=583869&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/index.html (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/index.html Thu Oct 11 09:28:03 2007
@@ -55,7 +55,7 @@
         an <em>html</em> extension. 
       </p>
       <p>
-        See below, <em>Velocity templates</em> for how to create
+        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>
@@ -88,8 +88,13 @@
   	is found, it used as a Velocity template to render the content.
   </p>
   <p>
-  	For microsling, Velocity is just another rendering templates mechanism, the architecture
allows
-  	additional rendering engines (JSP, Ruby, server-side Javascript, ...) to be plugged in
easily.   
+  	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 
@@ -133,6 +138,40 @@
   <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>
+  <p>
+    <pre>
+// Not the best way of rendering output, of course...
+out.println("&lt;html>&lt;body>");
+out.println("&lt;p>This page is generated from a rhino script&lt;/p>");
+out.println("&lt;h1>" + resource.getURI() + "&lt;/h1>");
+out.println("&lt;p>Title: " + resource.getData().getProperty('title').getString()
+ "&lt;/p>");
+out.println("&lt;p>Text: " + resource.getData().getProperty('text').getString() +
"&lt;/p>");
+out.println("&lt;/body>&lt;/html>");
+    </pre>
   </p>
   
   



Mime
View raw message