06711cc3de23fb517fc7a097dd223f55e131d721
[mir.git] / source / mir / core / ui / servlet / TemplateServlet.java
1 /*
2  * TemplateServlet.java created on 01.09.2003
3  * 
4  * Copyright (C) 2001, 2002, 2003 The Mir-coders group
5  *
6  * This file is part of Mir.
7  *
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.
12  *
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.
17  *
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
21  *
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.
31  */
32 package mir.core.ui.servlet;
33
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;
39 import java.util.Map;
40
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;
48
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;
54
55 import org.apache.struts.util.MessageResources;
56
57 /**
58  * TemplateServlet
59  * @author idefix
60  * @version $Id: TemplateServlet.java,v 1.1 2003/09/05 20:23:59 idfx Exp $
61  */
62 public class TemplateServlet extends HttpServlet {
63
64         /**
65          * 
66          */
67         public TemplateServlet() {
68                 super();
69         }
70         
71         /**
72          * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
73          */
74         protected final void doGet(HttpServletRequest request, HttpServletResponse response) 
75         throws ServletException, IOException {
76                 process(request, response);
77         }
78
79         /**
80          * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
81          */
82         protected final void doPost(HttpServletRequest request, HttpServletResponse response) 
83                 throws ServletException, IOException {
84                 process(request, response);
85         }
86         
87         /**
88          * Processes the request and invokes the presentation
89          * @param request
90          * @param response
91          */
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));
100                 }
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));
107                 }
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));
114                 }               
115                 Map templateData = new HashMap();
116                 templateData.put("request", requestData);
117                 templateData.put("session", sessionData);
118                 templateData.put("application", applicationData);
119                 try {
120                         templateData.put("config", MirPropertiesConfiguration.instance().allSettings());
121                 } catch (PropertiesConfigExc e) {
122                         throw new ServletException(e);
123                 }
124                 
125                 String templateName = generateTemplateString(request);
126                 
127                 Locale[] locales = new Locale[2];
128                 locales[0] = request.getLocale();
129                 locales[1] = request.getLocale();
130                 
131                 try {
132                         //write the servlet
133                         generateResponse(printWriter, templateData, templateName, locales);
134                 } catch (ServletModuleExc e) {
135                         throw new ServletException(e);
136                 }
137         }
138         
139         private String generateTemplateString(HttpServletRequest request){
140                 String returnString = request.getServletPath();
141                 returnString = returnString.substring(0, returnString.indexOf("."));
142                 return returnString + ".template";
143         }
144         
145         private void generateResponse(PrintWriter printWriter, Map map, 
146                 String templateString, Locale[] locales) 
147                 throws ServletModuleExc{
148                 if(templateString.startsWith("/admin")){
149                         map.put( "lang",
150                                         new ResourceBundleGeneratorFunction( locales,
151                                                  new MessageResources[] { MessageResources.getMessageResources("bundles.admin"),
152                                                                 MessageResources.getMessageResources("bundles.adminlocal")}));
153                                                                 
154                         templateString = templateString.substring(6);
155                         ServletHelper.generateResponse(printWriter, map, templateString);       
156                 }
157                 if(templateString.startsWith("/open")){
158                         map.put( "lang",
159                                         new ResourceBundleGeneratorFunction( locales,
160                                                  new MessageResources[] { MessageResources.getMessageResources("bundles.open"),
161                                                                 MessageResources.getMessageResources("bundles.open")}));
162                                                                 
163                         templateString = templateString.substring(5);
164                         ServletHelper.generateOpenPostingResponse(printWriter, map, templateString);    
165                 }               
166         }
167
168         /**
169          * @see javax.servlet.Servlet#destroy()
170          */
171         public void destroy() {
172                 super.destroy();
173         }
174
175         /**
176          * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
177          */
178         public void init(ServletConfig servletConfig) throws ServletException {
179                 super.init(servletConfig);
180         }
181 }