reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / servlet / ServletModule.java
1 /*\r
2  * Copyright (C) 2001-2006 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  * 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
27  */\r
28 package mir.servlet;\r
29 \r
30 import mir.log.LoggerWrapper;\r
31 import mir.config.MirPropertiesConfiguration;\r
32 \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
39 \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
45 \r
46   protected ServletModule() {\r
47   }\r
48 \r
49   /**\r
50    * Return the name of this module\r
51    */\r
52   protected String getName() {\r
53     return getClass().getName().substring("mircoders.servlet.ServletModule".length());\r
54   }\r
55 \r
56   /**\r
57    * Return the logger for this module\r
58    */\r
59   protected LoggerWrapper getLogger() {\r
60     return logger;\r
61   }\r
62 \r
63   /**\r
64    * Return the global mir configuration\r
65    */\r
66   protected MirPropertiesConfiguration getConfiguration() {\r
67     return configuration;\r
68   }\r
69 \r
70   /**\r
71    * signature of request handling methods\r
72    */\r
73   private static final Class[] HANDLER_METHOD_SIGNATURE = {\r
74       HttpServletRequest.class, HttpServletResponse.class\r
75   };\r
76 \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
81   }\r
82 \r
83   /**\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
86    */\r
87   protected Locale getFallbackLocale(HttpServletRequest aRequest) {\r
88     return fallbackLocale;\r
89   }\r
90 \r
91   public Locale[] getLocales(HttpServletRequest aRequest) {\r
92     return new Locale[] { getLocale(aRequest), fallbackLocale };\r
93   }\r
94 \r
95   /**\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
98    */\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
105     }\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
109     }\r
110     return locale;\r
111   }\r
112 \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
119 \r
120     if (handlerName ==null) {\r
121       handlerName = "defaultAction";\r
122     }\r
123 \r
124     Method method;\r
125     try {\r
126       method = getClass().getMethod(handlerName, HANDLER_METHOD_SIGNATURE);\r
127     }\r
128     catch (NoSuchMethodException e) {\r
129       throw new ServletModuleFailure("No handler found", e);\r
130     }\r
131 \r
132     // ok, we have the method's name, now call it\r
133     try {\r
134         method.invoke(this, new Object[] { aRequest,aResponse });\r
135     }\r
136     catch (InvocationTargetException e) {\r
137       getLogger().error("Error while dispatching: " + e.getTargetException().getMessage(),\r
138           e.getTargetException());\r
139 \r
140       throw new ServletModuleFailure(e.getTargetException().getMessage(), e.getTargetException());\r
141     }\r
142     catch (IllegalAccessException e) {\r
143       throw new ServletModuleFailure(e);\r
144     }\r
145   }\r
146 \r
147 }\r