Commits

Anonymous committed 84f27cf

Fixing WW-1159, which ensures that when using Velocity templates with Sitemesh,
any WW tags that also use a template engine won't break due to incorrect setup
of the context
Issue number: 1159

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2542573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (3)

src/java/com/opensymphony/webwork/sitemesh/FreeMarkerPageFilter.java

 import com.opensymphony.module.sitemesh.HTMLPage;
 import com.opensymphony.module.sitemesh.filter.PageFilter;
 import com.opensymphony.webwork.views.freemarker.FreemarkerManager;
-import com.opensymphony.webwork.ServletActionContext;
-import com.opensymphony.webwork.dispatcher.DispatcherUtils;
 import com.opensymphony.xwork.*;
-import com.opensymphony.xwork.interceptor.PreResultListener;
-import com.opensymphony.xwork.util.OgnlValueStack;
 import freemarker.template.Configuration;
 import freemarker.template.SimpleHash;
 import freemarker.template.Template;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import javax.servlet.FilterConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 /**
  * @author patrick
  */
-public class FreeMarkerPageFilter extends PageFilter {
+public class FreeMarkerPageFilter extends TemplatePageFilter {
     private static final Log LOG = LogFactory.getLog(FreeMarkerPageFilter.class);
 
-    private FilterConfig filterConfig;
-
-    public void init(FilterConfig filterConfig) {
-        super.init(filterConfig);
-        this.filterConfig = filterConfig;
-    }
-
     protected void applyDecorator(Page page, Decorator decorator,
-                                  HttpServletRequest req, HttpServletResponse res)
+                                  HttpServletRequest req, HttpServletResponse res,
+                                  ServletContext servletContext, ActionContext ctx)
             throws ServletException, IOException {
         try {
             FreemarkerManager fmm = FreemarkerManager.getInstance();
-            ServletContext servletContext = filterConfig.getServletContext();
-            ActionContext ctx = ServletActionContext.getActionContext(req);
-            if (ctx == null) {
-                // ok, one isn't associated with the request, so let's get a ThreadLocal one (which will create one if needed)
-                OgnlValueStack vs = new OgnlValueStack();
-                vs.getContext().putAll(DispatcherUtils.getInstance().createContextMap(req, res, null, servletContext));
-                ctx = new ActionContext(vs.getContext());
-                if (ctx.getActionInvocation() == null) {
-                    // put in a dummy ActionSupport so basic functionality still works
-                    ActionSupport action = new ActionSupport();
-                    vs.push(action);
-                    ctx.setActionInvocation(new DummyActionInvocation(action));
-                }
-            }
 
             // get the configuration and template
             Configuration config = fmm.getConfiguration(servletContext);
             throw new ServletException(msg, e);
         }
     }
-
+    
     /**
      * Returns the locale used for the {@link Configuration#getTemplate(String, Locale)} call. The base implementation
      * simply returns the locale setting of the action (assuming the action implements {@link LocaleProvider}) or, if
             return configuration.getLocale();
         }
     }
-    
-    static class DummyActionInvocation implements ActionInvocation {
-        ActionSupport action;
-
-        public DummyActionInvocation(ActionSupport action) {
-            this.action = action;
-        }
-
-        public Object getAction() {
-            return action;
-        }
-
-        public boolean isExecuted() {
-            return false;
-        }
-
-        public ActionContext getInvocationContext() {
-            return null;
-        }
-
-        public ActionProxy getProxy() {
-            return null;
-        }
-
-        public Result getResult() throws Exception {
-            return null;
-        }
-
-        public String getResultCode() {
-            return null;
-        }
-
-        public void setResultCode(String resultCode) {
-        }
-
-        public OgnlValueStack getStack() {
-            return null;
-        }
-
-        public void addPreResultListener(PreResultListener listener) {
-        }
-
-        public String invoke() throws Exception {
-            return null;
-        }
-
-        public String invokeActionOnly() throws Exception {
-            return null;
-        }
-    }
+ 
 }

src/java/com/opensymphony/webwork/sitemesh/TemplatePageFilter.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+
+package com.opensymphony.webwork.sitemesh;
+
+import com.opensymphony.module.sitemesh.Decorator;
+import com.opensymphony.module.sitemesh.Page;
+import com.opensymphony.module.sitemesh.filter.PageFilter;
+import com.opensymphony.webwork.ServletActionContext;
+import com.opensymphony.webwork.dispatcher.DispatcherUtils;
+import com.opensymphony.webwork.WebWorkConstants;
+import com.opensymphony.webwork.config.Configuration;
+
+import com.opensymphony.xwork.*;
+import com.opensymphony.xwork.interceptor.PreResultListener;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author patrick
+ */
+public abstract class TemplatePageFilter extends PageFilter {
+    
+    private FilterConfig filterConfig;
+
+    public void init(FilterConfig filterConfig) {
+        super.init(filterConfig);
+        this.filterConfig = filterConfig;
+    }
+
+    protected abstract void applyDecorator(Page page, Decorator decorator,
+                                  HttpServletRequest req, HttpServletResponse res,
+                                  ServletContext servletContext, ActionContext ctx)
+            throws ServletException, IOException;
+    
+    protected void applyDecorator(Page page, Decorator decorator,
+                                  HttpServletRequest req, HttpServletResponse res)
+            throws ServletException, IOException {
+            
+        ServletContext servletContext = filterConfig.getServletContext();
+        ActionContext ctx = ServletActionContext.getActionContext(req);
+        if (ctx == null) {
+            // ok, one isn't associated with the request, so let's get a ThreadLocal one (which will create one if needed)
+            OgnlValueStack vs = new OgnlValueStack();
+            vs.getContext().putAll(DispatcherUtils.getInstance().createContextMap(req, res, null, servletContext));
+            ctx = new ActionContext(vs.getContext());
+            if (ctx.getActionInvocation() == null) {
+                // put in a dummy ActionSupport so basic functionality still works
+                ActionSupport action = new ActionSupport();
+                vs.push(action);
+                ctx.setActionInvocation(new DummyActionInvocation(action));
+            }
+        }
+
+        applyDecorator(page, decorator, req, res, servletContext, ctx);
+   }
+
+   
+    protected String getEncoding() {
+        String encoding = (String) Configuration.get(WebWorkConstants.WEBWORK_I18N_ENCODING);
+        if (encoding == null) {
+            encoding = System.getProperty("file.encoding");
+        }
+        if (encoding == null) {
+            encoding = "UTF-8";
+        }
+        return encoding;
+    }
+
+    static class DummyActionInvocation implements ActionInvocation {
+        ActionSupport action;
+
+        public DummyActionInvocation(ActionSupport action) {
+            this.action = action;
+        }
+
+        public Object getAction() {
+            return action;
+        }
+
+        public boolean isExecuted() {
+            return false;
+        }
+
+        public ActionContext getInvocationContext() {
+            return null;
+        }
+
+        public ActionProxy getProxy() {
+            return null;
+        }
+
+        public Result getResult() throws Exception {
+            return null;
+        }
+
+        public String getResultCode() {
+            return null;
+        }
+
+        public void setResultCode(String resultCode) {
+        }
+
+        public OgnlValueStack getStack() {
+            return null;
+        }
+
+        public void addPreResultListener(PreResultListener listener) {
+        }
+
+        public String invoke() throws Exception {
+            return null;
+        }
+
+        public String invokeActionOnly() throws Exception {
+            return null;
+        }
+    }
+}

src/java/com/opensymphony/webwork/sitemesh/VelocityPageFilter.java

 import com.opensymphony.module.sitemesh.Page;
 import com.opensymphony.module.sitemesh.HTMLPage;
 import com.opensymphony.module.sitemesh.filter.PageFilter;
-import com.opensymphony.webwork.config.Configuration;
 import com.opensymphony.webwork.views.velocity.VelocityManager;
-import com.opensymphony.webwork.WebWorkConstants;
 import com.opensymphony.xwork.ActionContext;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.velocity.Template;
 import org.apache.velocity.context.Context;
 
-import javax.servlet.FilterConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
 import java.io.PrintWriter;
 
+
 /**
  * User: plightbo
  * Date: Aug 31, 2005
  * Time: 10:49:51 PM
  */
-public class VelocityPageFilter extends PageFilter {
+public class VelocityPageFilter extends TemplatePageFilter {
     private static final Log LOG = LogFactory.getLog(VelocityPageFilter.class);
 
-    private FilterConfig filterConfig;
-
-    public void init(FilterConfig filterConfig) {
-        super.init(filterConfig);
-        this.filterConfig = filterConfig;
-    }
 
     protected void applyDecorator(Page page, Decorator decorator,
-                                  HttpServletRequest req, HttpServletResponse res)
+                                  HttpServletRequest req, HttpServletResponse res,
+                                  ServletContext servletContext, ActionContext ctx)
             throws ServletException, IOException {
         try {
             VelocityManager vm = VelocityManager.getInstance();
-            ServletContext servletContext = filterConfig.getServletContext();
-            ActionContext ctx = ActionContext.getContext();
 
             // init (if needed)
             vm.init(servletContext);
             throw new ServletException(msg, e);
         }
     }
-
-    protected String getEncoding() {
-        String encoding = (String) Configuration.get(WebWorkConstants.WEBWORK_I18N_ENCODING);
-        if (encoding == null) {
-            encoding = System.getProperty("file.encoding");
-        }
-        if (encoding == null) {
-            encoding = "UTF-8";
-        }
-        return encoding;
-    }
-
 }