3d0b72fca1ad8251f46aa67d6249c8c25997ca60
[mir.git] / source / mir / servlet / ServletModule.java
1 /*\r
2  * Copyright (C) 2005 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
29  */\r
30 package mir.servlet;\r
31 \r
32 import mir.log.LoggerWrapper;\r
33 import mir.config.MirPropertiesConfiguration;\r
34 \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
41 \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
47 \r
48   protected ServletModule() {\r
49   }\r
50 \r
51   /**\r
52    * Return the name of this module\r
53    */\r
54   protected String getName() {\r
55     return getClass().getName().substring("mircoders.servlet.ServletModule".length());\r
56   }\r
57 \r
58   /**\r
59    * Return the logger for this module\r
60    */\r
61   protected LoggerWrapper getLogger() {\r
62     return logger;\r
63   }\r
64 \r
65   /**\r
66    * Return the global mir configuration\r
67    */\r
68   protected MirPropertiesConfiguration getConfiguration() {\r
69     return configuration;\r
70   }\r
71 \r
72   /**\r
73    * signature of request handling methods\r
74    */\r
75   private static final Class[] HANDLER_METHOD_SIGNATURE = {\r
76       HttpServletRequest.class, HttpServletResponse.class\r
77   };\r
78 \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
83   }\r
84 \r
85   /**\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
88    */\r
89   protected Locale getFallbackLocale(HttpServletRequest aRequest) {\r
90     return fallbackLocale;\r
91   }\r
92 \r
93   public Locale[] getLocales(HttpServletRequest aRequest) {\r
94     return new Locale[] { getLocale(aRequest), fallbackLocale };\r
95   }\r
96 \r
97   /**\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
100    */\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
107     }\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
111     }\r
112     return locale;\r
113   }\r
114 \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
121 \r
122     if (handlerName ==null) {\r
123       handlerName = "defaultAction";\r
124     }\r
125 \r
126     Method method;\r
127     try {\r
128       method = getClass().getMethod(handlerName, HANDLER_METHOD_SIGNATURE);\r
129     }\r
130     catch (NoSuchMethodException e) {\r
131       throw new ServletModuleFailure("No handler found", e);\r
132     }\r
133 \r
134     // ok, we have the method's name, now call it\r
135     try {\r
136         method.invoke(this, new Object[] { aRequest,aResponse });\r
137     }\r
138     catch (InvocationTargetException e) {\r
139       getLogger().error("Error while dispatching: " + e.getTargetException().getMessage(),\r
140           e.getTargetException());\r
141 \r
142       throw new ServletModuleFailure(e.getTargetException().getMessage(), e.getTargetException());\r
143     }\r
144     catch (IllegalAccessException e) {\r
145       throw new ServletModuleFailure(e);\r
146     }\r
147   }\r
148 \r
149 }\r