Various fixes + Upgrades to the FileEdit module:
[mir.git] / source / mir / servlet / AbstractServlet.java
1 /*\r
2  * Copyright (C) 2001, 2002  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 the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mir.servlet;\r
33 \r
34 import java.io.IOException;\r
35 import java.util.Locale;\r
36 import javax.servlet.ServletConfig;\r
37 import javax.servlet.ServletException;\r
38 import javax.servlet.http.HttpServlet;\r
39 import javax.servlet.http.HttpServletRequest;\r
40 import javax.servlet.http.HttpServletResponse;\r
41 import javax.servlet.http.HttpSession;\r
42 \r
43 import com.codestudio.util.JDBCPool;\r
44 import com.codestudio.util.JDBCPoolMetaData;\r
45 import com.codestudio.util.SQLManager;\r
46 import mir.config.MirPropertiesConfiguration;\r
47 import mir.config.MirPropertiesConfiguration$PropertiesConfigExc;\r
48 import mir.log.LoggerWrapper;\r
49 import mir.storage.DatabaseAdaptor;\r
50 \r
51 /**\r
52  * Title:        Mir\r
53  * Description:  Abstract servlet-class\r
54  * Copyright:    Copyright (c) 2001, 2002\r
55  * Company:      Mir-coders group\r
56  * @author       idfx, the Mir-coders group\r
57  * @version      $Id: AbstractServlet.java,v 1.25 2003/03/17 20:47:03 zapata Exp $\r
58  */\r
59 \r
60 public abstract class AbstractServlet extends HttpServlet {\r
61   protected static String lang;\r
62   protected LoggerWrapper logger;\r
63   protected MirPropertiesConfiguration configuration;\r
64 \r
65   /**\r
66    * Constructor for AbstractServlet.\r
67    */\r
68   public AbstractServlet() {\r
69     super();\r
70     logger = new LoggerWrapper("Servlet");\r
71   }\r
72 \r
73   protected void setNoCaching(HttpServletResponse aResponse) {\r
74     //nothing in Mir can or should be cached as it's all dynamic...\r
75     //\r
76     //this needs to be done here and not per page (via meta tags) as some\r
77     //browsers have problems w/ it per-page -mh\r
78     aResponse.setHeader("Pragma", "no-cache");\r
79     aResponse.setDateHeader("Expires", 0);\r
80     aResponse.setHeader("Cache-Control", "no-cache");\r
81   }\r
82 \r
83   /**\r
84    * Bind the language to the session\r
85    */\r
86   protected void setLanguage(HttpSession session, String language) {\r
87     logger.debug("setting language to " + language);\r
88 \r
89     session.setAttribute("language", language);\r
90     session.setAttribute("locale", new Locale(language, ""));\r
91   }\r
92 \r
93   /**\r
94    * Get the session-bound language\r
95    */\r
96   protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {\r
97     String lang = (String) session.getAttribute("language");\r
98 \r
99     if (lang == null || lang.length()==0) {\r
100       lang = getAcceptLanguage(aRequest);\r
101     }\r
102 \r
103     return lang;\r
104   }\r
105 \r
106   /**\r
107    * get the locale either from the session or the accept-language header ot the request\r
108    * this supersedes getLanguage for the new i18n\r
109    */\r
110   public Locale getLocale(HttpServletRequest aRequest) {\r
111     Locale loc = null;\r
112     HttpSession session = aRequest.getSession(false);\r
113     if (session != null) {\r
114       // session can be null in case of logout\r
115       loc = (Locale) session.getAttribute("locale");\r
116     }\r
117     // if there is nothing in the session get it fron the accept-language\r
118     if (loc == null) {\r
119       loc = aRequest.getLocale();\r
120     }\r
121 \r
122     logger.debug("getting locale: " + loc.getLanguage());\r
123 \r
124     return loc;\r
125   }\r
126 \r
127   /**\r
128    * Checks the Accept-Language of the client browser.\r
129    * If this language is available it returns its country-code,\r
130    * else it returns the standard-language\r
131    */\r
132   protected String getAcceptLanguage(HttpServletRequest aRequest) {\r
133     Locale loc = aRequest.getLocale();\r
134     lang = loc.getLanguage();\r
135     return lang;\r
136   }\r
137 \r
138   /**\r
139    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)\r
140    */\r
141   public void init(ServletConfig config) throws ServletException {\r
142     super.init(config);\r
143 \r
144     MirPropertiesConfiguration.setContext(config.getServletContext());\r
145     try {\r
146       configuration = MirPropertiesConfiguration.instance();\r
147     }\r
148     catch (PropertiesConfigExc e) {\r
149       throw new ServletException(e);\r
150     }\r
151 \r
152     String dbUser = configuration.getString("Database.Username");\r
153     String dbPassword = configuration.getString("Database.Password");\r
154     String dbHost = configuration.getString("Database.Host");\r
155     String dbAdapName = configuration.getString("Database.Adaptor");\r
156     String dbName = configuration.getString("Database.Name");\r
157 \r
158     DatabaseAdaptor adaptor;\r
159     try {\r
160       adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();\r
161     }\r
162     catch (Exception e) {\r
163       throw new ServletException("Could not load DB adapator: " +\r
164                                  e.toString());\r
165     }\r
166 \r
167     String dbDriver;\r
168     String dbUrl;\r
169     try {\r
170       dbDriver = adaptor.getDriver();\r
171       dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);\r
172     }\r
173     catch (Exception e) {\r
174       throw new ServletException(e);\r
175     }\r
176 \r
177     JDBCPoolMetaData meta = new JDBCPoolMetaData();\r
178     meta.setDbname(dbName);\r
179     meta.setDriver(dbDriver);\r
180     meta.setURL(dbUrl);\r
181     meta.setUserName(dbUser);\r
182     meta.setPassword(dbPassword);\r
183     meta.setJNDIName("mir");\r
184     meta.setMaximumSize(10);\r
185     meta.setMinimumSize(1);\r
186     meta.setPoolPreparedStatements(false);\r
187     meta.setCacheEnabled(false);\r
188     meta.setCacheSize(15);\r
189     meta.setDebugging(false);\r
190 //    meta.setLogFile(dblogfile+".pool");\r
191 \r
192     SQLManager manager = SQLManager.getInstance();\r
193     JDBCPool pool = null;\r
194     if (manager != null) {\r
195       pool = manager.createPool(meta);\r
196     }\r
197   }\r
198 \r
199 \r
200   protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
201     doPost(request, response);\r
202   }\r
203 \r
204   protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
205     if ( (configuration.getString("RootUri") == null) ||\r
206         configuration.getString("RootUri").equals("")) {\r
207       configuration.setProperty("RootUri", request.getContextPath());\r
208     }\r
209     process(request, response);\r
210   }\r
211 \r
212   abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;\r
213 \r
214 }\r