2 * Copyright (C) 2005 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mir.servlet;
\r
32 import mir.log.LoggerWrapper;
\r
33 import mir.config.MirPropertiesConfiguration;
\r
35 import javax.servlet.http.HttpServletRequest;
\r
36 import javax.servlet.http.HttpServletResponse;
\r
37 import javax.servlet.http.HttpSession;
\r
38 import java.lang.reflect.Method;
\r
39 import java.lang.reflect.InvocationTargetException;
\r
40 import java.util.Locale;
\r
42 public abstract class ServletModule {
\r
43 private LoggerWrapper logger = new LoggerWrapper("ServletModule." + getName());
\r
44 private MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
\r
45 private final Locale fallbackLocale = new Locale(
\r
46 getConfiguration().getString("Mir.Admin.FallbackLanguage", "en"), "");
\r
48 protected ServletModule() {
\r
52 * Return the name of this module
\r
54 protected String getName() {
\r
55 return getClass().getName().substring("mircoders.servlet.ServletModule".length());
\r
59 * Return the logger for this module
\r
61 protected LoggerWrapper getLogger() {
\r
66 * Return the global mir configuration
\r
68 protected MirPropertiesConfiguration getConfiguration() {
\r
69 return configuration;
\r
73 * signature of request handling methods
\r
75 private static final Class[] HANDLER_METHOD_SIGNATURE = {
\r
76 HttpServletRequest.class, HttpServletResponse.class
\r
79 protected void defaultAction(HttpServletRequest aRequest,
\r
80 HttpServletResponse aResponse)
\r
81 throws ServletModuleExc, ServletModuleFailure, ServletModuleUserExc {
\r
82 throw new ServletModuleExc("default action not defined for module " + getName());
\r
86 * get the locale either from the session or the accept-language header ot the request
\r
87 * this supersedes getLanguage for the new i18n
\r
89 protected Locale getFallbackLocale(HttpServletRequest aRequest) {
\r
90 return fallbackLocale;
\r
93 public Locale[] getLocales(HttpServletRequest aRequest) {
\r
94 return new Locale[] { getLocale(aRequest), fallbackLocale };
\r
98 * Return the locale either from the session or the accept-language header ot the request
\r
99 * this supersedes getLanguage for the new i18n
\r
101 public static Locale getLocale(HttpServletRequest aRequest) {
\r
102 Locale locale = null;
\r
103 HttpSession session = aRequest.getSession(false);
\r
104 if (session != null) {
\r
105 // session can be null in case of logout
\r
106 locale = (Locale) session.getAttribute("locale");
\r
108 // if there is nothing in the session get it fron the accept-language
\r
109 if (locale == null) {
\r
110 locale = aRequest.getLocale();
\r
115 public void handleRequest(HttpServletRequest aRequest,
\r
116 HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
\r
117 // look for requested method's name in the "do" http request param,
\r
118 // if not present, use default action
\r
119 String handlerName = aRequest.getParameter("do");
\r
120 getLogger().debug("ServletModuleDispatch: " + getClass().getName() + "." + handlerName);
\r
122 if (handlerName ==null) {
\r
123 handlerName = "defaultAction";
\r
128 method = getClass().getMethod(handlerName, HANDLER_METHOD_SIGNATURE);
\r
130 catch (NoSuchMethodException e) {
\r
131 throw new ServletModuleFailure("No handler found", e);
\r
134 // ok, we have the method's name, now call it
\r
136 method.invoke(this, new Object[] { aRequest,aResponse });
\r
138 catch (InvocationTargetException e) {
\r
139 getLogger().error("Error while dispatching: " + e.getTargetException().getMessage(),
\r
140 e.getTargetException());
\r
142 throw new ServletModuleFailure(e.getTargetException().getMessage(), e.getTargetException());
\r
144 catch (IllegalAccessException e) {
\r
145 throw new ServletModuleFailure(e);
\r