2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.io.IOException;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.util.Locale;
38 import javax.servlet.ServletConfig;
39 import javax.servlet.ServletException;
40 import javax.servlet.http.HttpServlet;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
46 import com.codestudio.util.*;
47 import com.codestudio.util.JDBCPoolMetaData;
48 import com.codestudio.util.SQLManager;
50 import mir.config.MirPropertiesConfiguration;
51 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
52 import mir.log.LoggerWrapper;
53 import mircoders.global.*;
57 * Description: Abstract servlet-class
58 * Copyright: Copyright (c) 2001, 2002
59 * Company: Mir-coders group
60 * @author idfx, the Mir-coders group
61 * @version $Id: AbstractServlet.java,v 1.30.2.3 2003/06/28 04:04:25 zapata Exp $
64 public abstract class AbstractServlet extends HttpServlet {
65 protected static String lang;
66 protected LoggerWrapper logger;
67 protected MirPropertiesConfiguration configuration;
70 * Constructor for AbstractServlet.
72 public AbstractServlet() {
76 protected void setNoCaching(HttpServletResponse aResponse) {
77 //nothing in Mir can or should be cached as it's all dynamic...
79 //this needs to be done here and not per page (via meta tags) as some
80 //browsers have problems w/ it per-page -mh
81 aResponse.setHeader("Pragma", "no-cache");
82 aResponse.setDateHeader("Expires", 0);
83 aResponse.setHeader("Cache-Control", "no-cache");
87 * Bind the language to the session
89 protected void setLanguage(HttpSession session, String language) {
90 logger.debug("setting language to " + language);
92 session.setAttribute("language", language);
93 session.setAttribute("locale", new Locale(language, ""));
97 * Get the session-bound language
99 protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {
100 String lang = (String) session.getAttribute("language");
102 if (lang == null || lang.length()==0) {
103 lang = getAcceptLanguage(aRequest);
110 * get the locale either from the session or the accept-language header ot the request
111 * this supersedes getLanguage for the new i18n
113 public Locale getLocale(HttpServletRequest aRequest) {
115 HttpSession session = aRequest.getSession(false);
116 if (session != null) {
117 // session can be null in case of logout
118 loc = (Locale) session.getAttribute("locale");
120 // if there is nothing in the session get it fron the accept-language
122 loc = aRequest.getLocale();
125 logger.debug("getting locale: " + loc.getLanguage());
131 * Checks the Accept-Language of the client browser.
132 * If this language is available it returns its country-code,
133 * else it returns the standard-language
135 protected String getAcceptLanguage(HttpServletRequest aRequest) {
136 Locale loc = aRequest.getLocale();
137 lang = loc.getLanguage();
142 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
144 public void init(ServletConfig config) throws ServletException {
147 MirPropertiesConfiguration.setContext(config.getServletContext());
149 configuration = MirPropertiesConfiguration.instance();
151 catch (Throwable t) {
152 throw new ServletException("can't read configuration: " + t.toString());
155 logger = new LoggerWrapper("Servlet");
158 MirGlobal.localizer();
160 catch (Throwable t) {
161 logger.fatal("can't get localizer: " + t.toString());
162 throw new ServletException("can't get localizer: " + t.toString());
165 String dbUser = configuration.getString("Database.Username");
166 String dbPassword = configuration.getString("Database.Password");
167 String dbHost = configuration.getString("Database.Host");
168 String dbPort = configuration.getString("Database.Port");
169 String dbAdapName = configuration.getString("Database.Adaptor");
170 String dbName = configuration.getString("Database.Name");
171 String dbDriver = configuration.getString("Database.Driver");
172 String dbUrl = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName;
173 int dbMin = configuration.getInteger("Database.poolMin", 1);
174 int dbMax = configuration.getInteger("Database.poolMax", 10);
176 JDBCPoolMetaData meta = new JDBCPoolMetaData();
177 meta.setDbname(dbName);
178 meta.setDriver(dbDriver);
180 meta.setUserName(dbUser);
181 meta.setPassword(dbPassword);
182 meta.setJNDIName("mir");
183 meta.setMaximumSize(dbMax);
184 meta.setMinimumSize(dbMin);
185 meta.setPoolPreparedStatements(false);
186 meta.setCacheEnabled(false);
187 meta.setCacheSize(15);
188 meta.setDebugging(false);
190 SQLManager manager = SQLManager.getInstance();
192 JDBCPool pool = null;
193 if (manager != null) {
194 pool = manager.createPool(meta);
197 Connection connection;
199 connection = pool.requestConnection();
200 pool.closeConnection(connection);
202 catch (Throwable t) {
203 logger.fatal("Can't connect to database: " + t.toString());
204 throw new ServletException("Can't connect to database: " + t.toString());
208 private void setEncoding(HttpServletRequest request){
210 Class reqClass = request.getClass();
211 Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class});
212 String encoding = configuration.getString("Mir.DefaultHTMLCharset");
213 method.invoke(request, new Object[]{encoding});
214 } catch (NoSuchMethodException e) {
215 // TODO set the encoding in a zapata-way
216 // logger.warn("set encoding not yet implemented: " + e.getMessage());
217 } catch (SecurityException e) {
218 logger.error(e.getMessage());
220 } catch (IllegalArgumentException e) {
221 logger.error(e.getMessage());
223 } catch (IllegalAccessException e) {
224 logger.error(e.getMessage());
226 } catch (InvocationTargetException e) {
227 logger.error(e.getMessage());
232 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
233 doPost(request, response);
236 protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
237 if ((configuration.getString("RootUri") == null) || configuration.getString("RootUri").equals("")) {
238 configuration.setProperty("RootUri", request.getContextPath());
240 setEncoding(request);
241 process(request, response);
244 abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
247 * Selects the language for the response.
252 protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) {
253 String requestLanguage = aRequest.getParameter("language");
254 String sessionLanguage = (String) aSession.getAttribute("language");
255 String acceptLanguage = aRequest.getLocale().getLanguage();
256 String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en");
258 String language = requestLanguage;
261 language = sessionLanguage;
264 language = acceptLanguage;
267 language = defaultLanguage;
269 setLanguage(aSession, language);