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 2012/12/23 22:41:21 UTC

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

Author: andy
Date: Sun Dec 23 21:41:21 2012
New Revision: 1425533

URL: http://svn.apache.org/viewvc?rev=1425533&view=rev
Log:
Factor out velocity handling.

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

Added: 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=1425533&view=auto
==============================================================================
--- jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java (added)
+++ jena/trunk/jena-fuseki/src/main/java/org/apache/jena/fuseki/servlets/SimpleVelocity.java Sun Dec 23 21:41:21 2012
@@ -0,0 +1,61 @@
+/**
+ * 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.jena.fuseki.servlets;
+
+import java.io.IOException ;
+import java.io.Writer ;
+import java.util.Map ;
+
+import org.apache.velocity.Template ;
+import org.apache.velocity.VelocityContext ;
+import org.apache.velocity.app.VelocityEngine ;
+import org.apache.velocity.exception.MethodInvocationException ;
+import org.apache.velocity.exception.ParseErrorException ;
+import org.apache.velocity.exception.ResourceNotFoundException ;
+import org.apache.velocity.runtime.RuntimeConstants ;
+import org.apache.velocity.runtime.log.LogChute ;
+import org.apache.velocity.runtime.log.NullLogChute ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+public class SimpleVelocity
+{
+    private static LogChute velocityLogChute = new NullLogChute() ;
+    private static Logger velocityLog = LoggerFactory.getLogger("Velocity");
+
+    public static void process(String base, String path, Writer out, Map<String, Object> params)
+    {
+        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() ;
+        VelocityContext context = new VelocityContext(params) ;
+        try {
+            Template temp = velocity.getTemplate(path) ;
+            temp.merge(context, out) ;
+            out.flush();
+        } 
+        catch (ResourceNotFoundException ex) { velocityLog.error("Resource not found: "+ex.getMessage()) ; }
+        catch (ParseErrorException ex)       { velocityLog.error("Parse error ("+path+") : "+ex.getMessage()) ; }
+        catch (MethodInvocationException ex) { velocityLog.error("Method invocation exception ("+path+") : "+ex.getMessage()) ; }
+        catch (IOException ex)               { velocityLog.warn("IOException", ex) ; }
+    }
+}

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=1425533&r1=1425532&r2=1425533&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 Sun Dec 23 21:41:21 2012
@@ -26,12 +26,7 @@ import javax.servlet.http.HttpServlet ;
 import javax.servlet.http.HttpServletRequest ;
 import javax.servlet.http.HttpServletResponse ;
 
-import org.apache.velocity.Template ;
-import org.apache.velocity.VelocityContext ;
 import org.apache.velocity.app.VelocityEngine ;
-import org.apache.velocity.exception.MethodInvocationException ;
-import org.apache.velocity.exception.ParseErrorException ;
-import org.apache.velocity.exception.ResourceNotFoundException ;
 import org.apache.velocity.runtime.RuntimeConstants ;
 import org.apache.velocity.runtime.RuntimeServices ;
 import org.apache.velocity.runtime.log.LogChute ;
@@ -89,22 +84,16 @@ public class SimpleVelocityServlet exten
 
     private void process(HttpServletRequest req, HttpServletResponse resp)
     { 
-        VelocityContext context = new VelocityContext(datamodel) ;
+        try { 
+        resp.setContentType("text/html") ;
+        resp.setCharacterEncoding("UTF-8") ;
+        Writer out = resp.getWriter() ;
         String path = path(req) ;
-        try
+        SimpleVelocity.process(docbase, path, out, datamodel) ;
+        } catch (IOException ex)
         {
-            Template temp = velocity.getTemplate(path) ;        // ResourceNotFoundException, ParseErrorException
-            context.put("request", req) ;
-            resp.setContentType("text/html") ;
-            resp.setCharacterEncoding("UTF-8") ;
-            Writer out = resp.getWriter() ;
-            temp.merge(context, out);                           // ResourceNotFoundException, ParseErrorException, MethodInvocationException
-            out.flush();
+            vlog.warn("IOException", ex) ;
         }
-        catch (ResourceNotFoundException ex)    { vlog.error("Resource not found: "+ex.getMessage()) ; }
-        catch (ParseErrorException ex)          { vlog.error("Parse error ("+path+") : "+ex.getMessage()) ; }
-        catch (MethodInvocationException ex)    { vlog.error("Method invocation exception ("+path+") : "+ex.getMessage()) ; }
-        catch (IOException ex)                  { vlog.warn ("IOException", ex) ; }
     }
     
     private String path(HttpServletRequest request)
@@ -119,7 +108,7 @@ public class SimpleVelocityServlet exten
     @Override
     public String getServletInfo()
     {
-        return "Lightweight FreeMarker Servlet";
+        return "Lightweight Velocity Servlet";
     }
     
     /** Velocity logger to SLF4J */