2 * TemplateServlet.java created on 01.09.2003
4 * Copyright (C) 2001, 2002, 2003 The Mir-coders group
6 * This file is part of Mir.
8 * Mir is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Mir is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Mir; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * In addition, as a special exception, The Mir-coders gives permission to link
23 * the code of this program with any library licensed under the Apache Software License,
24 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
25 * (or with modified versions of the above that use the same license as the above),
26 * and distribute linked combinations including the two. You must obey the
27 * GNU General Public License in all respects for all of the code used other than
28 * the above mentioned libraries. If you modify this file, you may extend this
29 * exception to your version of the file, but you are not obligated to do so.
30 * If you do not wish to do so, delete this exception statement from your version.
32 package mir.core.ui.servlet;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.Date;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
42 import java.util.Vector;
44 import javax.servlet.ServletConfig;
45 import javax.servlet.ServletContext;
46 import javax.servlet.ServletException;
47 import javax.servlet.http.HttpServlet;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
52 import mir.config.MirPropertiesConfiguration;
53 import mir.servlet.ServletModuleExc;
54 import mir.util.GeneratorDateTimeFunctions;
55 import mir.util.GeneratorExpressionFunctions;
56 import mir.util.GeneratorHTMLFunctions;
57 import mir.util.GeneratorIntegerFunctions;
58 import mir.util.GeneratorListFunctions;
59 import mir.util.GeneratorRegularExpressionFunctions;
60 import mir.util.GeneratorStringFunctions;
61 import mir.util.ResourceBundleGeneratorFunction;
62 import mir.util.StringRoutines;
63 import mircoders.servlet.ServletHelper;
65 import org.apache.struts.util.MessageResources;
70 * @version $Id: TemplateServlet.java,v 1.2 2003/09/07 16:55:00 idfx Exp $
72 public class TemplateServlet extends HttpServlet {
77 public TemplateServlet() {
84 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
86 protected final void doGet(HttpServletRequest request, HttpServletResponse response)
87 throws ServletException, IOException {
88 process(request, response);
92 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
94 protected final void doPost(HttpServletRequest request, HttpServletResponse response)
95 throws ServletException, IOException {
96 process(request, response);
100 * Processes the request and invokes the presentation
104 protected void process(HttpServletRequest request, HttpServletResponse response)
105 throws IOException, ServletException {
107 Map requestData = new HashMap();
108 Enumeration keys = request.getAttributeNames();
109 while(keys.hasMoreElements()){
110 final String key = (String)keys.nextElement();
111 requestData.put(key, request.getAttribute(key));
113 requestData.putAll(request.getParameterMap());
115 Map sessionData = new HashMap();
116 HttpSession httpSession = request.getSession();
117 keys = httpSession.getAttributeNames();
118 while(keys.hasMoreElements()){
119 final String key = (String)keys.nextElement();
120 sessionData.put(key, httpSession.getAttribute(key));
123 Map applicationData = new HashMap();
124 ServletContext servletContext = getServletContext();
125 keys = servletContext.getAttributeNames();
126 while(keys.hasMoreElements()){
127 final String key = (String)keys.nextElement();
128 applicationData.put(key, servletContext.getAttribute(key));
131 Map utilityMap = new HashMap();
132 utilityMap.put("compressWhitespace",
133 new freemarker.template.utility.CompressWhitespace());
134 utilityMap.put("encodeHTML",
135 new GeneratorHTMLFunctions.encodeHTMLGeneratorFunction());
136 utilityMap.put("encodeXML",
137 new GeneratorHTMLFunctions.encodeXMLGeneratorFunction());
138 utilityMap.put("encodeURI",
139 new GeneratorHTMLFunctions.encodeURIGeneratorFunction());
140 utilityMap.put("subString",
141 new GeneratorStringFunctions.subStringFunction());
142 utilityMap.put("subList",
143 new GeneratorListFunctions.subListFunction());
144 utilityMap.put("isOdd",
145 new GeneratorIntegerFunctions.isOddFunction());
146 utilityMap.put("increment",
147 new GeneratorIntegerFunctions.incrementFunction());
148 utilityMap.put("evaluate",
149 new GeneratorExpressionFunctions.evaluateExpressionFunction());
150 utilityMap.put("constructString",
151 new GeneratorStringFunctions.constructStructuredStringFunction());
152 utilityMap.put("escapeJDBCString",
153 new GeneratorStringFunctions.jdbcStringEscapeFunction());
154 utilityMap.put("regexpreplace",
155 new GeneratorRegularExpressionFunctions.regularExpressionReplaceFunction());
156 utilityMap.put("datetime",
157 new GeneratorDateTimeFunctions.DateTimeFunctions(
158 MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
159 utilityMap.put("encodeLink",
160 new GeneratorHTMLFunctions.encodeLinksGeneratorFunction(response));
162 Map configData = MirPropertiesConfiguration.instance().allSettings();
163 configData.put("docRoot", request.getContextPath());
164 configData.put("now", new Date());
166 //administeroperations
167 List administerOperations = new Vector();
168 String settings[] = MirPropertiesConfiguration.instance().getStringArray("Mir.Localizer.Admin.AdministerOperations");
169 if (settings!=null) {
170 for (int i = 0; i < settings.length; i++) {
171 String setting = settings[i].trim();
173 if (setting.length() > 0) {
174 List parts = StringRoutines.splitString(setting, ":");
175 if (parts.size() != 2) {
176 // logger.error("config error: " + settings[i] + ", 2 parts expected");
179 Map entry = new HashMap();
180 entry.put("name", (String) parts.get(0));
181 entry.put("url", (String) parts.get(1));
182 administerOperations.add(entry);
188 Map templateData = new HashMap();
189 templateData.put("request", requestData);
190 templateData.put("session", sessionData);
191 templateData.put("application", applicationData);
192 templateData.put("utility", utilityMap);
193 templateData.put("config", configData);
194 templateData.put("administeroperations", administerOperations);
196 String templateName = generateTemplateString(request);
198 Locale[] locales = new Locale[2];
199 locales[0] = request.getLocale();
200 locales[1] = request.getLocale();
203 PrintWriter printWriter = response.getWriter();
204 generateResponse(printWriter, templateData, templateName, locales);
205 } catch (Throwable e) {
206 throw new ServletException(e);
210 private String generateTemplateString(HttpServletRequest request){
211 String returnString = request.getServletPath();
212 returnString = returnString.substring(0, returnString.indexOf("."));
213 return returnString + ".tmpl";
216 private void generateResponse(PrintWriter printWriter, Map map,
217 String templateString, Locale[] locales)
218 throws ServletModuleExc{
219 if(templateString.startsWith("/admin")){
221 new ResourceBundleGeneratorFunction( locales,
222 new MessageResources[] {
223 MessageResources.getMessageResources("bundles.admin"),
224 MessageResources.getMessageResources("bundles.adminlocal")
227 templateString = templateString.substring(templateString.lastIndexOf('/'));
228 ServletHelper.generateResponse(printWriter, map, templateString);
230 if(templateString.startsWith("/open")){
232 new ResourceBundleGeneratorFunction( locales,
233 new MessageResources[] {
234 MessageResources.getMessageResources("bundles.open"),
235 MessageResources.getMessageResources("bundles.open")
238 templateString = templateString.substring(templateString.lastIndexOf('/'));
239 ServletHelper.generateOpenPostingResponse(printWriter, map, templateString);
244 * @see javax.servlet.Servlet#destroy()
246 public void destroy() {
251 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
253 public void init(ServletConfig servletConfig) throws ServletException {
254 super.init(servletConfig);
255 MirPropertiesConfiguration.setContext(servletConfig.getServletContext());
257 MirPropertiesConfiguration.instance();
259 catch (Throwable t) {
260 throw new ServletException("can't read configuration: " + t.toString());