2 * Copyright (C) 2001, 2002 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 * 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
30 package mir.servlet;
\r
32 import java.io.IOException;
\r
33 import java.lang.reflect.InvocationTargetException;
\r
34 import java.lang.reflect.Method;
\r
35 import java.sql.Connection;
\r
36 import java.util.Locale;
\r
38 import javax.servlet.ServletConfig;
\r
39 import javax.servlet.ServletException;
\r
40 import javax.servlet.http.HttpServlet;
\r
41 import javax.servlet.http.HttpServletRequest;
\r
42 import javax.servlet.http.HttpServletResponse;
\r
43 import javax.servlet.http.HttpSession;
\r
45 import com.codestudio.util.JDBCPool;
\r
46 import com.codestudio.util.JDBCPoolMetaData;
\r
47 import com.codestudio.util.SQLManager;
\r
49 import mir.config.MirPropertiesConfiguration;
\r
50 import mir.log.LoggerWrapper;
\r
52 import mircoders.global.MirGlobal;
\r
56 * Description: Abstract servlet-class
\r
57 * Copyright: Copyright (c) 2001, 2002
\r
58 * Company: Mir-coders group
\r
59 * @author idfx, the Mir-coders group
\r
60 * @version $Id: AbstractServlet.java,v 1.30.2.4 2003/09/03 17:49:38 zapata Exp $
\r
63 public abstract class AbstractServlet extends HttpServlet {
\r
64 protected static String lang;
\r
65 protected LoggerWrapper logger;
\r
66 protected MirPropertiesConfiguration configuration;
\r
69 * Constructor for AbstractServlet.
\r
71 public AbstractServlet() {
\r
75 protected void setNoCaching(HttpServletResponse aResponse) {
\r
76 //nothing in Mir can or should be cached as it's all dynamic...
\r
78 //this needs to be done here and not per page (via meta tags) as some
\r
79 //browsers have problems w/ it per-page -mh
\r
80 aResponse.setHeader("Pragma", "no-cache");
\r
81 aResponse.setDateHeader("Expires", 0);
\r
82 aResponse.setHeader("Cache-Control", "no-cache");
\r
86 * Bind the language to the session
\r
88 protected void setLanguage(HttpSession session, String language) {
\r
89 logger.debug("setting language to " + language);
\r
91 session.setAttribute("language", language);
\r
92 session.setAttribute("locale", new Locale(language, ""));
\r
96 * Get the session-bound language
\r
98 protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {
\r
99 String lang = (String) session.getAttribute("language");
\r
101 if (lang == null || lang.length()==0) {
\r
102 lang = getAcceptLanguage(aRequest);
\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
112 public Locale getLocale(HttpServletRequest aRequest) {
\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
119 // if there is nothing in the session get it fron the accept-language
\r
121 loc = aRequest.getLocale();
\r
124 logger.debug("getting locale: " + loc.getLanguage());
\r
130 * Checks the Accept-Language of the client browser.
\r
131 * If this language is available it returns its country-code,
\r
132 * else it returns the standard-language
\r
134 protected String getAcceptLanguage(HttpServletRequest aRequest) {
\r
135 Locale loc = aRequest.getLocale();
\r
136 lang = loc.getLanguage();
\r
141 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
\r
143 public void init(ServletConfig config) throws ServletException {
\r
144 super.init(config);
\r
146 MirPropertiesConfiguration.setContext(config.getServletContext());
\r
148 configuration = MirPropertiesConfiguration.instance();
\r
150 catch (Throwable t) {
\r
151 throw new ServletException("can't read configuration: " + t.toString());
\r
154 logger = new LoggerWrapper("Servlet");
\r
157 MirGlobal.localizer();
\r
159 catch (Throwable t) {
\r
160 logger.fatal("can't get localizer: " + t.toString());
\r
161 throw new ServletException("can't get localizer: " + t.toString());
\r
164 String dbUser = configuration.getString("Database.Username");
\r
165 String dbPassword = configuration.getString("Database.Password");
\r
166 String dbHost = configuration.getString("Database.Host");
\r
167 String dbPort = configuration.getString("Database.Port");
\r
168 String dbAdapName = configuration.getString("Database.Adaptor");
\r
169 String dbName = configuration.getString("Database.Name");
\r
170 String dbDriver = configuration.getString("Database.Driver");
\r
171 String dbUrl = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName;
\r
172 int dbMin = configuration.getInteger("Database.poolMin", 1);
\r
173 int dbMax = configuration.getInteger("Database.poolMax", 10);
\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(dbMax);
\r
183 meta.setMinimumSize(dbMin);
\r
184 meta.setPoolPreparedStatements(false);
\r
185 meta.setCacheEnabled(false);
\r
186 meta.setCacheSize(15);
\r
187 meta.setDebugging(false);
\r
189 SQLManager manager = SQLManager.getInstance();
\r
191 JDBCPool pool = null;
\r
192 if (manager != null) {
\r
193 pool = manager.createPool(meta);
\r
196 Connection connection;
\r
198 connection = pool.requestConnection();
\r
199 pool.closeConnection(connection);
\r
201 catch (Throwable t) {
\r
202 logger.fatal("Can't connect to database: " + t.toString());
\r
203 throw new ServletException("Can't connect to database: " + t.toString());
\r
207 private void setEncoding(HttpServletRequest request){
\r
209 logger.info("Request has encoding: " + request.getCharacterEncoding());
\r
210 logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
\r
211 Class reqClass = request.getClass();
\r
212 Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class});
\r
213 String encoding = configuration.getString("Mir.DefaultHTMLCharset");
\r
214 method.invoke(request, new Object[]{encoding});
\r
215 logger.info("Request now has encoding: " + request.getCharacterEncoding());
\r
217 catch (NoSuchMethodException e) {
\r
218 // TODO set the encoding in a zapata-way
\r
219 // logger.warn("set encoding not yet implemented: " + e.getMessage());
\r
221 catch (SecurityException e) {
\r
222 logger.error(e.getMessage());
\r
223 e.printStackTrace();
\r
225 catch (IllegalArgumentException e) {
\r
226 logger.error(e.getMessage());
\r
227 e.printStackTrace();
\r
229 catch (IllegalAccessException e) {
\r
230 logger.error(e.getMessage());
\r
231 e.printStackTrace();
\r
233 catch (InvocationTargetException e) {
\r
234 logger.error(e.getMessage());
\r
235 e.printStackTrace();
\r
239 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\r
240 doPost(request, response);
\r
243 protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\r
244 if ((configuration.getString("RootUri") == null) || configuration.getString("RootUri").equals("")) {
\r
245 configuration.setProperty("RootUri", request.getContextPath());
\r
247 setEncoding(request);
\r
248 process(request, response);
\r
251 abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
\r
254 * Selects the language for the response.
\r
259 protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) {
\r
260 String requestLanguage = aRequest.getParameter("language");
\r
261 String sessionLanguage = (String) aSession.getAttribute("language");
\r
262 String acceptLanguage = aRequest.getLocale().getLanguage();
\r
263 String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en");
\r
265 String language = requestLanguage;
\r
267 if (language==null)
\r
268 language = sessionLanguage;
\r
270 if (language==null)
\r
271 language = acceptLanguage;
\r
273 if (language==null)
\r
274 language = defaultLanguage;
\r
276 setLanguage(aSession, language);
\r