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