1 5 9 package com.opensymphony.webwork.views.freemarker; 10 11 import com.opensymphony.webwork.ServletActionContext; 12 import com.opensymphony.xwork.util.OgnlValueStack; 13 import freemarker.template.*; 14 15 import javax.servlet.ServletContext ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServlet ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.util.Locale ; 23 24 25 29 public class FreemarkerServlet extends HttpServlet { 30 32 protected Configuration configuration; 33 34 36 39 public FreemarkerServlet() { 40 super(); 41 } 42 43 45 final public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 46 request.setAttribute("webwork.freemarker.servlet", this); 47 process(request, response); 48 } 49 50 final public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 51 request.setAttribute("webwork.freemarker.servlet", this); 52 process(request, response); 53 } 54 55 public void init() throws ServletException { 56 try { 57 configuration = createConfiguration(); 58 } catch (TemplateException e) { 59 throw new ServletException ("could not configure Freemarker", e); 60 } 61 } 62 63 74 protected ObjectWrapper getObjectWrapper() { 75 return configuration.getObjectWrapper(); 76 } 77 78 protected Configuration createConfiguration() throws TemplateException { 79 return FreemarkerManager.getInstance().getConfigruation(getServletContext()); 80 } 81 82 protected TemplateModel createModel(ObjectWrapper wrapper, ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws TemplateModelException { 83 84 OgnlValueStack stack = ServletActionContext.getContext().getValueStack(); 85 Object action = null; 86 if (ServletActionContext.getContext().getActionInvocation() != null) { 87 action = ServletActionContext.getContext().getActionInvocation().getAction(); 88 } 89 TemplateModel model = FreemarkerManager.getInstance().buildTemplateModel(stack, action, servletContext, request, response, wrapper); 90 return model; 91 } 92 93 100 protected Locale deduceLocale(String templatePath, HttpServletRequest request, HttpServletResponse response) { 101 return configuration.getLocale(); 102 } 103 104 115 protected void postTemplateProcess(HttpServletRequest request, HttpServletResponse response, Template template, TemplateModel data) throws ServletException , IOException { 116 } 117 118 135 148 protected boolean preTemplateProcess(HttpServletRequest request, HttpServletResponse response, Template template, TemplateModel data) throws ServletException , IOException { 149 return true; 150 } 151 152 162 protected String requestUrlToTemplatePath(HttpServletRequest request) { 163 String includeServletPath = (String ) request.getAttribute("javax.servlet.include.servlet_path"); 165 166 if (includeServletPath != null) { 167 String includePathInfo = (String ) request.getAttribute("javax.servlet.include.path_info"); 170 171 return (includePathInfo == null) ? includeServletPath : includePathInfo; 172 } 173 174 String path = request.getPathInfo(); 178 179 if (path != null) { 180 return path; 181 } 182 183 path = request.getServletPath(); 184 185 if (path != null) { 186 return path; 187 } 188 189 return ""; 191 } 192 193 private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 194 String path = requestUrlToTemplatePath(request); 195 196 Template template = null; 197 198 try { 199 template = configuration.getTemplate(path, deduceLocale(path, request, response)); 200 } catch (FileNotFoundException e) { 201 response.sendError(HttpServletResponse.SC_NOT_FOUND); 202 203 return; 204 } 205 206 Object attrContentType = template.getCustomAttribute("content_type"); 207 208 if (attrContentType != null) { 209 response.setContentType(attrContentType.toString()); 210 } else { 211 response.setContentType("text/html; charset=" + template.getEncoding()); 212 } 213 214 ServletContext servletContext = getServletContext(); 217 218 try { 219 TemplateModel model = createModel(getObjectWrapper(), servletContext, request, response); 220 221 if (preTemplateProcess(request, response, template, model)) { 223 try { 224 template.process(model, response.getWriter()); 226 } finally { 227 postTemplateProcess(request, response, template, model); 229 } 230 } 231 } catch (TemplateException te) { 232 if ((configuration.getTemplateExceptionHandler() != freemarker.template.TemplateExceptionHandler.HTML_DEBUG_HANDLER) && (configuration.getTemplateExceptionHandler() != freemarker.template.TemplateExceptionHandler.DEBUG_HANDLER)) { 235 throw new ServletException (te); 236 } 237 } 238 } 239 } 240 | Popular Tags |