001: /*
002: * $Id: FreeMarkerViewHandler.java,v 1.7 2003/12/23 13:50:40 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.ftl;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import javax.servlet.ServletContext;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.http.HttpSession;
036:
037: import org.ofbiz.base.util.UtilHttp;
038: import org.ofbiz.content.webapp.view.ViewHandler;
039: import org.ofbiz.content.webapp.view.ViewHandlerException;
040:
041: import freemarker.ext.beans.BeansWrapper;
042: import freemarker.ext.jsp.TaglibFactory;
043: import freemarker.ext.servlet.HttpRequestHashModel;
044: import freemarker.ext.servlet.HttpSessionHashModel;
045: import freemarker.template.Configuration;
046: import freemarker.template.SimpleHash;
047: import freemarker.template.Template;
048: import freemarker.template.TemplateException;
049: import freemarker.template.WrappingTemplateModel;
050:
051: /**
052: * FreemarkerViewHandler - Freemarker Template Engine View Handler
053: *
054: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
055: * @version $Revision: 1.7 $
056: * @since 2.1
057: */
058: public class FreeMarkerViewHandler implements ViewHandler {
059:
060: public static final String module = FreeMarkerViewHandler.class
061: .getName();
062:
063: protected ServletContext servletContext = null;
064: protected Configuration config = null;
065:
066: public void init(ServletContext context)
067: throws ViewHandlerException {
068: this .servletContext = context;
069: config = Configuration.getDefaultConfiguration();
070: //nice thought, but doesn't do auto reloading with this: config.setServletContextForTemplateLoading(context, "/");
071: try {
072: config.setDirectoryForTemplateLoading(new File(context
073: .getRealPath("/")));
074: } catch (java.io.IOException e) {
075: throw new ViewHandlerException(
076: "Could not create file for webapp root path", e);
077: }
078: WrappingTemplateModel.setDefaultObjectWrapper(BeansWrapper
079: .getDefaultInstance());
080: try {
081: config.setObjectWrapper(BeansWrapper.getDefaultInstance());
082: config.setCacheStorage(new OfbizCacheStorage("unknown"));
083: config.setSetting("datetime_format",
084: "yyyy-MM-dd HH:mm:ss.SSS");
085: } catch (TemplateException e) {
086: throw new ViewHandlerException(
087: "Freemarker TemplateException", e.getCause());
088: }
089: }
090:
091: public void render(String name, String page, String info,
092: String contentType, String encoding,
093: HttpServletRequest request, HttpServletResponse response)
094: throws ViewHandlerException {
095: if (page == null || page.length() == 0)
096: throw new ViewHandlerException("Invalid template source");
097:
098: // make the root context (data model) for freemarker
099: SimpleHash root = new SimpleHash(BeansWrapper
100: .getDefaultInstance());
101: prepOfbizRoot(root, request, response);
102:
103: // get the template
104: Template template = null;
105: try {
106: template = config.getTemplate(page, request.getLocale());
107: } catch (IOException e) {
108: throw new ViewHandlerException(
109: "Cannot open template file: " + page, e);
110: }
111: template.setObjectWrapper(BeansWrapper.getDefaultInstance());
112:
113: // process the template & flush the output
114: try {
115: template.process(root, response.getWriter(), BeansWrapper
116: .getDefaultInstance());
117: response.flushBuffer();
118: } catch (TemplateException te) {
119: throw new ViewHandlerException(
120: "Problems processing Freemarker template", te);
121: } catch (IOException ie) {
122: throw new ViewHandlerException(
123: "Problems writing to output stream", ie);
124: }
125: }
126:
127: public static void prepOfbizRoot(SimpleHash root,
128: HttpServletRequest request, HttpServletResponse response) {
129: Map rootPrep = new HashMap();
130: prepOfbizRoot(rootPrep, request, response);
131: root.putAll(rootPrep);
132: }
133:
134: public static void prepOfbizRoot(Map root,
135: HttpServletRequest request, HttpServletResponse response) {
136: ServletContext servletContext = (ServletContext) request
137: .getAttribute("servletContext");
138: HttpSession session = request.getSession();
139:
140: BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
141:
142: // add in the OFBiz objects
143: root.put("delegator", request.getAttribute("delegator"));
144: root.put("dispatcher", request.getAttribute("dispatcher"));
145: root.put("security", request.getAttribute("security"));
146: root.put("userLogin", session.getAttribute("userLogin"));
147:
148: // add the response object (for transforms) to the context as a BeanModel
149: root.put("response", response);
150:
151: // add the application object (for transforms) to the context as a BeanModel
152: root.put("application", servletContext);
153:
154: // add the servlet context -- this has been deprecated, and now requires servlet, do we really need it?
155: //root.put("applicationAttributes", new ServletContextHashModel(servletContext, BeansWrapper.getDefaultInstance()));
156:
157: // add the session object (for transforms) to the context as a BeanModel
158: root.put("session", session);
159:
160: // add the session
161: root.put("sessionAttributes", new HttpSessionHashModel(session,
162: wrapper));
163:
164: // add the request object (for transforms) to the context as a BeanModel
165: root.put("request", request);
166:
167: // add the request
168: root.put("requestAttributes", new HttpRequestHashModel(request,
169: wrapper));
170:
171: // add the request parameters -- this now uses a Map from UtilHttp
172: Map requestParameters = UtilHttp.getParameterMap(request);
173: root.put("requestParameters", requestParameters);
174:
175: // add the TabLibFactory
176: TaglibFactory JspTaglibs = new TaglibFactory(servletContext);
177: root.put("JspTaglibs", JspTaglibs);
178:
179: FreeMarkerWorker.addAllOfbizTransforms(root);
180: }
181: }
|