X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fservlet%2FServletHelper.java;fp=source%2Fmircoders%2Fservlet%2FServletHelper.java;h=fd699a29c4e33fc717d8a6fa6b9e3b77518cb034;hb=c9ac8fa71b679f8d967aac901bbef945c13b94c9;hp=1fa090180b37cf6d0c706d82bc6629d1119ba3ee;hpb=d63595f89aaa4b6a524dc0b4af9e0eef888f4c6b;p=mir.git diff --git a/source/mircoders/servlet/ServletHelper.java b/source/mircoders/servlet/ServletHelper.java index 1fa09018..fd699a29 100755 --- a/source/mircoders/servlet/ServletHelper.java +++ b/source/mircoders/servlet/ServletHelper.java @@ -1,172 +1,226 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. - * If you do not wish to do so, delete this exception statement from your version. - */ -package mircoders.servlet; - -import java.io.PrintWriter; -import java.io.IOException; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import mir.config.MirPropertiesConfiguration; -import mir.entity.adapter.EntityAdapter; -import mir.generator.Generator; -import mir.generator.GeneratorHelper; -import mir.log.LoggerWrapper; -import mir.servlet.ServletModuleExc; -import mir.servlet.ServletModuleFailure; -import mircoders.entity.EntityUsers; -import mircoders.global.MirGlobal; - - -public class ServletHelper { - private static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper"); - - public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) throws ServletModuleExc { - return makeGenerationData(aRequest, aResponse, aLocales, "etc/bundles/adminlocal", "bundles/admin"); - } - - public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) throws ServletModuleExc { - return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle); - } - - public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc { - try { - MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance(); - Map result = GeneratorHelper.makeBasicGenerationData(aLocales, aBundle, aDefaultBundle); - if (configuration.getString("Mir.Admin.ShowLoggedinUsers").equals("1")) { - result.put("loggedinusers", MirGlobal.getLoggedInUsers()); - } - else { - result.put("loggedinusers", null); - } - result.put("systemstatus", MirGlobal.getStatus()); - - // ML: hackish - ((Map) result.get("config")).put("actionRoot", - aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir")); - - result.put("returnurl", null); - result.put("login_user", getUserAdapter(aRequest)); - - return result; - } - catch (Throwable t) { - throw new ServletModuleFailure(t); - } - } - - public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc { - logger.debug("generator used: " + aGenerator); - - Generator generator; - - try { - generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator); - - generator.generate(aWriter, aGenerationData, logger); - } - catch (Throwable t) { - throw new ServletModuleFailure(t); - } - } - - public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc { - Generator generator; - - try { - generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator); - - generator.generate(aWriter, aGenerationData, logger); - } - catch (Throwable t) { - throw new ServletModuleFailure(t); - } - } - - public static void generateInfoMessage(HttpServletRequest aRequest, HttpServletResponse aResponse, - Locale[] aLocales, String aBundle, String aDefaultBundle, String aMessage, String anArgument1, String anArgument2) throws ServletModuleExc { - Map responseData = makeGenerationData(aRequest, aResponse, aLocales, aBundle, aDefaultBundle); - responseData.put("message", aMessage); - responseData.put("argument1", anArgument1); - responseData.put("argument2", anArgument2); - - try { - generateResponse(aResponse.getWriter(), responseData, "infomessage.template"); - } - catch (IOException e) { - throw new ServletModuleFailure(e); - } - } - - public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure { - try { - aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery)); - } - catch (IOException t) { - throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t); - } - } - - public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleExc, ServletModuleFailure { - redirect(aResponse, "module="+aModule+"&do="+aMethod); - } - - public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) { - if (aUser!=null) - aRequest.getSession().setAttribute("login.uid", aUser); - else - aRequest.getSession().removeAttribute("login.uid"); - } - - public static EntityUsers getUser(HttpServletRequest aRequest) { - return (EntityUsers) aRequest.getSession().getAttribute("login.uid"); - } - - public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) { - try { - return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter( "user", (EntityUsers) aRequest.getSession().getAttribute("login.uid")); - } - catch (Throwable t) { - throw new ServletModuleFailure (t); - } - } - - public static String getUserName(HttpServletRequest aRequest) { - EntityUsers user = getUser(aRequest); - - if (user!=null) - return user.getFieldValue("login"); - else - return "nobody"; - } -} +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mircoders.servlet; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Locale; +import java.util.Map; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import mir.config.MirPropertiesConfiguration; +import mir.entity.adapter.EntityAdapter; +import mir.entity.Entity; +import mir.generator.Generator; +import mir.generator.GeneratorHelper; +import mir.log.LoggerWrapper; +import mir.servlet.ServletModuleExc; +import mir.servlet.ServletModuleFailure; +import mir.servlet.AdminServletModule; +import mircoders.entity.EntityUsers; +import mircoders.global.MirGlobal; + + +public class ServletHelper { + private static final LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper"); + + private ServletHelper() { + } + + public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) { + return makeGenerationData(aRequest, aResponse, aLocales, "etc/bundles/adminlocal", "bundles/admin"); + } + + public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) { + return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle); + } + + public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) { + try { + MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance(); + Map result = GeneratorHelper.makeBasicGenerationData(aLocales, aBundle, aDefaultBundle); + if (configuration.getBoolean("Mir.Admin.ShowLoggedinUsers")) { + result.put("loggedinusers", MirGlobal.getLoggedInUsers()); + } + else { + result.put("loggedinusers", null); + } + result.put("systemstatus", MirGlobal.getStatus()); + + // ML: hackish + ((Map) result.get("config")).put("actionRoot", + aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir")); + + result.put("returnurl", null); + result.put("login_user", getUserAdapter(aRequest)); + + return result; + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + } + + public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) { + logger.debug("generator used: " + aGenerator); + + try { + Generator generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator, null); + + generator.generate(aWriter, aGenerationData, logger); + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + } + + public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) { + try { + Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator, null); + + generator.generate(aWriter, aGenerationData, logger); + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + } + + public static void generateInfoMessage(HttpServletRequest aRequest, HttpServletResponse aResponse, + Locale[] aLocales, String aBundle, String aDefaultBundle, String aMessage, String anArgument1, String anArgument2) { + Map responseData = makeGenerationData(aRequest, aResponse, aLocales, aBundle, aDefaultBundle); + responseData.put("message", aMessage); + responseData.put("argument1", anArgument1); + responseData.put("argument2", anArgument2); + + try { + generateResponse(aResponse.getWriter(), responseData, "infomessage.template"); + } + catch (IOException e) { + throw new ServletModuleFailure(e); + } + } + + public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleFailure { + try { + aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery)); + } + catch (IOException t) { + throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t); + } + } + + public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleFailure { + redirect(aResponse, "module="+aModule+"&do="+aMethod); + } + + public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) { + if (aUser!=null) { + aRequest.getSession().setAttribute("login.uid", aUser); + } + else { + aRequest.getSession().removeAttribute("login.uid"); + } + } + + public static EntityUsers getUser(HttpServletRequest aRequest) { + return (EntityUsers) aRequest.getSession().getAttribute("login.uid"); + } + + public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) { + try { + return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("user", + (Entity) aRequest.getSession().getAttribute("login.uid")); + } + catch (Throwable t) { + throw new ServletModuleFailure (t); + } + } + + public static String getUserName(HttpServletRequest aRequest) { + EntityUsers user = getUser(aRequest); + + if (user!=null) { + return user.getFieldValue("login"); + } + + return "nobody"; + } + + private static final Map servletModules = new HashMap(); + + public static AdminServletModule getServletModule(String aName) throws ServletModuleExc { + synchronized (servletModules) { + if (!servletModules.containsKey(aName)) { + // was not found in the cache... + try { + Class servletModuleClass = + Class.forName("mircoders.servlet.ServletModule" + aName); + + AdminServletModule module = (AdminServletModule) + servletModuleClass.newInstance(); + + // we put it into our cache for future calls + servletModules.put(aName, module); + + return module; + } + catch (Exception e) { + throw new ServletModuleExc("*** error resolving classname for " + aName + " -- " + e.getMessage()); + } + } + + return (AdminServletModule) servletModules.get(aName); + } + } + + public static ServletModuleFileEdit getServletModuleFileEdit() throws ServletModuleExc { + return (ServletModuleFileEdit) getServletModule("FileEdit"); + } + + public static ServletModuleLocalizer getServletModuleLocalizer() throws ServletModuleExc { + return (ServletModuleLocalizer) getServletModule("Localizer"); + } + + public static ServletModuleAdmin getServletModuleAdmin() throws ServletModuleExc { + return (ServletModuleAdmin) getServletModule("Admin"); + } + + public static ServletModuleContent getServletModuleContent() throws ServletModuleExc { + return (ServletModuleContent) getServletModule("Content"); + } + + public static ServletModuleComment getServletModuleComment() throws ServletModuleExc { + return (ServletModuleComment) getServletModule("Comment"); + } + +}