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.sql.Connection;
36 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;
45 import mir.config.MirPropertiesConfiguration;
46 import mir.log.LoggerWrapper;
47 import mircoders.global.MirGlobal;
51 * Description: Abstract servlet-class
52 * Copyright: Copyright (c) 2001, 2002
53 * Company: Mir-coders group
54 * @author idfx, the Mir-coders group
55 * @version $Id: AbstractServlet.java,v 1.30.2.10 2005/10/01 18:40:40 zapata Exp $
58 public abstract class AbstractServlet extends HttpServlet {
59 protected LoggerWrapper logger;
60 protected MirPropertiesConfiguration configuration;
62 public AbstractServlet() {
66 protected void setNoCaching(HttpServletResponse aResponse) {
67 aResponse.setHeader("Pragma", "no-cache");
68 aResponse.setDateHeader("Expires", 0);
69 aResponse.setHeader("Cache-Control", "no-cache");
73 * Bind the language to the session
75 protected void setLanguage(HttpSession aRequest, String aLanguage) {
76 aRequest.setAttribute("language", aLanguage);
77 aRequest.setAttribute("locale", new Locale(aLanguage, ""));
81 * get the locale either from the session or the accept-language header ot the request
82 * this supersedes getLanguage for the new i18n
84 public Locale getLocale(HttpServletRequest aRequest) {
86 HttpSession session = aRequest.getSession(false);
87 if (session != null) {
88 // session can be null in case of logout
89 locale = (Locale) session.getAttribute("locale");
91 // if there is nothing in the session get it fron the accept-language
93 locale = aRequest.getLocale();
96 logger.debug("getting locale: " + locale.getLanguage());
102 * Checks the Accept-Language of the client browser.
103 * If this language is available it returns its country-code,
104 * else it returns the standard-language
106 protected String getAcceptLanguage(HttpServletRequest aRequest) {
107 Locale locale = aRequest.getLocale();
109 return locale.getLanguage();
113 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
115 public void init(ServletConfig aConfiguration) throws ServletException {
116 super.init(aConfiguration);
118 MirPropertiesConfiguration.setContext(aConfiguration.getServletContext());
120 configuration = MirPropertiesConfiguration.instance();
122 catch (Throwable t) {
123 throw new ServletException("can't read configuration: " + t.toString());
126 logger = new LoggerWrapper("Servlet");
129 MirGlobal.localizer();
131 catch (Throwable t) {
132 logger.fatal("can't get localizer: " + t.toString());
133 throw new ServletException("can't get localizer: " + t.toString());
137 Connection connection = MirGlobal.getDatabaseEngine().obtainConnection();
138 MirGlobal.getDatabaseEngine().releaseConnection(connection);
140 catch (Throwable t) {
141 logger.fatal("Can't connect to database: " + t.toString());
142 throw new ServletException("Can't connect to database: " + t.toString());
146 private void setEncoding(HttpServletRequest aRequest) {
148 logger.info("Request has encoding: " + aRequest.getCharacterEncoding());
149 logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
151 Class requestClass = aRequest.getClass();
152 Method method = requestClass.getMethod("setCharacterEncoding", new Class[]{String.class});
153 String encoding = configuration.getString("Mir.DefaultHTMLCharset");
154 method.invoke(aRequest, new Object[]{encoding});
155 logger.info("Request now has encoding: " + aRequest.getCharacterEncoding());
157 catch (NoSuchMethodException e) {
159 catch (SecurityException e) {
160 logger.error(e.getMessage(), e);
162 catch (IllegalArgumentException e) {
163 logger.error(e.getMessage(), e);
165 catch (IllegalAccessException e) {
166 logger.error(e.getMessage(), e);
168 catch (InvocationTargetException e) {
169 logger.error(e.getMessage(), e);
173 protected final void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
174 doPost(aRequest, aResponse);
177 protected final void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
178 if ((configuration.getString("RootUri") == null) ||
179 configuration.getString("RootUri").equals("")) {
180 configuration.setProperty("RootUri", aRequest.getContextPath());
183 setEncoding(aRequest);
184 process(aRequest, aResponse);
187 public abstract void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException;
190 * Selects the language for the response.
192 protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) {
193 String requestLanguage = aRequest.getParameter("language");
194 String sessionLanguage = (String) aSession.getAttribute("language");
195 String acceptLanguage = aRequest.getLocale().getLanguage();
196 String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en");
198 String language = requestLanguage;
201 language = sessionLanguage;
204 language = acceptLanguage;
207 language = defaultLanguage;
209 setLanguage(aSession, language);