2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mircoders.servlet;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.Locale;
36 import java.util.HashMap;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
41 import mir.config.MirPropertiesConfiguration;
42 import mir.entity.adapter.EntityAdapter;
43 import mir.entity.Entity;
44 import mir.generator.Generator;
45 import mir.generator.GeneratorHelper;
46 import mir.log.LoggerWrapper;
47 import mir.servlet.ServletModuleExc;
48 import mir.servlet.ServletModuleFailure;
49 import mir.servlet.AdminServletModule;
50 import mircoders.entity.EntityUsers;
51 import mircoders.global.MirGlobal;
54 public class ServletHelper {
55 private static final LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");
57 private ServletHelper() {
60 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) {
61 return makeGenerationData(aRequest, aResponse, aLocales, "etc/bundles/adminlocal", "bundles/admin");
64 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) {
65 return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle);
68 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) {
70 MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
71 Map result = GeneratorHelper.makeBasicGenerationData(aLocales, aBundle, aDefaultBundle);
72 if (configuration.getBoolean("Mir.Admin.ShowLoggedinUsers")) {
73 result.put("loggedinusers", MirGlobal.getLoggedInUsers());
76 result.put("loggedinusers", null);
78 result.put("systemstatus", MirGlobal.getStatus());
81 ((Map) result.get("config")).put("actionRoot",
82 aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir"));
84 result.put("returnurl", null);
85 result.put("login_user", getUserAdapter(aRequest));
90 throw new ServletModuleFailure(t);
94 public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) {
95 logger.debug("generator used: " + aGenerator);
98 Generator generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator, null);
100 generator.generate(aWriter, aGenerationData, logger);
102 catch (Throwable t) {
103 throw new ServletModuleFailure(t);
107 public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) {
109 Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator, null);
111 generator.generate(aWriter, aGenerationData, logger);
113 catch (Throwable t) {
114 throw new ServletModuleFailure(t);
118 public static void generateInfoMessage(HttpServletRequest aRequest, HttpServletResponse aResponse,
119 Locale[] aLocales, String aBundle, String aDefaultBundle, String aMessage, String anArgument1, String anArgument2) {
120 Map responseData = makeGenerationData(aRequest, aResponse, aLocales, aBundle, aDefaultBundle);
121 responseData.put("message", aMessage);
122 responseData.put("argument1", anArgument1);
123 responseData.put("argument2", anArgument2);
126 generateResponse(aResponse.getWriter(), responseData, "infomessage.template");
128 catch (IOException e) {
129 throw new ServletModuleFailure(e);
133 public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleFailure {
135 aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));
137 catch (IOException t) {
138 throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
142 public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleFailure {
143 redirect(aResponse, "module="+aModule+"&do="+aMethod);
146 public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) {
148 aRequest.getSession().setAttribute("login.uid", aUser);
151 aRequest.getSession().removeAttribute("login.uid");
155 public static EntityUsers getUser(HttpServletRequest aRequest) {
156 return (EntityUsers) aRequest.getSession().getAttribute("login.uid");
159 public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) {
161 return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("user",
162 (Entity) aRequest.getSession().getAttribute("login.uid"));
164 catch (Throwable t) {
165 throw new ServletModuleFailure (t);
169 public static String getUserName(HttpServletRequest aRequest) {
170 EntityUsers user = getUser(aRequest);
173 return user.getFieldValue("login");
179 private static final Map servletModules = new HashMap();
181 public static AdminServletModule getServletModule(String aName) throws ServletModuleExc {
182 synchronized (servletModules) {
183 if (!servletModules.containsKey(aName)) {
184 // was not found in the cache...
186 Class servletModuleClass =
187 Class.forName("mircoders.servlet.ServletModule" + aName);
189 AdminServletModule module = (AdminServletModule)
190 servletModuleClass.newInstance();
192 // we put it into our cache for future calls
193 servletModules.put(aName, module);
197 catch (Exception e) {
198 throw new ServletModuleExc("*** error resolving classname for " + aName + " -- " + e.getMessage());
202 return (AdminServletModule) servletModules.get(aName);
206 public static ServletModuleFileEdit getServletModuleFileEdit() throws ServletModuleExc {
207 return (ServletModuleFileEdit) getServletModule("FileEdit");
210 public static ServletModuleLocalizer getServletModuleLocalizer() throws ServletModuleExc {
211 return (ServletModuleLocalizer) getServletModule("Localizer");
214 public static ServletModuleAdmin getServletModuleAdmin() throws ServletModuleExc {
215 return (ServletModuleAdmin) getServletModule("Admin");
218 public static ServletModuleContent getServletModuleContent() throws ServletModuleExc {
219 return (ServletModuleContent) getServletModule("Content");
222 public static ServletModuleComment getServletModuleComment() throws ServletModuleExc {
223 return (ServletModuleComment) getServletModule("Comment");