2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mircoders.servlet;
\r
32 import java.io.PrintWriter;
\r
33 import java.util.Locale;
\r
34 import java.util.Map;
\r
36 import javax.servlet.http.HttpServletRequest;
\r
37 import javax.servlet.http.HttpServletResponse;
\r
39 import mir.config.MirPropertiesConfiguration;
\r
40 import mir.entity.adapter.EntityAdapter;
\r
41 import mir.generator.Generator;
\r
42 import mir.generator.GeneratorHelper;
\r
43 import mir.log.LoggerWrapper;
\r
44 import mir.servlet.ServletModuleExc;
\r
45 import mir.servlet.ServletModuleFailure;
\r
46 import mircoders.entity.EntityUsers;
\r
47 import mircoders.global.MirGlobal;
\r
50 public class ServletHelper {
\r
51 private static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");
\r
53 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) throws ServletModuleExc {
\r
54 return makeGenerationData(aRequest, aResponse, aLocales, "bundles.adminlocal", "bundles.admin");
\r
57 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) throws ServletModuleExc {
\r
58 return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle);
\r
61 public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc {
\r
63 MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
\r
64 Map result=GeneratorHelper.makeBasicGenerationData(aLocales,aBundle,aDefaultBundle);
\r
65 if (configuration.getString("Mir.Admin.ShowLoggedinUsers").equals("1")) {
\r
66 result.put("loggedinusers", MirGlobal.getLoggedInUsers());
\r
69 result.put("loggedinusers", null);
\r
71 result.put("systemstatus", MirGlobal.getStatus());
\r
74 ((Map) result.get("config")).put("actionRoot",
\r
75 aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir"));
\r
77 result.put("returnurl", null);
\r
78 result.put("login_user", getUserAdapter(aRequest));
\r
82 catch (Throwable t) {
\r
83 throw new ServletModuleFailure(t);
\r
87 public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {
\r
88 logger.debug("generator used: " + aGenerator);
\r
90 Generator generator;
\r
93 generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator);
\r
95 generator.generate(aWriter, aGenerationData, logger);
\r
97 catch (Throwable t) {
\r
98 throw new ServletModuleFailure(t);
\r
102 public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {
\r
103 Generator generator;
\r
106 generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);
\r
108 generator.generate(aWriter, aGenerationData, logger);
\r
110 catch (Throwable t) {
\r
111 throw new ServletModuleFailure(t);
\r
115 public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure {
\r
117 aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));
\r
119 catch (Throwable t) {
\r
120 throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
\r
124 public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleExc, ServletModuleFailure {
\r
125 redirect(aResponse, "module="+aModule+"&do="+aMethod);
\r
128 public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) {
\r
130 aRequest.getSession().setAttribute("login.uid", aUser);
\r
132 aRequest.getSession().removeAttribute("login.uid");
\r
135 public static EntityUsers getUser(HttpServletRequest aRequest) {
\r
136 return (EntityUsers) aRequest.getSession().getAttribute("login.uid");
\r
139 public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) {
\r
141 return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter( "user", (EntityUsers) aRequest.getSession().getAttribute("login.uid"));
\r
143 catch (Throwable t) {
\r
144 throw new ServletModuleFailure (t);
\r
148 public static String getUserName(HttpServletRequest aRequest) {
\r
149 EntityUsers user = getUser(aRequest);
\r
152 return user.getFieldValue("login");
\r