incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r590031 - in /incubator/sling/whiteboard/microsling: ./ src/main/java/org/apache/sling/microsling/scripting/ src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/
Date Tue, 30 Oct 2007 10:57:02 GMT
Author: bdelacretaz
Date: Tue Oct 30 03:56:59 2007
New Revision: 590031

URL: http://svn.apache.org/viewvc?rev=590031&view=rev
Log:
SLING-87 - Freemarker Scripting Support, work in progress

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/FreemarkerScriptEngine.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/pom.xml
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java

Modified: incubator/sling/whiteboard/microsling/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/pom.xml?rev=590031&r1=590030&r2=590031&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/pom.xml (original)
+++ incubator/sling/whiteboard/microsling/pom.xml Tue Oct 30 03:56:59 2007
@@ -93,6 +93,11 @@
       <version>1.6R7</version>
     </dependency>
     <dependency>
+        <groupId>freemarker</groupId>
+        <artifactId>freemarker</artifactId>
+        <version>2.3.6</version>
+    </dependency>
+    <dependency>
       <groupId>commons-httpclient</groupId>
       <artifactId>commons-httpclient</artifactId>
       <version>3.1</version>

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=590031&r1=590030&r2=590031&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 30 03:56:59 2007
@@ -38,6 +38,7 @@
 import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.SlingRequestContext;
 import org.apache.sling.microsling.api.exceptions.SlingException;
+import org.apache.sling.microsling.scripting.engines.freemarker.FreemarkerScriptEngine;
 import org.apache.sling.microsling.scripting.engines.rhino.RhinoJavasSriptEngine;
 import org.apache.sling.microsling.scripting.engines.velocity.VelocityTemplatesScriptEngine;
 import org.apache.sling.microsling.scripting.helpers.ScriptFilenameBuilder;
@@ -75,6 +76,7 @@
         scriptEngines = new HashMap<String, ScriptEngine>();
         addScriptEngine(new RhinoJavasSriptEngine());
         addScriptEngine(new VelocityTemplatesScriptEngine());
+        addScriptEngine(new FreemarkerScriptEngine());
     }
 
     /**

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/FreemarkerScriptEngine.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/FreemarkerScriptEngine.java?rev=590031&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/FreemarkerScriptEngine.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/engines/freemarker/FreemarkerScriptEngine.java
Tue Oct 30 03:56:59 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.engines.freemarker;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
+import org.apache.sling.microsling.api.exceptions.SlingException;
+import org.apache.sling.microsling.scripting.ScriptEngine;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Map;
+
+/**
+ * A ScriptEngine that uses {@link http://freemarker.org/ Freemarker}
+ * templates to render a Resource in HTML.
+ */
+public class FreemarkerScriptEngine implements ScriptEngine {
+
+    public final static String FREEMARKER_SCRIPT_EXTENSION = "ftl";
+    private final Configuration configuration;
+
+    public FreemarkerScriptEngine() throws SlingException {
+        configuration = new Configuration();
+    }
+
+    public String[] getExtensions() {
+        return new String[] { FREEMARKER_SCRIPT_EXTENSION };
+    }
+
+    public void eval(Reader script, Map<String, Object> props)
+            throws SlingException, IOException {
+        String scriptName = (String)props.get(FILENAME);                                
                                   
+        // ensure get method
+        HttpServletRequest request = (HttpServletRequest) props.get(REQUEST);
+        if (!"GET".equals(request.getMethod())) {
+            throw new HttpStatusCodeException(
+                HttpServletResponse.SC_METHOD_NOT_ALLOWED,
+                "Freemarker templates only support GET requests");
+        }
+
+        Template tmpl = new Template(scriptName, script, configuration);
+
+        try {
+            Writer w = ((HttpServletResponse) props.get(RESPONSE)).getWriter();
+            tmpl.process(props, w);            
+        } catch (IOException ioe) {
+            throw ioe;
+        } catch (Throwable t) {
+            throw new SlingException("Failure running Freemarker script " + scriptName, t);
+        }
+    }
+}
\ No newline at end of file

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

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



Mime
View raw message