2 * Copyright (C) 2001-2006 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 * and distribute linked combinations including the two. You must obey the
\r
23 * GNU General Public License in all respects for all of the code used other than
\r
24 * the above mentioned libraries. If you modify this file, you may extend this
\r
25 * exception to your version of the file, but you are not obligated to do so.
\r
26 * If you do not wish to do so, delete this exception statement from your version.
\r
28 package mir.servlet;
\r
30 import mir.log.LoggerWrapper;
\r
31 import mir.config.MirPropertiesConfiguration;
\r
33 import javax.servlet.http.HttpServletRequest;
\r
34 import javax.servlet.http.HttpServletResponse;
\r
35 import javax.servlet.http.HttpSession;
\r
36 import java.lang.reflect.Method;
\r
37 import java.lang.reflect.InvocationTargetException;
\r
38 import java.util.Locale;
\r
40 public abstract class ServletModule {
\r
41 private LoggerWrapper logger = new LoggerWrapper("ServletModule." + getName());
\r
42 private MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
\r
43 private final Locale fallbackLocale = new Locale(
\r
44 getConfiguration().getString("Mir.Admin.FallbackLanguage", "en"), "");
\r
46 protected ServletModule() {
\r
50 * Return the name of this module
\r
52 protected String getName() {
\r
53 return getClass().getName().substring("mircoders.servlet.ServletModule".length());
\r
57 * Return the logger for this module
\r
59 protected LoggerWrapper getLogger() {
\r
64 * Return the global mir configuration
\r
66 protected MirPropertiesConfiguration getConfiguration() {
\r
67 return configuration;
\r
71 * signature of request handling methods
\r
73 private static final Class[] HANDLER_METHOD_SIGNATURE = {
\r
74 HttpServletRequest.class, HttpServletResponse.class
\r
77 protected void defaultAction(HttpServletRequest aRequest,
\r
78 HttpServletResponse aResponse)
\r
79 throws ServletModuleExc, ServletModuleFailure, ServletModuleUserExc {
\r
80 throw new ServletModuleExc("default action not defined for module " + getName());
\r
84 * get the locale either from the session or the accept-language header ot the request
\r
85 * this supersedes getLanguage for the new i18n
\r
87 protected Locale getFallbackLocale(HttpServletRequest aRequest) {
\r
88 return fallbackLocale;
\r
91 public Locale[] getLocales(HttpServletRequest aRequest) {
\r
92 return new Locale[] { getLocale(aRequest), fallbackLocale };
\r
96 * Return the locale either from the session or the accept-language header ot the request
\r
97 * this supersedes getLanguage for the new i18n
\r
99 public static Locale getLocale(HttpServletRequest aRequest) {
\r
100 Locale locale = null;
\r
101 HttpSession session = aRequest.getSession(false);
\r
102 if (session != null) {
\r
103 // session can be null in case of logout
\r
104 locale = (Locale) session.getAttribute("locale");
\r
106 // if there is nothing in the session get it fron the accept-language
\r
107 if (locale == null) {
\r
108 locale = aRequest.getLocale();
\r
113 public void handleRequest(HttpServletRequest aRequest,
\r
114 HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
\r
115 // look for requested method's name in the "do" http request param,
\r
116 // if not present, use default action
\r
117 String handlerName = aRequest.getParameter("do");
\r
118 getLogger().debug("ServletModuleDispatch: " + getClass().getName() + "." + handlerName);
\r
120 if (handlerName ==null) {
\r
121 handlerName = "defaultAction";
\r
126 method = getClass().getMethod(handlerName, HANDLER_METHOD_SIGNATURE);
\r
128 catch (NoSuchMethodException e) {
\r
129 throw new ServletModuleFailure("No handler found", e);
\r
132 // ok, we have the method's name, now call it
\r
134 method.invoke(this, new Object[] { aRequest,aResponse });
\r
136 catch (InvocationTargetException e) {
\r
137 getLogger().error("Error while dispatching: " + e.getTargetException().getMessage(),
\r
138 e.getTargetException());
\r
140 throw new ServletModuleFailure(e.getTargetException().getMessage(), e.getTargetException());
\r
142 catch (IllegalAccessException e) {
\r
143 throw new ServletModuleFailure(e);
\r