merged 1.1 branch into head
[mir.git] / source / mircoders / servlet / ServletHelper.java
index 1fa0901..fd699a2 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with  any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two.  You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries.  If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-package mircoders.servlet;\r
-\r
-import java.io.PrintWriter;\r
-import java.io.IOException;\r
-import java.util.Locale;\r
-import java.util.Map;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.entity.adapter.EntityAdapter;\r
-import mir.generator.Generator;\r
-import mir.generator.GeneratorHelper;\r
-import mir.log.LoggerWrapper;\r
-import mir.servlet.ServletModuleExc;\r
-import mir.servlet.ServletModuleFailure;\r
-import mircoders.entity.EntityUsers;\r
-import mircoders.global.MirGlobal;\r
-\r
-\r
-public class ServletHelper {\r
-  private static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");\r
-\r
-  public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) throws ServletModuleExc {\r
-    return makeGenerationData(aRequest, aResponse, aLocales, "etc/bundles/adminlocal", "bundles/admin");\r
-  }\r
-\r
-  public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) throws ServletModuleExc {\r
-    return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle);\r
-  }\r
-\r
-  public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc {\r
-    try {\r
-      MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();\r
-      Map result = GeneratorHelper.makeBasicGenerationData(aLocales, aBundle, aDefaultBundle);\r
-      if (configuration.getString("Mir.Admin.ShowLoggedinUsers").equals("1")) {\r
-        result.put("loggedinusers", MirGlobal.getLoggedInUsers());\r
-      }\r
-      else {\r
-        result.put("loggedinusers", null);\r
-      }\r
-      result.put("systemstatus", MirGlobal.getStatus());\r
-\r
-      // ML: hackish\r
-      ((Map) result.get("config")).put("actionRoot",\r
-             aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir"));\r
-\r
-      result.put("returnurl", null);\r
-      result.put("login_user", getUserAdapter(aRequest));\r
-\r
-      return result;\r
-    }\r
-    catch (Throwable t) {\r
-      throw new ServletModuleFailure(t);\r
-    }\r
-  }\r
-\r
-  public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
-    logger.debug("generator used: " + aGenerator);\r
-\r
-    Generator generator;\r
-\r
-    try {\r
-      generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator);\r
-\r
-      generator.generate(aWriter, aGenerationData, logger);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new ServletModuleFailure(t);\r
-    }\r
-  }\r
-\r
-  public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
-    Generator generator;\r
-\r
-    try {\r
-      generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);\r
-\r
-      generator.generate(aWriter, aGenerationData, logger);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new ServletModuleFailure(t);\r
-    }\r
-  }\r
-\r
-  public static void generateInfoMessage(HttpServletRequest aRequest, HttpServletResponse aResponse,\r
-      Locale[] aLocales, String aBundle, String aDefaultBundle, String aMessage, String anArgument1, String anArgument2) throws ServletModuleExc {\r
-    Map responseData = makeGenerationData(aRequest, aResponse, aLocales, aBundle, aDefaultBundle);\r
-    responseData.put("message", aMessage);\r
-    responseData.put("argument1", anArgument1);\r
-    responseData.put("argument2", anArgument2);\r
-\r
-    try {\r
-      generateResponse(aResponse.getWriter(), responseData, "infomessage.template");\r
-    }\r
-    catch (IOException e) {\r
-      throw new ServletModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure {\r
-    try {\r
-      aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));\r
-    }\r
-    catch (IOException t) {\r
-      throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);\r
-    }\r
-  }\r
-\r
-  public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleExc, ServletModuleFailure {\r
-    redirect(aResponse, "module="+aModule+"&do="+aMethod);\r
-  }\r
-\r
-  public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) {\r
-    if (aUser!=null)\r
-      aRequest.getSession().setAttribute("login.uid", aUser);\r
-    else\r
-      aRequest.getSession().removeAttribute("login.uid");\r
-  }\r
-\r
-  public static EntityUsers getUser(HttpServletRequest aRequest) {\r
-    return (EntityUsers) aRequest.getSession().getAttribute("login.uid");\r
-  }\r
-\r
-  public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) {\r
-    try {\r
-      return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter( "user", (EntityUsers) aRequest.getSession().getAttribute("login.uid"));\r
-    }\r
-    catch (Throwable t) {\r
-      throw new ServletModuleFailure (t);\r
-    }\r
-  }\r
-\r
-  public static String getUserName(HttpServletRequest aRequest) {\r
-    EntityUsers user = getUser(aRequest);\r
-\r
-    if (user!=null)\r
-      return user.getFieldValue("login");\r
-    else\r
-      return "nobody";\r
-  }\r
-}\r
+/*
+ * 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");
+  }
+
+}