svn commit: r920370 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r920370 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

lektran
Author: lektran
Date: Mon Mar  8 16:05:14 2010
New Revision: 920370

URL: http://svn.apache.org/viewvc?rev=920370&view=rev
Log:
Added an OFBiz specific FreeMarker TemplateExceptionHandler, ensures that any stack trace error messages are properly encoded before being dumped to the browser (or anywhere else).

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=920370&r1=920369&r2=920370&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java Mon Mar  8 16:05:14 2010
@@ -21,8 +21,10 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringReader;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -50,6 +52,7 @@
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.StringUtil.SimpleEncoder;
 import org.ofbiz.base.util.cache.UtilCache;
 
 import freemarker.cache.TemplateLoader;
@@ -61,6 +64,7 @@
 import freemarker.template.SimpleScalar;
 import freemarker.template.Template;
 import freemarker.template.TemplateException;
+import freemarker.template.TemplateExceptionHandler;
 import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 
@@ -85,6 +89,7 @@
         newConfig.setSharedVariable("StringUtil", new BeanModel(new StringUtil(), wrapper));
         newConfig.setTemplateLoader(new FlexibleTemplateLoader());
         newConfig.setAutoImports(UtilProperties.getProperties("freemarkerImports"));
+        newConfig.setTemplateExceptionHandler(new FreeMarkerWorker.OFBizTemplateExceptionHandler());
         try {
             newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
             newConfig.setSetting("number_format", "0.##########");
@@ -621,4 +626,30 @@
             // do nothing
         }
     }
+
+    /**
+     * OFBiz specific TemplateExceptionHandler.  Sanitizes any error messages present in
+     * the stack trace prior to printing to the output writer.
+     *
+     */
+    static class OFBizTemplateExceptionHandler implements TemplateExceptionHandler {
+
+        @Override
+        public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
+            StringWriter tempWriter = new StringWriter();
+            PrintWriter pw = new PrintWriter(tempWriter, true);
+            te.printStackTrace(pw);
+            String stackTrace = tempWriter.toString();
+
+            StringUtil.SimpleEncoder simpleEncoder = (SimpleEncoder) FreeMarkerWorker.getWrappedObject("simpleEncoder", env);
+            if (simpleEncoder != null) {
+                stackTrace = simpleEncoder.encode(stackTrace);
+            }
+            try {
+                out.write(stackTrace);
+            } catch (IOException e) {
+                Debug.logError(e, module);
+            }
+        }
+    }
 }