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.Enumeration;
37 import java.util.HashMap;
38 import java.util.Locale;
41 import javax.servlet.ServletConfig;
42 import javax.servlet.ServletContext;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServlet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.http.HttpSession;
49 import mir.config.MirPropertiesConfiguration;
50 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
51 import mir.servlet.ServletModuleExc;
52 import mir.util.ResourceBundleGeneratorFunction;
53 import mircoders.servlet.ServletHelper;
55 import org.apache.struts.util.MessageResources;
60 * @version $Id: TemplateServlet.java,v 1.1 2003/09/05 20:23:59 idfx Exp $
62 public class TemplateServlet extends HttpServlet {
67 public TemplateServlet() {
72 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
74 protected final void doGet(HttpServletRequest request, HttpServletResponse response)
75 throws ServletException, IOException {
76 process(request, response);
80 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
82 protected final void doPost(HttpServletRequest request, HttpServletResponse response)
83 throws ServletException, IOException {
84 process(request, response);
88 * Processes the request and invokes the presentation
92 protected void process(HttpServletRequest request, HttpServletResponse response)
93 throws IOException, ServletException {
94 PrintWriter printWriter = response.getWriter();
95 Map requestData = new HashMap();
96 Enumeration keys = request.getAttributeNames();
97 while(keys.hasMoreElements()){
98 final String key = (String)keys.nextElement();
99 requestData.put(key, request.getAttribute(key));
101 Map sessionData = new HashMap();
102 HttpSession httpSession = request.getSession();
103 keys = httpSession.getAttributeNames();
104 while(keys.hasMoreElements()){
105 final String key = (String)keys.nextElement();
106 sessionData.put(key, httpSession.getAttribute(key));
108 Map applicationData = new HashMap();
109 ServletContext servletContext = getServletContext();
110 keys = servletContext.getAttributeNames();
111 while(keys.hasMoreElements()){
112 final String key = (String)keys.nextElement();
113 applicationData.put(key, servletContext.getAttribute(key));
115 Map templateData = new HashMap();
116 templateData.put("request", requestData);
117 templateData.put("session", sessionData);
118 templateData.put("application", applicationData);
120 templateData.put("config", MirPropertiesConfiguration.instance().allSettings());
121 } catch (PropertiesConfigExc e) {
122 throw new ServletException(e);
125 String templateName = generateTemplateString(request);
127 Locale[] locales = new Locale[2];
128 locales[0] = request.getLocale();
129 locales[1] = request.getLocale();
133 generateResponse(printWriter, templateData, templateName, locales);
134 } catch (ServletModuleExc e) {
135 throw new ServletException(e);
139 private String generateTemplateString(HttpServletRequest request){
140 String returnString = request.getServletPath();
141 returnString = returnString.substring(0, returnString.indexOf("."));
142 return returnString + ".template";
145 private void generateResponse(PrintWriter printWriter, Map map,
146 String templateString, Locale[] locales)
147 throws ServletModuleExc{
148 if(templateString.startsWith("/admin")){
150 new ResourceBundleGeneratorFunction( locales,
151 new MessageResources[] { MessageResources.getMessageResources("bundles.admin"),
152 MessageResources.getMessageResources("bundles.adminlocal")}));
154 templateString = templateString.substring(6);
155 ServletHelper.generateResponse(printWriter, map, templateString);
157 if(templateString.startsWith("/open")){
159 new ResourceBundleGeneratorFunction( locales,
160 new MessageResources[] { MessageResources.getMessageResources("bundles.open"),
161 MessageResources.getMessageResources("bundles.open")}));
163 templateString = templateString.substring(5);
164 ServletHelper.generateOpenPostingResponse(printWriter, map, templateString);
169 * @see javax.servlet.Servlet#destroy()
171 public void destroy() {
176 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
178 public void init(ServletConfig servletConfig) throws ServletException {
179 super.init(servletConfig);