2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Locale;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.http.HttpSession;
44 import mir.config.MirPropertiesConfiguration;
45 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
46 import mir.entity.EntityList;
47 import mir.log.LoggerWrapper;
48 import mir.misc.HTMLParseException;
49 import mir.misc.HTMLTemplateProcessor;
50 import mir.misc.LineFilterWriter;
51 import mir.module.AbstractModule;
52 import mir.module.ModuleException;
53 import mir.storage.StorageObject;
54 import mir.storage.StorageObjectFailure;
55 import freemarker.template.SimpleHash;
56 import freemarker.template.TemplateModelRoot;
60 * Abstract class ServletModule provides the base functionality for servlets.
61 * Deriving a class from ServletModule enables class to insert/edit/update/delete
62 * and list Entity from a Database via mainModule.
65 * Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der
66 * abgeleiteten ServletModule zur Verf?gung.
72 public abstract class ServletModule {
74 public String defaultAction;
75 protected LoggerWrapper logger;
76 protected MirPropertiesConfiguration configuration;
77 protected AbstractModule mainModule;
78 protected String templateListString;
79 protected String templateObjektString;
80 protected String templateConfirmString;
83 public ServletModule(){
85 configuration = MirPropertiesConfiguration.instance();
86 } catch (PropertiesConfigExc e) {
87 e.printStackTrace(System.err);
93 * Singelton - Methode muss in den abgeleiteten Klassen ueberschrieben werden.
94 * @return ServletModule
96 public static ServletModule getInstance() {
101 * get the module name to be used for generic operations like delete.
103 protected String getOperationModuleName() {
104 return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
108 * get the session binded language
110 public String getLanguage(HttpServletRequest req) {
111 HttpSession session = req.getSession(false);
112 String language = (String) session.getAttribute("Language");
113 if (language == null) {
114 language = configuration.getString("StandardLanguage");
120 * get the locale either from the session or the accept-language header ot the request
121 * this supersedes getLanguage for the new i18n
123 public Locale getLocale(HttpServletRequest req) {
125 HttpSession session = req.getSession(false);
126 if (session != null) {
127 // session can be null in case of logout
128 loc = (Locale) session.getAttribute("Locale");
130 // if there is nothing in the session get it fron the accept-language
132 loc = req.getLocale();
137 public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleException {
139 aResponse.sendRedirect(MirPropertiesConfiguration.instance().getString("RootUri") + "/Mir?"+aQuery);
141 catch (Throwable t) {
142 throw new ServletModuleException(t.getMessage());
147 * list(req,res) - generische Listmethode. Wennn die Funktionalitaet
148 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
149 * ueberschreiben werden.
151 * @param req Http-Request, das vom Dispatcher durchgereicht wird
152 * @param res Http-Response, die vom Dispatcher durchgereicht wird
154 public void list(HttpServletRequest req, HttpServletResponse res)
155 throws ServletModuleException {
158 String offsetParam = req.getParameter("offset");
160 PrintWriter out = res.getWriter();
162 // hier offsetcode bearbeiten
163 if (offsetParam != null && !offsetParam.equals("")) {
164 offset = Integer.parseInt(offsetParam);
166 if (req.getParameter("next") != null) {
167 offset = Integer.parseInt(req.getParameter("nextoffset"));
170 if (req.getParameter("prev") != null) {
171 offset = Integer.parseInt(req.getParameter("prevoffset"));
174 theList = mainModule.getByWhereClause(null, offset);
176 HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
178 catch (Exception e) {
179 throw new ServletModuleException(e.getMessage());
184 * add(req,res) - generische Addmethode. Wennn die Funktionalitaet
185 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
186 * ueberschreiben werden.
187 * @param req Http-Request, das vom Dispatcher durchgereicht wird
188 * @param res Http-Response, die vom Dispatcher durchgereicht wird
190 public void add(HttpServletRequest req, HttpServletResponse res)
191 throws ServletModuleException {
194 SimpleHash mergeData = new SimpleHash();
195 mergeData.put("new", "1");
196 deliver(req, res, mergeData, templateObjektString);
198 catch (Exception e) {
199 throw new ServletModuleException(e.getMessage());
204 * insert(req,res) - generische Insertmethode, folgt auf add.
205 * Wennn die Funktionalitaet
206 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
207 * ueberschreiben werden.
209 * @param req Http-Request, das vom Dispatcher durchgereicht wird
210 * @param res Http-Response, die vom Dispatcher durchgereicht wird
212 public void insert(HttpServletRequest req, HttpServletResponse res)
213 throws ServletModuleException, ServletModuleUserException {
215 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
216 logger.debug("--trying to add...");
217 String id = mainModule.add(withValues);
218 logger.debug("--trying to deliver..." + id);
221 catch (Exception e) {
222 throw new ServletModuleException(e.getMessage());
227 * delete(req,res) - generic delete method. Can be overridden in subclasses.
231 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
233 String idParam = req.getParameter("id");
236 throw new ServletModuleException("Invalid call to delete: no id supplied");
238 String confirmParam = req.getParameter("confirm");
239 String cancelParam = req.getParameter("cancel");
240 if (confirmParam == null && cancelParam == null) {
241 SimpleHash mergeData = new SimpleHash();
243 mergeData.put("module", getOperationModuleName());
244 mergeData.put("infoString", getOperationModuleName() + ": " + idParam);
245 mergeData.put("id", idParam);
246 mergeData.put("where", req.getParameter("where"));
247 mergeData.put("order", req.getParameter("order"));
248 mergeData.put("offset", req.getParameter("offset"));
249 // this stuff is to be compatible with the other more advanced
250 // search method used for media and comments
251 mergeData.put("query_media_folder", req.getParameter("query_media_folder"));
252 mergeData.put("query_is_published", req.getParameter("query_is_published"));
253 mergeData.put("query_text", req.getParameter("query_text"));
254 mergeData.put("query_field", req.getParameter("query_field"));
256 deliver(req, res, mergeData, templateConfirmString);
259 if (confirmParam != null && !confirmParam.equals("")) {
260 //theLog.printInfo("delete confirmed!");
261 mainModule.deleteById(idParam);
262 list(req, res); // back to list
265 if (req.getParameter("where") != null)
272 catch (Exception e) {
273 throw new ServletModuleException(e.getMessage());
278 * edit(req,res) - generische Editmethode. Wennn die Funktionalitaet
279 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
280 * ueberschreiben werden.
282 * @param req Http-Request, das vom Dispatcher durchgereicht wird
283 * @param res Http-Response, die vom Dispatcher durchgereicht wird
285 public void edit(HttpServletRequest req, HttpServletResponse res)
286 throws ServletModuleException {
288 String idParam = req.getParameter("id");
289 deliver(req, res, mainModule.getById(idParam), templateObjektString);
291 catch (ModuleException e) {
292 throw new ServletModuleException(e.getMessage());
297 * update(req,res) - generische Updatemethode. Wennn die Funktionalitaet
298 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
299 * ueberschreiben werden.
301 * @param req Http-Request, das vom Dispatcher durchgereicht wird
302 * @param res Http-Response, die vom Dispatcher durchgereicht wird
305 public void update(HttpServletRequest req, HttpServletResponse res)
306 throws ServletModuleException {
308 String idParam = req.getParameter("id");
309 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
311 String id = mainModule.set(withValues);
312 String whereParam = req.getParameter("where");
313 String orderParam = req.getParameter("order");
315 if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) {
322 catch (Exception e) {
323 throw new ServletModuleException(e.getMessage());
328 * deliver liefert das Template mit dem Filenamen templateFilename
329 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
330 * TemplateModelRoot rtm gemischt wurde
332 * @param res Http-Response, die vom Dispatcher durchgereicht wird
333 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
334 * Daten, die ins Template gemerged werden sollen.
335 * @param tmpl Name des Templates
336 * @exception ServletModuleException
338 public void deliver(HttpServletRequest req, HttpServletResponse res,
339 TemplateModelRoot rtm, TemplateModelRoot popups,
340 String templateFilename)
341 throws ServletModuleException {
342 if (rtm == null) rtm = new SimpleHash();
344 PrintWriter out = res.getWriter();
345 HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out, getLocale(req));
347 // we default to admin bundles here, which is not exactly beautiful...
348 // but this whole producer stuff is going to be rewritten soon.
349 // ServletModuleOpenIndy overwrites deliver() to use open bundles
353 catch (HTMLParseException e) {
354 throw new ServletModuleException(e.getMessage());
355 } catch (IOException e) {
356 throw new ServletModuleException(e.getMessage());
362 * deliver liefert das Template mit dem Filenamen templateFilename
363 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
364 * TemplateModelRoot rtm gemischt wurde
366 * @param res Http-Response, die vom Dispatcher durchgereicht wird
367 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
368 * Daten, die ins Template gemerged werden sollen.
369 * @param tmpl Name des Templates
370 * @exception ServletModuleException
372 public void deliver(HttpServletRequest req, HttpServletResponse res,
373 TemplateModelRoot rtm, String templateFilename)
374 throws ServletModuleException {
375 deliver(req, res, rtm, null, templateFilename);
379 * deliver liefert das Template mit dem Filenamen templateFilename
380 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
381 * TemplateModelRoot rtm gemischt wurde
383 * @param res Http-Response, die vom Dispatcher durchgereicht wird
384 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
385 * Daten, die ins Template gemerged werden sollen.
386 * @param tmpl Name des Templates
387 * @exception ServletModuleException
389 public void deliver_compressed(HttpServletRequest req, HttpServletResponse res,
390 TemplateModelRoot rtm, String templateFilename)
391 throws ServletModuleException {
392 if (rtm == null) rtm = new SimpleHash();
394 PrintWriter out = new LineFilterWriter(res.getWriter());
395 //PrintWriter out = res.getWriter();
396 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
399 catch (HTMLParseException e) {
400 throw new ServletModuleException(e.getMessage());
402 catch (IOException e) {
403 throw new ServletModuleException(e.getMessage());
408 * deliver liefert das Template mit dem Filenamen templateFilename
409 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
410 * TemplateModelRoot rtm gemischt wurde
412 * @param out ist der OutputStream, in den die gergten Daten geschickt werden sollen.
413 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
414 * Daten, die ins Template gemerged werden sollen.
415 * @param tmpl Name des Templates
416 * @exception ServletModuleException
418 private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out,
419 TemplateModelRoot rtm, String templateFilename)
420 throws HTMLParseException {
421 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
425 * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
426 * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
427 * doParameters ausgefuehrt.
429 * @return Name der Default-Action
431 public String defaultAction() {
432 return defaultAction;
436 * Hier kann vor der Datenaufbereitung schon mal ein response geschickt
437 * werden (um das subjektive Antwortverhalten bei langsamen Verbindungen
440 public void predeliver(HttpServletRequest req, HttpServletResponse res) {
445 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
446 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
448 * @return HashMap mit den Werten
450 public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
451 throws ServletModuleException {
452 ArrayList theFieldList;
454 theFieldList = theStorage.getFields();
456 catch (StorageObjectFailure e) {
457 throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage());
460 HashMap withValues = new HashMap();
461 String aField, aValue;
463 for (int i = 0; i < theFieldList.size(); i++) {
464 aField = (String) theFieldList.get(i);
465 aValue = req.getParameter(aField);
466 if (aValue != null) withValues.put(aField, aValue);