merged with 1.1
[mir.git] / source / mircoders / servlet / ServletHelper.java
index 7836c12..5ed322c 100755 (executable)
-/*
- * 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.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.struts.util.MessageResources;
-
-
-import mir.entity.adapter.EntityIteratorAdapter;
-import mir.generator.Generator;
-import mir.log.LoggerWrapper;
-import mir.servlet.ServletModuleExc;
-import mir.servlet.ServletModuleFailure;
-import mir.util.CachingRewindableIterator;
-import mir.util.ResourceBundleGeneratorFunction;
-import mircoders.global.MirGlobal;
-
-
-public class ServletHelper {
-  static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");
-
-
-  public static Map makeGenerationData(HttpServletResponse aResponse, Locale[] aLocales) throws ServletModuleExc {
-    return makeGenerationData(aResponse, aLocales, "bundles.adminlocal", "bundles.admin");
-  }
-
-  public static Map makeGenerationData(HttpServletResponse aResponse, Locale[] aLocales, String aBundle) throws ServletModuleExc {
-    return makeGenerationData(aResponse, aLocales, aBundle, aBundle);
-  }
-
-  public static Map makeGenerationData(HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc {
-
-    try {
-      Map result = new HashMap();
-
-      MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(result);
-
-      // ML: hackish
-      ((Map) result.get("config")).put("actionRoot",
-             aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir"));
-
-      result.put("returnurl", null);
-
-      Object languages =
-          new CachingRewindableIterator(
-            new EntityIteratorAdapter( "", "id", 30,
-               MirGlobal.localizer().dataModel().adapterModel(), "language"));
-      Object topics =
-          new CachingRewindableIterator(
-            new EntityIteratorAdapter("", "id", 30,
-               MirGlobal.localizer().dataModel().adapterModel(), "topic"));
-
-      Object articleTypes =
-          new CachingRewindableIterator(
-            new EntityIteratorAdapter( "", "id", 30,
-               MirGlobal.localizer().dataModel().adapterModel(), "articleType"));
-
-      Object commentStatuses =
-          new CachingRewindableIterator(
-            new EntityIteratorAdapter( "", "id", 30,
-               MirGlobal.localizer().dataModel().adapterModel(), "commentStatus"));
-
-      result.put("commentstatuses", commentStatuses);
-      result.put("articletypes", articleTypes);
-      result.put("languages", languages);
-      result.put("topics", topics);
-
-      result.put( "lang",
-          new ResourceBundleGeneratorFunction( aLocales,
-             new MessageResources[] { MessageResources.getMessageResources(aBundle),
-                                   MessageResources.getMessageResources(aDefaultBundle)}));
-
-      return result;
-    }
-    catch (Throwable t) {
-      throw new ServletModuleFailure(t);
-    }
-  }
-
-  public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {
-    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);
-    }
-  }
-}
+/*\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.util.HashMap;\r
+import java.util.Locale;\r
+import java.util.Map;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.apache.struts.util.MessageResources;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+import mir.entity.adapter.*;\r
+import mir.generator.Generator;\r
+import mir.log.LoggerWrapper;\r
+import mir.servlet.ServletModuleExc;\r
+import mir.servlet.ServletModuleFailure;\r
+import mir.util.CachingRewindableIterator;\r
+import mir.util.ResourceBundleGeneratorFunction;\r
+import mircoders.entity.EntityUsers;\r
+import mircoders.global.MirGlobal;\r
+\r
+\r
+public class ServletHelper {\r
+  static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");\r
+\r
+\r
+  public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) throws ServletModuleExc {\r
+    return makeGenerationData(aRequest, aResponse, aLocales, "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
+\r
+    try {\r
+      Map result = new HashMap();\r
+\r
+      MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(result);\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
+      Object languages =\r
+          new CachingRewindableIterator(\r
+            new EntityIteratorAdapter( "", "id", 30,\r
+               MirGlobal.localizer().dataModel().adapterModel(), "language"));\r
+\r
+      Object topics =\r
+          new CachingRewindableIterator(\r
+            new EntityIteratorAdapter("", "id", 30,\r
+               MirGlobal.localizer().dataModel().adapterModel(), "topic"));\r
+\r
+      Object articleTypes =\r
+          new CachingRewindableIterator(\r
+            new EntityIteratorAdapter( "", "id", 30,\r
+               MirGlobal.localizer().dataModel().adapterModel(), "articleType"));\r
+\r
+      Object commentStatuses =\r
+          new CachingRewindableIterator(\r
+            new EntityIteratorAdapter( "", "id", 30,\r
+               MirGlobal.localizer().dataModel().adapterModel(), "commentStatus"));\r
+\r
+      result.put("commentstatuses", commentStatuses);\r
+      result.put("articletypes", articleTypes);\r
+      result.put("languages", languages);\r
+      result.put("topics", topics);\r
+\r
+      result.put( "lang",\r
+          new ResourceBundleGeneratorFunction( aLocales,\r
+             new MessageResources[] { MessageResources.getMessageResources(aBundle),\r
+                MessageResources.getMessageResources(aDefaultBundle)}));\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 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 (Throwable 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.getValue("login");\r
+    else\r
+      return "nobody";\r
+  }\r
+}\r