ServletModule exception cleanup + different error templates for admin + open postings...
[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.util.Locale;\r
35 \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 mir.config.MirPropertiesConfiguration;\r
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
45 import mir.log.LoggerWrapper;\r
46 import mir.storage.DatabaseAdaptor;\r
47 \r
48 import com.codestudio.util.JDBCPool;\r
49 import com.codestudio.util.JDBCPoolMetaData;\r
50 import com.codestudio.util.SQLManager;\r
51 \r
52 /**\r
53  * Title:        Mir\r
54  * Description:  Abstract servlet-class\r
55  * Copyright:    Copyright (c) 2001, 2002\r
56  * Company:      Mir-coders group\r
57  * @author       idfx, the Mir-coders group\r
58  * @version      $Id: AbstractServlet.java,v 1.23 2003/03/06 05:40:39 zapata Exp $\r
59  */\r
60 \r
61 public abstract class AbstractServlet extends HttpServlet {\r
62   protected static String lang;\r
63   protected LoggerWrapper logger;\r
64   protected MirPropertiesConfiguration configuration;\r
65 \r
66   /**\r
67    * Constructor for AbstractServlet.\r
68    */\r
69   public AbstractServlet() {\r
70     super();\r
71     logger = new LoggerWrapper("Servlet");\r
72   }\r
73 \r
74   protected void setNoCaching(HttpServletResponse aResponse) {\r
75     //nothing in Mir can or should be cached as it's all dynamic...\r
76     //\r
77     //this needs to be done here and not per page (via meta tags) as some\r
78     //browsers have problems w/ it per-page -mh\r
79     aResponse.setHeader("Pragma", "no-cache");\r
80     aResponse.setDateHeader("Expires", 0);\r
81     aResponse.setHeader("Cache-Control", "no-cache");\r
82   }\r
83 \r
84   /**\r
85    * Bind the language to the session\r
86    */\r
87   protected void setLanguage(HttpSession session, String language) {\r
88     session.setAttribute("Language", language);\r
89   }\r
90 \r
91   protected void setLocale(HttpSession session, Locale loc) {\r
92     session.setAttribute("Locale", loc);\r
93   }\r
94 \r
95   /**\r
96    * Get the session-bound language\r
97    */\r
98   protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {\r
99     String lang = (String) session.getAttribute("Language");\r
100 \r
101     if (lang == null || lang.length()==0) {\r
102       lang = getAcceptLanguage(aRequest);\r
103     }\r
104 \r
105     return lang;\r
106   }\r
107 \r
108   /**\r
109    * get the locale either from the session or the accept-language header ot the request\r
110    * this supersedes getLanguage for the new i18n\r
111    */\r
112   public Locale getLocale(HttpServletRequest aRequest) {\r
113     Locale loc = null;\r
114     HttpSession session = aRequest.getSession(false);\r
115     if (session != null) {\r
116       // session can be null in case of logout\r
117       loc = (Locale) session.getAttribute("Locale");\r
118     }\r
119     // if there is nothing in the session get it fron the accept-language\r
120     if (loc == null) {\r
121       loc = aRequest.getLocale();\r
122     }\r
123     return loc;\r
124   }\r
125 \r
126   /**\r
127    * Checks the Accept-Language of the client browser.\r
128    * If this language is available it returns its country-code,\r
129    * else it returns the standard-language\r
130    */\r
131   protected String getAcceptLanguage(HttpServletRequest aRequest) {\r
132     Locale loc = aRequest.getLocale();\r
133     lang = loc.getLanguage();\r
134     return lang;\r
135   }\r
136 \r
137   /**\r
138    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)\r
139    */\r
140   public void init(ServletConfig config) throws ServletException {\r
141     super.init(config);\r
142     MirPropertiesConfiguration.setContext(config.getServletContext());\r
143     try {\r
144       configuration = MirPropertiesConfiguration.instance();\r
145     }\r
146     catch (PropertiesConfigExc e) {\r
147       throw new ServletException(e);\r
148     }\r
149 \r
150     String dbUser = configuration.getString("Database.Username");\r
151     String dbPassword = configuration.getString("Database.Password");\r
152     String dbHost = configuration.getString("Database.Host");\r
153     String dbAdapName = configuration.getString("Database.Adaptor");\r
154     String dbName = configuration.getString("Database.Name");\r
155 \r
156     DatabaseAdaptor adaptor;\r
157     try {\r
158       adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();\r
159     }\r
160     catch (Exception e) {\r
161       throw new ServletException("Could not load DB adapator: " +\r
162                                  e.toString());\r
163     }\r
164 \r
165     String dbDriver;\r
166     String dbUrl;\r
167     try {\r
168       dbDriver = adaptor.getDriver();\r
169       dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);\r
170     }\r
171     catch (Exception e) {\r
172       throw new ServletException(e);\r
173     }\r
174 \r
175     JDBCPoolMetaData meta = new JDBCPoolMetaData();\r
176     meta.setDbname(dbName);\r
177     meta.setDriver(dbDriver);\r
178     meta.setURL(dbUrl);\r
179     meta.setUserName(dbUser);\r
180     meta.setPassword(dbPassword);\r
181     meta.setJNDIName("mir");\r
182     meta.setMaximumSize(10);\r
183     meta.setMinimumSize(1);\r
184     meta.setPoolPreparedStatements(false);\r
185     meta.setCacheEnabled(false);\r
186     meta.setCacheSize(15);\r
187     meta.setDebugging(false);\r
188 //    meta.setLogFile(dblogfile+".pool");\r
189 \r
190     SQLManager manager = SQLManager.getInstance();\r
191     JDBCPool pool = null;\r
192     if (manager != null) {\r
193       pool = manager.createPool(meta);\r
194     }\r
195   }\r
196 }\r