-/*
- * 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 the com.oreilly.servlet library, 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 mir.servlet;
-
-import freemarker.template.SimpleHash;
-import freemarker.template.TemplateModelRoot;
-import freemarker.template.TemplateModel;
-
-import mir.entity.EntityList;
-import mir.log.*;
-import mir.misc.*;
-import mir.module.AbstractModule;
-import mir.module.ModuleException;
-import mir.storage.StorageObject;
-import mir.storage.StorageObjectException;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Locale;
-
-
-/**
- * Abstract class ServletModule provides the base functionality for servlets.
- * Deriving a class from ServletModule enables class to insert/edit/update/delete
- * and list Entity from a Database via mainModule.
- *
- *
- * Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der
- * abgeleiteten ServletModule zur Verfügung.
- *
- * @version 28.6.1999
- * @author RK
- */
-
-public abstract class ServletModule {
-
- public String defaultAction;
- protected LoggerWrapper logger;
-
- protected AbstractModule mainModule;
- protected String templateListString;
- protected String templateObjektString;
- protected String templateConfirmString;
-
- /**
- * Singelton - Methode muss in den abgeleiteten Klassen ueberschrieben werden.
- * @return ServletModule
- */
- public static ServletModule getInstance() {
- return null;
- }
-
- /**
- * get the module name to be used for generic operations like delete.
- */
- protected String getOperationModuleName() {
- return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
- }
-
- /**
- * get the session binded language
- */
- public String getLanguage(HttpServletRequest req) {
- HttpSession session = req.getSession(false);
- String language = (String) session.getAttribute("Language");
- if (language == null) {
- language = MirConfig.getProp("StandardLanguage");
- }
- return language;
- }
-
- /**
- * get the locale either from the session or the accept-language header ot the request
- * this supersedes getLanguage for the new i18n
- */
- public Locale getLocale(HttpServletRequest req) {
- Locale loc = null;
- HttpSession session = req.getSession(false);
- if (session != null) {
- // session can be null in case of logout
- loc = (Locale) session.getAttribute("Locale");
- }
- // if there is nothing in the session get it fron the accept-language
- if (loc == null) {
- loc = req.getLocale();
- }
- return loc;
- }
-
- public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleException {
- try {
- aResponse.sendRedirect(MirConfig.getProp("RootUri") + "/Mir?"+aQuery);
- }
- catch (Throwable t) {
- throw new ServletModuleException(t.getMessage());
- }
- }
-
- /**
- * list(req,res) - generische Listmethode. Wennn die Funktionalitaet
- * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
- * ueberschreiben werden.
- *
- * @param req Http-Request, das vom Dispatcher durchgereicht wird
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- */
- public void list(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException {
- try {
- EntityList theList;
- String offsetParam = req.getParameter("offset");
- int offset = 0;
- PrintWriter out = res.getWriter();
-
- // hier offsetcode bearbeiten
- if (offsetParam != null && !offsetParam.equals("")) {
- offset = Integer.parseInt(offsetParam);
- }
- if (req.getParameter("next") != null) {
- offset = Integer.parseInt(req.getParameter("nextoffset"));
- }
- else {
- if (req.getParameter("prev") != null) {
- offset = Integer.parseInt(req.getParameter("prevoffset"));
- }
- }
- theList = mainModule.getByWhereClause(null, offset);
-
- HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
- }
- catch (Exception e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * add(req,res) - generische Addmethode. Wennn die Funktionalitaet
- * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
- * ueberschreiben werden.
- * @param req Http-Request, das vom Dispatcher durchgereicht wird
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- */
- public void add(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException {
-
- try {
- SimpleHash mergeData = new SimpleHash();
- mergeData.put("new", "1");
- deliver(req, res, mergeData, templateObjektString);
- }
- catch (Exception e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * insert(req,res) - generische Insertmethode, folgt auf add.
- * Wennn die Funktionalitaet
- * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
- * ueberschreiben werden.
- *
- * @param req Http-Request, das vom Dispatcher durchgereicht wird
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- */
- public void insert(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException, ServletModuleUserException {
- try {
- HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
- logger.debug("--trying to add...");
- String id = mainModule.add(withValues);
- logger.debug("--trying to deliver..." + id);
- list(req, res);
- }
- catch (Exception e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * delete(req,res) - generic delete method. Can be overridden in subclasses.
- *
- */
-
- public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
- try {
- String idParam = req.getParameter("id");
-
- if (idParam == null)
- throw new ServletModuleException("Invalid call to delete: no id supplied");
-
- String confirmParam = req.getParameter("confirm");
- String cancelParam = req.getParameter("cancel");
- if (confirmParam == null && cancelParam == null) {
- SimpleHash mergeData = new SimpleHash();
-
- mergeData.put("module", getOperationModuleName());
- mergeData.put("infoString", getOperationModuleName() + ": " + idParam);
- mergeData.put("id", idParam);
- mergeData.put("where", req.getParameter("where"));
- mergeData.put("order", req.getParameter("order"));
- mergeData.put("offset", req.getParameter("offset"));
- // this stuff is to be compatible with the other more advanced
- // search method used for media and comments
- mergeData.put("query_media_folder", req.getParameter("query_media_folder"));
- mergeData.put("query_is_published", req.getParameter("query_is_published"));
- mergeData.put("query_text", req.getParameter("query_text"));
- mergeData.put("query_field", req.getParameter("query_field"));
-
- deliver(req, res, mergeData, templateConfirmString);
- }
- else {
- if (confirmParam != null && !confirmParam.equals("")) {
- //theLog.printInfo("delete confirmed!");
- mainModule.deleteById(idParam);
- list(req, res); // back to list
- }
- else {
- if (req.getParameter("where") != null)
- list(req, res);
- else
- edit(req, res);
- }
- }
- }
- catch (Exception e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * edit(req,res) - generische Editmethode. Wennn die Funktionalitaet
- * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
- * ueberschreiben werden.
- *
- * @param req Http-Request, das vom Dispatcher durchgereicht wird
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- */
- public void edit(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException {
- try {
- String idParam = req.getParameter("id");
- deliver(req, res, mainModule.getById(idParam), templateObjektString);
- }
- catch (ModuleException e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * update(req,res) - generische Updatemethode. Wennn die Funktionalitaet
- * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
- * ueberschreiben werden.
- *
- * @param req Http-Request, das vom Dispatcher durchgereicht wird
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- */
-
- public void update(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException {
- try {
- String idParam = req.getParameter("id");
- HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
-
- String id = mainModule.set(withValues);
- String whereParam = req.getParameter("where");
- String orderParam = req.getParameter("order");
-
- if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) {
- list(req, res);
- }
- else {
- edit(req, res);
- }
- }
- catch (Exception e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * deliver liefert das Template mit dem Filenamen templateFilename
- * an den HttpServletResponse res aus, nachdem es mit den Daten aus
- * TemplateModelRoot rtm gemischt wurde
- *
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
- * Daten, die ins Template gemerged werden sollen.
- * @param tmpl Name des Templates
- * @exception ServletModuleException
- */
- public void deliver(HttpServletRequest req, HttpServletResponse res,
- TemplateModelRoot rtm, TemplateModelRoot popups,
- String templateFilename)
- throws ServletModuleException {
- if (rtm == null) rtm = new SimpleHash();
- try {
- PrintWriter out = res.getWriter();
- HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out, getLocale(req));
-
- // we default to admin bundles here, which is not exactly beautiful...
- // but this whole producer stuff is going to be rewritten soon.
- // ServletModuleOpenIndy overwrites deliver() to use open bundles
- // (br1)
- out.close();
- }
- catch (HTMLParseException e) {
- throw new ServletModuleException(e.getMessage());
- } catch (IOException e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
-
- /**
- * deliver liefert das Template mit dem Filenamen templateFilename
- * an den HttpServletResponse res aus, nachdem es mit den Daten aus
- * TemplateModelRoot rtm gemischt wurde
- *
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
- * Daten, die ins Template gemerged werden sollen.
- * @param tmpl Name des Templates
- * @exception ServletModuleException
- */
- public void deliver(HttpServletRequest req, HttpServletResponse res,
- TemplateModelRoot rtm, String templateFilename)
- throws ServletModuleException {
- deliver(req, res, rtm, null, templateFilename);
- }
-
- /**
- * deliver liefert das Template mit dem Filenamen templateFilename
- * an den HttpServletResponse res aus, nachdem es mit den Daten aus
- * TemplateModelRoot rtm gemischt wurde
- *
- * @param res Http-Response, die vom Dispatcher durchgereicht wird
- * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
- * Daten, die ins Template gemerged werden sollen.
- * @param tmpl Name des Templates
- * @exception ServletModuleException
- */
- public void deliver_compressed(HttpServletRequest req, HttpServletResponse res,
- TemplateModelRoot rtm, String templateFilename)
- throws ServletModuleException {
- if (rtm == null) rtm = new SimpleHash();
- try {
- PrintWriter out = new LineFilterWriter(res.getWriter());
- //PrintWriter out = res.getWriter();
- HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
- out.close();
- }
- catch (HTMLParseException e) {
- throw new ServletModuleException(e.getMessage());
- }
- catch (IOException e) {
- throw new ServletModuleException(e.getMessage());
- }
- }
-
- /**
- * deliver liefert das Template mit dem Filenamen templateFilename
- * an den HttpServletResponse res aus, nachdem es mit den Daten aus
- * TemplateModelRoot rtm gemischt wurde
- *
- * @param out ist der OutputStream, in den die gergten Daten geschickt werden sollen.
- * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
- * Daten, die ins Template gemerged werden sollen.
- * @param tmpl Name des Templates
- * @exception ServletModuleException
- */
- private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out,
- TemplateModelRoot rtm, String templateFilename)
- throws HTMLParseException {
- HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
- }
-
- /**
- * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
- * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
- * doParameters ausgefuehrt.
- *
- * @return Name der Default-Action
- */
- public String defaultAction() {
- return defaultAction;
- }
-
- /**
- * Hier kann vor der Datenaufbereitung schon mal ein response geschickt
- * werden (um das subjektive Antwortverhalten bei langsamen Verbindungen
- * zu verbessern).
- */
- public void predeliver(HttpServletRequest req, HttpServletResponse res) {
- ;
- }
-
- /**
- * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
- * im HttpRequest vorkommen und liefert sie als HashMap zurueck
- *
- * @return HashMap mit den Werten
- */
- public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
- throws ServletModuleException {
- ArrayList theFieldList;
- try {
- theFieldList = theStorage.getFields();
- }
- catch (StorageObjectException e) {
- throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage());
- }
-
- HashMap withValues = new HashMap();
- String aField, aValue;
-
- for (int i = 0; i < theFieldList.size(); i++) {
- aField = (String) theFieldList.get(i);
- aValue = req.getParameter(aField);
- if (aValue != null) withValues.put(aField, aValue);
- }
- return withValues;
- }
-
-}
+/*\r
+ * Copyright (C) 2001-2006 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
+ * 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 mir.servlet;\r
+\r
+import mir.log.LoggerWrapper;\r
+import mir.config.MirPropertiesConfiguration;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+import javax.servlet.http.HttpSession;\r
+import java.lang.reflect.Method;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Locale;\r
+\r
+public abstract class ServletModule {\r
+ private LoggerWrapper logger = new LoggerWrapper("ServletModule." + getName());\r
+ private MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();\r
+ private final Locale fallbackLocale = new Locale(\r
+ getConfiguration().getString("Mir.Admin.FallbackLanguage", "en"), "");\r
+\r
+ protected ServletModule() {\r
+ }\r
+\r
+ /**\r
+ * Return the name of this module\r
+ */\r
+ protected String getName() {\r
+ return getClass().getName().substring("mircoders.servlet.ServletModule".length());\r
+ }\r
+\r
+ /**\r
+ * Return the logger for this module\r
+ */\r
+ protected LoggerWrapper getLogger() {\r
+ return logger;\r
+ }\r
+\r
+ /**\r
+ * Return the global mir configuration\r
+ */\r
+ protected MirPropertiesConfiguration getConfiguration() {\r
+ return configuration;\r
+ }\r
+\r
+ /**\r
+ * signature of request handling methods\r
+ */\r
+ private static final Class[] HANDLER_METHOD_SIGNATURE = {\r
+ HttpServletRequest.class, HttpServletResponse.class\r
+ };\r
+\r
+ protected void defaultAction(HttpServletRequest aRequest,\r
+ HttpServletResponse aResponse)\r
+ throws ServletModuleExc, ServletModuleFailure, ServletModuleUserExc {\r
+ throw new ServletModuleExc("default action not defined for module " + getName());\r
+ }\r
+\r
+ /**\r
+ * get the locale either from the session or the accept-language header ot the request\r
+ * this supersedes getLanguage for the new i18n\r
+ */\r
+ protected Locale getFallbackLocale(HttpServletRequest aRequest) {\r
+ return fallbackLocale;\r
+ }\r
+\r
+ public Locale[] getLocales(HttpServletRequest aRequest) {\r
+ return new Locale[] { getLocale(aRequest), fallbackLocale };\r
+ }\r
+\r
+ /**\r
+ * Return the locale either from the session or the accept-language header ot the request\r
+ * this supersedes getLanguage for the new i18n\r
+ */\r
+ public static Locale getLocale(HttpServletRequest aRequest) {\r
+ Locale locale = null;\r
+ HttpSession session = aRequest.getSession(false);\r
+ if (session != null) {\r
+ // session can be null in case of logout\r
+ locale = (Locale) session.getAttribute("locale");\r
+ }\r
+ // if there is nothing in the session get it fron the accept-language\r
+ if (locale == null) {\r
+ locale = aRequest.getLocale();\r
+ }\r
+ return locale;\r
+ }\r
+\r
+ public void handleRequest(HttpServletRequest aRequest,\r
+ HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {\r
+ // look for requested method's name in the "do" http request param,\r
+ // if not present, use default action\r
+ String handlerName = aRequest.getParameter("do");\r
+ getLogger().debug("ServletModuleDispatch: " + getClass().getName() + "." + handlerName);\r
+\r
+ if (handlerName ==null) {\r
+ handlerName = "defaultAction";\r
+ }\r
+\r
+ Method method;\r
+ try {\r
+ method = getClass().getMethod(handlerName, HANDLER_METHOD_SIGNATURE);\r
+ }\r
+ catch (NoSuchMethodException e) {\r
+ throw new ServletModuleFailure("No handler found", e);\r
+ }\r
+\r
+ // ok, we have the method's name, now call it\r
+ try {\r
+ method.invoke(this, new Object[] { aRequest,aResponse });\r
+ }\r
+ catch (InvocationTargetException e) {\r
+ getLogger().error("Error while dispatching: " + e.getTargetException().getMessage(),\r
+ e.getTargetException());\r
+\r
+ throw new ServletModuleFailure(e.getTargetException().getMessage(), e.getTargetException());\r
+ }\r
+ catch (IllegalAccessException e) {\r
+ throw new ServletModuleFailure(e);\r
+ }\r
+ }\r
+\r
+}\r