Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 4403 invoked from network); 11 Oct 2007 16:29:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 11 Oct 2007 16:29:26 -0000 Received: (qmail 12605 invoked by uid 500); 11 Oct 2007 16:29:08 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 12579 invoked by uid 500); 11 Oct 2007 16:29:08 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 12559 invoked by uid 99); 11 Oct 2007 16:29:08 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Oct 2007 09:29:08 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 Oct 2007 16:29:08 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 465C51A9832; Thu, 11 Oct 2007 09:28:11 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: sling-commits@incubator.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071011162811.465C51A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ velocity 1.5 + + rhino + js + 1.6R7 + 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 html extension.

- See below, Velocity templates for how to create + See below, Velocity templates and Server-side javascript, for how to create a rendering template for the resulting content.

@@ -88,8 +88,13 @@ is found, it used as a Velocity template to render the content.

- 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 RhinoJavascriptServlet + described below. +

+

+ 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.

To test this, try storing (via WebDAV, see mount point URL below) the following script in your @@ -133,6 +138,40 @@

Removing or renaming the Velocity scripts found under /sling/scripts/content/testing will cause the content/testing pages to be rendered by the DefaultSlingServlet. +

+ +

Server-side Javascript

+

+ To show how easy it is to add new scripting languages for processing requests, the + RhinoJavascriptServlet uses the Rhino javascript + interpreter to allow Microsling requests to be handled with server-side javascript. +

+

+ To test this, remove or rename the Velocity script of the above example, and store the script shown + below under /sling/scripts/content/get.js in the repository. +

+

+ Content nodes created with the Content creation form above should then be displayed in HTML, + with a This page is generated from a rhino script note at the top of the page. +

+

+ The RhinoJavascriptServlet uses the same SlingScriptResolver as the VelocityTemplatesServlet, + only with a different extension. +

+

+ Raw server-side javascript is probably more suited for handling POST and PUT requests (that would be + the post.js and put.js scripts), unless a suitable templating library is used. +

+

+

+// 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>");
+