You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/01/15 15:22:43 UTC

svn commit: r1433429 - in /jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets: SimpleVelocity.java SimpleVelocityServlet.java

Author: andy
Date: Tue Jan 15 14:22:08 2013
New Revision: 1433429

URL: http://svn.apache.org/viewvc?rev=1433429&view=rev
Log:
Fix: handling velocity set up did not include the HTTP request object.

Modified:
    jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java
    jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocityServlet.java

Modified: jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java?rev=1433429&r1=1433428&r2=1433429&view=diff
==============================================================================
--- jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java (original)
+++ jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java Tue Jan 15 14:22:08 2013
@@ -39,20 +39,21 @@ public class SimpleVelocity
     private static LogChute velocityLogChute = new NullLogChute() ;
     private static Logger velocityLog = LoggerFactory.getLogger("Velocity");
 
+    /** Process a template */
     public static void process(String base, String path, Writer out, Map<String, Object> params)
     {
+        process(base, path, out, createContext(params)) ;
+    }
+    
+    /** Process a template */
+    public static void process(String base, String path, Writer out, VelocityContext context)
+    {
         VelocityEngine velocity = new VelocityEngine() ;
         // Turn off logging - catch exceptions and log ourselves
         velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute) ;
         velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8") ;
         velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base) ;
         velocity.init() ;
-        
-        // Velocity requires a mutable map.
-        VelocityContext context = new VelocityContext() ;
-        for ( Map.Entry<String, Object> e : params.entrySet() )
-            context.put(e.getKey(), e.getValue()) ;
-        
         try {
             Template temp = velocity.getTemplate(path) ;
             temp.merge(context, out) ;
@@ -63,4 +64,15 @@ public class SimpleVelocity
         catch (MethodInvocationException ex) { velocityLog.error("Method invocation exception ("+path+") : "+ex.getMessage()) ; }
         catch (IOException ex)               { velocityLog.warn("IOException", ex) ; }
     }
+    
+    public static VelocityContext createContext(Map<String, Object> params)
+    {
+        // Velocity requires a mutable map.
+        // Scala leads to immutable maps ... be safe and copy.
+        VelocityContext context = new VelocityContext() ;
+        for ( Map.Entry<String, Object> e : params.entrySet() )
+            context.put(e.getKey(), e.getValue()) ;
+        return context ;
+    }
+    
 }

Modified: jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocityServlet.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocityServlet.java?rev=1433429&r1=1433428&r2=1433429&view=diff
==============================================================================
--- jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocityServlet.java (original)
+++ jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocityServlet.java Tue Jan 15 14:22:08 2013
@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServlet ;
 import javax.servlet.http.HttpServletRequest ;
 import javax.servlet.http.HttpServletResponse ;
 
+import org.apache.velocity.VelocityContext ;
 import org.apache.velocity.app.VelocityEngine ;
 import org.apache.velocity.runtime.RuntimeConstants ;
 import org.apache.velocity.runtime.RuntimeServices ;
@@ -83,13 +84,16 @@ public class SimpleVelocityServlet exten
     }
 
     private void process(HttpServletRequest req, HttpServletResponse resp)
-    { 
-        try { 
-        resp.setContentType("text/html") ;
-        resp.setCharacterEncoding("UTF-8") ;
-        Writer out = resp.getWriter() ;
-        String path = path(req) ;
-        SimpleVelocity.process(docbase, path, out, datamodel) ;
+    {
+        try
+        {
+            resp.setContentType("text/html") ;
+            resp.setCharacterEncoding("UTF-8") ;
+            Writer out = resp.getWriter() ;
+            String path = path(req) ;
+            VelocityContext vc = SimpleVelocity.createContext(datamodel) ;
+            vc.put("request", req) ;
+            SimpleVelocity.process(docbase, path, out, vc) ;
         } catch (IOException ex)
         {
             vlog.warn("IOException", ex) ;