ac3fdcb8cd181d1f43fac0de6e5fd82e74537e42
[mir.git] / source / mir / servlet / AbstractServlet.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30 package mir.servlet;
31
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;
37
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;
44
45 import mir.config.MirPropertiesConfiguration;
46 import mir.log.LoggerWrapper;
47 import mircoders.global.MirGlobal;
48
49 /**
50  * Title:        Mir
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 $
56  */
57
58 public abstract class AbstractServlet extends HttpServlet {
59   protected LoggerWrapper logger;
60   protected MirPropertiesConfiguration configuration;
61
62   public AbstractServlet() {
63     super();
64   }
65
66   protected void setNoCaching(HttpServletResponse aResponse) {
67     aResponse.setHeader("Pragma", "no-cache");
68     aResponse.setDateHeader("Expires", 0);
69     aResponse.setHeader("Cache-Control", "no-cache");
70   }
71
72   /**
73    * Bind the language to the session
74    */
75   protected void setLanguage(HttpSession aRequest, String aLanguage) {
76     aRequest.setAttribute("language", aLanguage);
77     aRequest.setAttribute("locale", new Locale(aLanguage, ""));
78   }
79
80   /**
81    * get the locale either from the session or the accept-language header ot the request
82    * this supersedes getLanguage for the new i18n
83    */
84   public Locale getLocale(HttpServletRequest aRequest) {
85     Locale locale = null;
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");
90     }
91     // if there is nothing in the session get it fron the accept-language
92     if (locale == null) {
93       locale = aRequest.getLocale();
94     }
95
96     logger.debug("getting locale: " + locale.getLanguage());
97
98     return locale;
99   }
100
101   /**
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
105    */
106   protected String getAcceptLanguage(HttpServletRequest aRequest) {
107     Locale locale = aRequest.getLocale();
108
109     return locale.getLanguage();
110   }
111
112   /**
113    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
114    */
115   public void init(ServletConfig aConfiguration) throws ServletException {
116     super.init(aConfiguration);
117
118     MirPropertiesConfiguration.setContext(aConfiguration.getServletContext());
119     try {
120       configuration = MirPropertiesConfiguration.instance();
121     }
122     catch (Throwable t) {
123       throw new ServletException("can't read configuration: " + t.toString());
124     }
125
126     logger = new LoggerWrapper("Servlet");
127
128     try {
129       MirGlobal.localizer();
130     }
131     catch (Throwable t) {
132       logger.fatal("can't get localizer: " + t.toString());
133       throw new ServletException("can't get localizer: " + t.toString());
134     }
135
136     try {
137       Connection connection = MirGlobal.getDatabaseEngine().obtainConnection();
138       MirGlobal.getDatabaseEngine().releaseConnection(connection);
139     }
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());
143     }
144   }
145
146   private void setEncoding(HttpServletRequest aRequest) {
147     try {
148       logger.info("Request has encoding: " + aRequest.getCharacterEncoding());
149       logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
150
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());
156     }
157     catch (NoSuchMethodException e) {
158     }
159     catch (SecurityException e) {
160       logger.error(e.getMessage(), e);
161     }
162     catch (IllegalArgumentException e) {
163       logger.error(e.getMessage(), e);
164     }
165     catch (IllegalAccessException e) {
166       logger.error(e.getMessage(), e);
167     }
168     catch (InvocationTargetException e) {
169       logger.error(e.getMessage(), e);
170     }
171   }
172
173   protected final void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
174     doPost(aRequest, aResponse);
175   }
176
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());
181     }
182
183     setEncoding(aRequest);
184     process(aRequest, aResponse);
185   }
186
187   public abstract void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException;
188
189   /**
190    * Selects the language for the response.
191    */
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");
197
198     String language = requestLanguage;
199
200     if (language==null)
201       language = sessionLanguage;
202
203     if (language==null)
204       language = acceptLanguage;
205
206     if (language==null)
207       language = defaultLanguage;
208
209     setLanguage(aSession, language);
210   }
211 }