1 5 package com.opensymphony.webwork.dispatcher; 6 7 import com.opensymphony.webwork.ServletActionContext; 8 import com.opensymphony.webwork.config.Configuration; 9 import com.opensymphony.webwork.views.JspSupportServlet; 10 import com.opensymphony.webwork.views.velocity.VelocityManager; 11 import com.opensymphony.xwork.ActionContext; 12 import com.opensymphony.xwork.ActionInvocation; 13 import com.opensymphony.xwork.util.OgnlValueStack; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.velocity.Template; 17 import org.apache.velocity.app.VelocityEngine; 18 import org.apache.velocity.context.Context; 19 20 import javax.servlet.Servlet ; 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.jsp.JspFactory ; 25 import javax.servlet.jsp.PageContext ; 26 import java.io.Writer ; 27 import java.io.OutputStreamWriter ; 28 29 30 38 public class VelocityResult extends WebWorkResultSupport { 39 41 private static final Log log = LogFactory.getLog(VelocityResult.class); 42 43 45 54 public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { 55 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 56 57 HttpServletRequest request = ServletActionContext.getRequest(); 58 HttpServletResponse response = ServletActionContext.getResponse(); 59 JspFactory jspFactory = null; 60 ServletContext servletContext = ServletActionContext.getServletContext(); 61 Servlet servlet = JspSupportServlet.jspSupportServlet; 62 63 VelocityManager.getInstance().init(servletContext); 64 65 boolean usedJspFactory = false; 66 PageContext pageContext = (PageContext ) ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT); 67 68 if (pageContext == null && servlet != null) { 69 jspFactory = JspFactory.getDefaultFactory(); 70 pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true); 71 ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT, pageContext); 72 usedJspFactory = true; 73 } 74 75 try { 76 String encoding = getEncoding(finalLocation); 77 String contentType = getContentType(finalLocation); 78 79 if (encoding != null) { 80 contentType = contentType + ";charset=" + encoding; 81 } 82 83 VelocityManager velocityManager = VelocityManager.getInstance(); 84 Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding); 85 86 Context context = createContext(velocityManager, stack, request, response, finalLocation); 87 Writer writer = new OutputStreamWriter (response.getOutputStream(), encoding); 88 89 90 response.setContentType(contentType); 91 92 t.merge(context, writer); 93 94 if (usedJspFactory) { 95 writer.flush(); 96 } 97 } catch (Exception e) { 98 log.error("Unable to render Velocity Template, '" + finalLocation + "'", e); 99 throw e; 100 } finally { 101 if (usedJspFactory) { 102 jspFactory.releasePageContext(pageContext); 103 } 104 } 105 106 return; 107 } 108 109 116 protected String getContentType(String templateLocation) { 117 return "text/html"; 118 } 119 120 127 protected String getEncoding(String templateLocation) { 128 String encoding = (String ) Configuration.get("webwork.i18n.encoding"); 129 if (encoding == null) { 130 encoding = System.getProperty("file.encoding"); 131 } 132 if (encoding == null) { 133 encoding = "UTF-8"; 134 } 135 return encoding; 136 } 137 138 150 protected Template getTemplate(OgnlValueStack stack, VelocityEngine velocity, ActionInvocation invocation, String location, String encoding) throws Exception { 151 if (!location.startsWith("/")) { 152 location = invocation.getProxy().getNamespace() + "/" + location; 153 } 154 155 Template template = velocity.getTemplate(location, encoding); 156 157 return template; 158 } 159 160 168 protected Context createContext(VelocityManager velocityManager, OgnlValueStack stack, HttpServletRequest request, HttpServletResponse response, String location) { 169 return velocityManager.createContext(stack, request, response); 170 } 171 } 172 | Popular Tags |