1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2007 Tim Pizey
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * Tim Pizey <timp At paneris.org>
42 */
43 package org.melati.template.freemarker;
44
45 import java.io.IOException;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 import javax.servlet.http.HttpServlet;
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.melati.Melati;
53 import org.melati.MelatiConfig;
54 import org.melati.template.ServletTemplateContext;
55 import org.melati.template.ServletTemplateEngine;
56 import org.melati.template.TemplateEngineException;
57 import org.melati.template.TemplateIOException;
58 import org.melati.template.velocity.MelatiBufferedVelocityWriter;
59 import org.melati.template.velocity.MelatiVelocityWriter;
60 import org.melati.util.MelatiWriter;
61
62 /**
63 * @author timp
64 * @since 7 Mar 2012
65 *
66 */
67 public class FreemarkerServletTemplateEngine extends FreemarkerTemplateEngine
68 implements ServletTemplateEngine {
69 /**
70 * The HTTP request object context key.
71 */
72 public static final String REQUEST = "Request";
73
74 /**
75 * The HTTP response object context key.
76 */
77 public static final String RESPONSE = "Response";
78
79 /** Mimicking the $Form behaviour of Webmacro. */
80 public static final String FORM = "Form";
81
82 /**
83 * Constructor.
84 */
85 public FreemarkerServletTemplateEngine() {
86 super();
87 }
88
89 /**
90 * Construct a new Engine for use in a servlet environment. For Velocity this
91 * is no different to initialisation outside of a servlets environment.
92 *
93 * @param melatiConfig
94 * a {@link MelatiConfig}
95 * @param servlet
96 * the servlet we are within
97 * @throws TemplateEngineException
98 * if any problem occurs with the engine
99 * @see org.melati.template.ServletTemplateEngine#init(org.melati.MelatiConfig, javax.servlet.http.HttpServlet)
100 */
101 @Override
102 public void init(MelatiConfig melatiConfig, HttpServlet servlet)
103 throws TemplateEngineException {
104 init(melatiConfig);
105 }
106
107 /**
108 * @param response
109 * the <code>HttpServletResponse</code> that this writer will be
110 * part of
111 * @param buffered
112 * whether the writer should be buffered
113 * @return a {@link MelatiWriter} appropriate for this engine.
114 * @see org.melati.template.ServletTemplateEngine#getServletWriter(javax.servlet.http.HttpServletResponse, boolean)
115 */
116 @Override
117 public MelatiWriter getServletWriter(HttpServletResponse response,
118 boolean buffered) {
119 if (buffered) {
120 try {
121 return new MelatiBufferedVelocityWriter(response);
122 } catch (IOException e) {
123 throw new TemplateIOException(e);
124 }
125 } else {
126 try {
127 return new MelatiVelocityWriter(response);
128 } catch (IOException e) {
129 throw new TemplateIOException(e);
130 }
131 }
132 }
133
134 /**
135 * Get the template context for Velocity, with servlet specific objects added.
136 *
137 * @param melati
138 * the {@link Melati}
139 * @return a {@link ServletTemplateContext}
140 * @see org.melati.template.ServletTemplateEngine#getServletTemplateContext(org.melati.Melati)
141 */
142 @Override
143 public ServletTemplateContext getServletTemplateContext(Melati melati) throws TemplateEngineException {
144 Map<String, Object> context = new HashMap<String, Object>();
145
146 context.put(REQUEST, melati.getRequest());
147 context.put(RESPONSE, melati.getResponse());
148 return new FreemarkerServletTemplateContext(context);
149 }
150
151 }