1a90e763331b69d058fec1da8cf6d1e74bb795f1
[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.8 2005/03/26 11:26:24 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 session, String language) {
76     session.setAttribute("language", language);
77     session.setAttribute("locale", new Locale(language, ""));
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     String language = locale.getLanguage();
109
110     return language;
111   }
112
113   /**
114    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
115    */
116   public void init(ServletConfig aConfiguration) throws ServletException {
117     super.init(aConfiguration);
118
119     MirPropertiesConfiguration.setContext(aConfiguration.getServletContext());
120     try {
121       configuration = MirPropertiesConfiguration.instance();
122     }
123     catch (Throwable t) {
124       throw new ServletException("can't read configuration: " + t.toString());
125     }
126
127     logger = new LoggerWrapper("Servlet");
128
129     try {
130       MirGlobal.localizer();
131     }
132     catch (Throwable t) {
133       logger.fatal("can't get localizer: " + t.toString());
134       throw new ServletException("can't get localizer: " + t.toString());
135     }
136
137     Connection connection;
138     try {
139       connection = MirGlobal.getDatabaseEngine().obtainConnection();
140       MirGlobal.getDatabaseEngine().releaseConnection(connection);
141     }
142     catch (Throwable t) {
143       logger.fatal("Can't connect to database: " + t.toString());
144       throw new ServletException("Can't connect to database: " + t.toString());
145     }
146   }
147
148   private void setEncoding(HttpServletRequest aRequest) {
149     try {
150       logger.info("Request has encoding: " + aRequest.getCharacterEncoding());
151       logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
152
153       Class requestClass = aRequest.getClass();
154       Method method = requestClass.getMethod("setCharacterEncoding", new Class[]{String.class});
155       String encoding = configuration.getString("Mir.DefaultHTMLCharset");
156       method.invoke(aRequest, new Object[]{encoding});
157       logger.info("Request now has encoding: " + aRequest.getCharacterEncoding());
158     }
159     catch (NoSuchMethodException e) {
160       // TODO do something to support old servlet containers
161     }
162     catch (SecurityException e) {
163       logger.error(e.getMessage());
164       e.printStackTrace();
165     }
166     catch (IllegalArgumentException e) {
167       logger.error(e.getMessage());
168       e.printStackTrace();
169     }
170     catch (IllegalAccessException e) {
171       logger.error(e.getMessage());
172       e.printStackTrace();
173     }
174     catch (InvocationTargetException e) {
175       logger.error(e.getMessage());
176       e.printStackTrace();
177     }
178   }
179
180   protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
181     doPost(request, response);
182   }
183
184   protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
185     if ((configuration.getString("RootUri") == null) || configuration.getString("RootUri").equals("")) {
186       configuration.setProperty("RootUri", request.getContextPath());
187     }
188
189     setEncoding(request);
190     process(request, response);
191   }
192
193   abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
194
195   /**
196    * Selects the language for the response.
197    */
198   protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) {
199     String requestLanguage = aRequest.getParameter("language");
200     String sessionLanguage = (String) aSession.getAttribute("language");
201     String acceptLanguage = aRequest.getLocale().getLanguage();
202     String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en");
203
204     String language = requestLanguage;
205
206     if (language==null)
207       language = sessionLanguage;
208
209     if (language==null)
210       language = acceptLanguage;
211
212     if (language==null)
213       language = defaultLanguage;
214
215     setLanguage(aSession, language);
216   }
217 }