incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r585188 - in /incubator/sling/whiteboard/microsling/src: main/java/org/apache/sling/microsling/scripting/ main/java/org/apache/sling/microsling/scripting/helpers/ main/webapp/ test/java/org/apache/sling/microsling/scripting/ test/java/org/a...
Date Tue, 16 Oct 2007 16:27:33 GMT
Author: bdelacretaz
Date: Tue Oct 16 09:27:25 2007
New Revision: 585188

URL: http://svn.apache.org/viewvc?rev=585188&view=rev
Log:
SLING-52 - scripts use more user-friendly names

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
  (with props)
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
    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/scripting/SlingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java?rev=585188&r1=585187&r2=585188&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
Tue Oct 16 09:27:25 2007
@@ -23,6 +23,7 @@
 
 import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.request.SlingRequestContext;
+import org.apache.sling.microsling.scripting.helpers.ScriptFilenameBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,6 +44,8 @@
 
     public static final String SCRIPT_BASE_PATH = "/sling/scripts";
     
+    private final ScriptFilenameBuilder scriptFilenameBuilder = new ScriptFilenameBuilder();
+    
     /** Try to find a script Node that can process the given request, based on the
      *  rules defined above.
      *  @return null if not found.
@@ -58,19 +61,19 @@
             return null;
         }
 
-        String contentTypePart = "";
-        if(ctx.getResponseContentType() != null && ctx.getResponseContentType().length()
> 0) {
-            contentTypePart = "." + filterStringForFilename(ctx.getResponseContentType());
-        }
+        final String scriptFilename = scriptFilenameBuilder.buildScriptFilename(
+                req.getMethod(),
+                ctx.getRequestPathInfo().getSelectorString(),
+                ctx.getResponseContentType(),
+                scriptExtension
+        );
 
         final String scriptPath =
             SCRIPT_BASE_PATH
             + "/"
             + r.getResourceType()
             + "/"
-            + req.getMethod().toLowerCase()
-            + contentTypePart
-            + "." + scriptExtension
+            + scriptFilename
         ;
 
         if(s.itemExists(scriptPath)) {

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java?rev=585188&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
Tue Oct 16 09:27:25 2007
@@ -0,0 +1,82 @@
+/*
+ * 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.helpers;
+
+import org.apache.sling.microsling.etc.HttpConstants;
+
+/** Builds the names of script files based on the current input:
+ *  <ul>
+ *    <li>HTTP request method name</li>
+ *    <li>Request selectors, if any</li>
+ *    <li>Desired response Content-Type</li>
+ *    <li>Desired script extension</li>
+ *  </ul>
+ *  
+ *  See ScriptFilenameBuilderTest for examples.
+ *  
+ *  Note that names can include partial paths, for example we return
+ *  "print/a4/html.js" for a GET request for an html document with
+ *  selectors "print.a4".
+ */
+public class ScriptFilenameBuilder {
+
+    /** @return a name like "html.js" or "print/a4/html.vlt" or "POST.js" */ 
+    public String buildScriptFilename(String methodName,String selectors,String contentType,String
scriptExtension) {
+        final StringBuffer sb = new StringBuffer();
+        
+        // path before filename:
+        // add selectors in front of the filename if any, replacing dots in them by slashes
+        // so that print.a4 becomes print/a4/
+        if(selectors != null && selectors.length() > 0) {
+            sb.append(selectors.toLowerCase().replace('.','/'));
+            sb.append('/');
+        }
+        
+        // filename:
+        if(methodName==null || methodName.length() == 0) {
+            sb.append("NO_METHOD");
+            
+        } else if(HttpConstants.METHOD_GET.equalsIgnoreCase(methodName)) {
+            // for the GET method, use the simplified content-type, lowercased,
+            // as the filename.
+            // TODO: how to handle HEAD?
+            if(contentType == null || contentType.length() == 0) {
+                sb.append("NO_CONTENT_TYPE");
+            } else {
+                // keep only what follows slash in the content-type
+                final String [] splitContentType = contentType.split("/");
+                sb.append(splitContentType[splitContentType.length - 1].toLowerCase());
+            }
+            
+        } else {
+            // for other methods use the method name
+            sb.append(methodName.toUpperCase());
+        }
+        
+        // extension: use desired script extension 
+        sb.append(".");
+        if(scriptExtension == null || scriptExtension.length()==0) {
+            sb.append("NO_EXT");
+        } else {
+            sb.append(scriptExtension.toLowerCase());
+        }
+        return sb.toString();
+    }
+    
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Modified: 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=585188&r1=585187&r2=585188&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html (original)
+++ incubator/sling/whiteboard/microsling/src/main/webapp/server-side-javascript.html Tue
Oct 16 09:27:25 2007
@@ -37,7 +37,7 @@
   	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 one of the scripts shown
-  	below under <em>/sling/scripts/microsling/example/</em>, using the filename
shown in the example comments.
+  	below under <em>/sling/scripts/microsling/example/</em>, using the filenames
shown in comments in the scripts below.
   </p>
   <p>
   	Content nodes created with the <a href="content-creation-forms.html">Content creation
forms</a>
@@ -45,8 +45,8 @@
   </p>
   <p>
     As for the Velocity templates, other output formats can be generated, for example by
renaming the ESP template 
-    to <em>get.text_plain.esp</em>, modifying it to output plain text and using
a <em>.txt</em> extension
-    in the request.
+    to <em>plain.esp</em> (as the Content-type for text is <em>text/plain</em>),
modifying it to output plain text 
+    and using a <em>.txt</em> extension in the request.
   </p>
   
   <h2>ECMAscript Server Pages (ESP)</h2>
@@ -70,7 +70,7 @@
   </p>
   <h3>ESP template example</h3>
     <pre>
-&lt;%-- microsling ESP template example, store this as get.text_html.esp --%>
+&lt;%-- microsling ESP template example, store this as html.esp --%>
 &lt;html>
 &lt;body>
 &lt;p>This page is generated from an ESP template!&lt;/p>
@@ -98,7 +98,7 @@
   
   <h3>Javascript example</h3>
     <pre>
-// store this as get.text_html.js in the repository    
+// store this as html.js in the repository    
 // TODO this is not very useful, rewrite this example
 out.println("&lt;html>&lt;body>");
 out.println("&lt;p>This page is generated from a rhino script&lt;/p>");

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=585188&r1=585187&r2=585188&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
09:27:25 2007
@@ -19,8 +19,8 @@
   </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.text_html.vlt</em>.

-    Doing so will cause that script to be used
+  	repository under <em> /sling/scripts/microsling/example/html.vlt</em> . 
+    Doing so will cause microsling to use that script
   	to render nodes having <em>slingResourceType=microsling/example</em>. 
   </p>
   <p>
@@ -28,13 +28,16 @@
     and node and display it.
   </p>
   <p>
-  	 The name <em>get.text_html.vlt</em> indicates that this script is meant to
process HTTP GET requests with
-  	 expect a <em>text/html</em> response.
-  	 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. 
+  	 The name <em>html.vlt</em> indicates that this script is meant to process
HTTP GET requests with
+  	 expect a <em>text/html</em> response. 
   </p>
   <p>
-    To generate XML instead, rename the script to <em>get.text_xml.vlt</em>,
modify it to generate
+  	For other HTTP methods the script would have the method name,
+  	 for example POST.vlt for a POST. But 
+  	 GET is probably the only request method that makes sense to process with Velocity templates.
+  </p>
+  <p>
+    To generate XML instead, rename the script to <em>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>
@@ -55,8 +58,6 @@
 
 &lt;/body>
 &lt;/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

Added: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java?rev=585188&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
(added)
+++ incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
Tue Oct 16 09:27:25 2007
@@ -0,0 +1,93 @@
+/*
+ * 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.helpers;
+
+import junit.framework.TestCase;
+
+/** Test the ScriptFilenameBuilder */
+public class ScriptFilenameBuilderTest extends TestCase {
+    
+    private ScriptFilenameBuilder builder;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        builder = new ScriptFilenameBuilder();
+    }
+
+    public void testNull() {
+        assertEquals("NO_METHOD.NO_EXT",builder.buildScriptFilename(null,null,null,null));
+    }
+    
+    public void testSimpleGet() {
+        assertEquals("html.js",builder.buildScriptFilename("GET",null,"text/html","js"));
+    }
+    
+    public void testGetAndNulls() {
+        assertEquals("NO_CONTENT_TYPE.NO_EXT",builder.buildScriptFilename("GET",null,null,null));
+    }
+    
+    public void testSimpleHead() {
+        assertEquals("HEAD.js",builder.buildScriptFilename("HEAD",null,"text/html","js"));
+    }
+    
+    public void testGetOneSelector() {
+        assertEquals("print/html.js",builder.buildScriptFilename("GET","print","text/html","js"));
+    }
+    
+    public void testGetTwoSelectors() {
+        assertEquals("print/a4/xml.vlt",builder.buildScriptFilename("GET","print.a4","text/xml","vlt"));
+    }
+    
+    public void testSimplePost() {
+        assertEquals("POST.js",builder.buildScriptFilename("POST",null,"text/html","js"));
+    }
+    
+    public void testSimplePut() {
+        assertEquals("PUT.js",builder.buildScriptFilename("PUT",null,"text/html","js"));
+    }
+    
+    public void testSimpleDelete() {
+        assertEquals("DELETE.js",builder.buildScriptFilename("DELETE",null,"text/html","js"));
+    }
+    
+    public void testGetTextMimeType() {
+        assertEquals("plain.js",builder.buildScriptFilename("GET",null,"text/plain","js"));
+    }
+    
+    public void testGetWhateverMimeType() {
+        assertEquals("foo.js",builder.buildScriptFilename("GET",null,"whatever/Foo","js"));
+    }
+    
+    public void testGetNoSlashMimeType() {
+        assertEquals("bar.js",builder.buildScriptFilename("GET",null,"bar","js"));
+    }
+    
+    public void testCaseCleanupOne() {
+        assertEquals("POST.js",builder.buildScriptFilename("posT",null,"text/html","jS"));
+    }
+    
+    public void testCaseCleanupTwo() {
+        assertEquals("html.js",builder.buildScriptFilename("get",null,"text/HTML","JS"));
+    }
+    
+    public void testCaseCleanupThree() {
+        assertEquals("print/a4/html.js",builder.buildScriptFilename("get","PRInT.A4","text/HTML","JS"));
+    }
+}

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message