2 * Copyright (C) 2001-2006 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 * and distribute linked combinations including the two. You must obey the
23 * GNU General Public License in all respects for all of the code used other than
24 * the above mentioned libraries. If you modify this file, you may extend this
25 * exception to your version of the file, but you are not obligated to do so.
26 * If you do not wish to do so, delete this exception statement from your version.
30 import mir.config.MirPropertiesConfiguration;
31 import mir.log.LoggerWrapper;
32 import mircoders.global.MirGlobal;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServlet;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.servlet.http.HttpSession;
40 import java.io.IOException;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.sql.Connection;
44 import java.util.Locale;
48 * Description: Abstract servlet-class
49 * Copyright: Copyright (c) 2001, 2002
50 * Company: Mir-coders group
51 * @author idfx, the Mir-coders group
52 * @version $Id: AbstractServlet.java,v 1.30.2.11 2006/08/10 19:29:35 zapata Exp $
55 public abstract class AbstractServlet extends HttpServlet {
56 protected LoggerWrapper logger;
57 protected MirPropertiesConfiguration configuration;
59 public AbstractServlet() {
63 protected void setNoCaching(HttpServletResponse aResponse) {
64 aResponse.setHeader("Pragma", "no-cache");
65 aResponse.setDateHeader("Expires", 0);
66 aResponse.setHeader("Cache-Control", "no-cache");
70 * Bind the language to the session
72 protected void setLanguage(HttpSession aRequest, String aLanguage) {
73 aRequest.setAttribute("language", aLanguage);
74 aRequest.setAttribute("locale", new Locale(aLanguage, ""));
78 * get the locale either from the session or the accept-language header ot the request
79 * this supersedes getLanguage for the new i18n
81 public Locale getLocale(HttpServletRequest aRequest) {
83 HttpSession session = aRequest.getSession(false);
84 if (session != null) {
85 // session can be null in case of logout
86 locale = (Locale) session.getAttribute("locale");
88 // if there is nothing in the session get it fron the accept-language
90 locale = aRequest.getLocale();
93 logger.debug("getting locale: " + locale.getLanguage());
99 * Checks the Accept-Language of the client browser.
100 * If this language is available it returns its country-code,
101 * else it returns the standard-language
103 protected String getAcceptLanguage(HttpServletRequest aRequest) {
104 Locale locale = aRequest.getLocale();
106 return locale.getLanguage();
110 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
112 public void init(ServletConfig aConfiguration) throws ServletException {
113 super.init(aConfiguration);
115 MirPropertiesConfiguration.setContext(aConfiguration.getServletContext());
117 configuration = MirPropertiesConfiguration.instance();
119 catch (Throwable t) {
120 throw new ServletException("can't read configuration: " + t.toString());
123 logger = new LoggerWrapper("Servlet");
126 MirGlobal.localizer();
128 catch (Throwable t) {
129 logger.fatal("can't get localizer: " + t.toString());
130 throw new ServletException("can't get localizer: " + t.toString());
134 Connection connection = MirGlobal.getDatabaseEngine().obtainConnection();
135 MirGlobal.getDatabaseEngine().releaseConnection(connection);
137 catch (Throwable t) {
138 logger.fatal("Can't connect to database: " + t.toString());
139 throw new ServletException("Can't connect to database: " + t.toString());
143 private void setEncoding(HttpServletRequest aRequest) {
145 logger.info("Request has encoding: " + aRequest.getCharacterEncoding());
146 logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
148 Class requestClass = aRequest.getClass();
149 Method method = requestClass.getMethod("setCharacterEncoding", new Class[]{String.class});
150 String encoding = configuration.getString("Mir.DefaultHTMLCharset");
151 method.invoke(aRequest, new Object[]{encoding});
152 logger.info("Request now has encoding: " + aRequest.getCharacterEncoding());
154 catch (NoSuchMethodException e) {
156 catch (SecurityException e) {
157 logger.error(e.getMessage(), e);
159 catch (IllegalArgumentException e) {
160 logger.error(e.getMessage(), e);
162 catch (IllegalAccessException e) {
163 logger.error(e.getMessage(), e);
165 catch (InvocationTargetException e) {
166 logger.error(e.getMessage(), e);
170 protected final void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
171 doPost(aRequest, aResponse);
174 protected final void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
175 if ((configuration.getString("RootUri") == null) ||
176 configuration.getString("RootUri").equals("")) {
177 configuration.setProperty("RootUri", aRequest.getContextPath());
180 setEncoding(aRequest);
181 process(aRequest, aResponse);
184 public abstract void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException;
187 * Selects the language for the response.
189 protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) {
190 String requestLanguage = aRequest.getParameter("language");
191 String sessionLanguage = (String) aSession.getAttribute("language");
192 String acceptLanguage = aRequest.getLocale().getLanguage();
193 String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en");
195 String language = requestLanguage;
198 language = sessionLanguage;
201 language = acceptLanguage;
204 language = defaultLanguage;
206 setLanguage(aSession, language);