1 25 package org.ofbiz.webapp.ftl; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.Map ; 30 31 import javax.servlet.ServletContext ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.ofbiz.webapp.taglib.ContentUrlTag; 36 37 import freemarker.core.Environment; 38 import freemarker.ext.beans.BeanModel; 39 import freemarker.template.TemplateModelException; 40 import freemarker.template.TemplateTransformModel; 41 42 49 public class OfbizContentTransform implements TemplateTransformModel { 50 51 public Writer getWriter(final Writer out, Map args) { 52 final StringBuffer buf = new StringBuffer (); 53 return new Writer (out) { 54 public void write(char cbuf[], int off, int len) { 55 buf.append(cbuf, off, len); 56 } 57 58 public void flush() throws IOException { 59 out.flush(); 60 } 61 62 public void close() throws IOException { 63 try { 64 Environment env = Environment.getCurrentEnvironment(); 65 BeanModel req = (BeanModel)env.getVariable("request"); 66 BeanModel res = (BeanModel) env.getVariable("response"); 67 HttpServletRequest request = (HttpServletRequest ) req.getWrappedObject(); 68 HttpServletResponse response = (HttpServletResponse ) res.getWrappedObject(); 69 ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 70 71 StringBuffer newURL = new StringBuffer (); 73 ContentUrlTag.appendContentPrefix(request, newURL); 74 newURL.append(buf.toString()); 75 out.write(newURL.toString()); 76 } catch (TemplateModelException e) { 77 throw new IOException (e.getMessage()); 78 } 79 } 80 }; 81 } 82 } | Popular Tags |