2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mir.servlet;
\r
34 import java.io.IOException;
\r
35 import java.util.Locale;
\r
36 import javax.servlet.ServletConfig;
\r
37 import javax.servlet.ServletException;
\r
38 import javax.servlet.http.HttpServlet;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
41 import javax.servlet.http.HttpSession;
\r
43 import com.codestudio.util.JDBCPool;
\r
44 import com.codestudio.util.JDBCPoolMetaData;
\r
45 import com.codestudio.util.SQLManager;
\r
46 import mir.config.MirPropertiesConfiguration;
\r
47 import mir.config.MirPropertiesConfiguration$PropertiesConfigExc;
\r
48 import mir.log.LoggerWrapper;
\r
49 import mir.storage.DatabaseAdaptor;
\r
53 * Description: Abstract servlet-class
\r
54 * Copyright: Copyright (c) 2001, 2002
\r
55 * Company: Mir-coders group
\r
56 * @author idfx, the Mir-coders group
\r
57 * @version $Id: AbstractServlet.java,v 1.25 2003/03/17 20:47:03 zapata Exp $
\r
60 public abstract class AbstractServlet extends HttpServlet {
\r
61 protected static String lang;
\r
62 protected LoggerWrapper logger;
\r
63 protected MirPropertiesConfiguration configuration;
\r
66 * Constructor for AbstractServlet.
\r
68 public AbstractServlet() {
\r
70 logger = new LoggerWrapper("Servlet");
\r
73 protected void setNoCaching(HttpServletResponse aResponse) {
\r
74 //nothing in Mir can or should be cached as it's all dynamic...
\r
76 //this needs to be done here and not per page (via meta tags) as some
\r
77 //browsers have problems w/ it per-page -mh
\r
78 aResponse.setHeader("Pragma", "no-cache");
\r
79 aResponse.setDateHeader("Expires", 0);
\r
80 aResponse.setHeader("Cache-Control", "no-cache");
\r
84 * Bind the language to the session
\r
86 protected void setLanguage(HttpSession session, String language) {
\r
87 logger.debug("setting language to " + language);
\r
89 session.setAttribute("language", language);
\r
90 session.setAttribute("locale", new Locale(language, ""));
\r
94 * Get the session-bound language
\r
96 protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {
\r
97 String lang = (String) session.getAttribute("language");
\r
99 if (lang == null || lang.length()==0) {
\r
100 lang = getAcceptLanguage(aRequest);
\r
107 * get the locale either from the session or the accept-language header ot the request
\r
108 * this supersedes getLanguage for the new i18n
\r
110 public Locale getLocale(HttpServletRequest aRequest) {
\r
112 HttpSession session = aRequest.getSession(false);
\r
113 if (session != null) {
\r
114 // session can be null in case of logout
\r
115 loc = (Locale) session.getAttribute("locale");
\r
117 // if there is nothing in the session get it fron the accept-language
\r
119 loc = aRequest.getLocale();
\r
122 logger.debug("getting locale: " + loc.getLanguage());
\r
128 * Checks the Accept-Language of the client browser.
\r
129 * If this language is available it returns its country-code,
\r
130 * else it returns the standard-language
\r
132 protected String getAcceptLanguage(HttpServletRequest aRequest) {
\r
133 Locale loc = aRequest.getLocale();
\r
134 lang = loc.getLanguage();
\r
139 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
\r
141 public void init(ServletConfig config) throws ServletException {
\r
142 super.init(config);
\r
144 MirPropertiesConfiguration.setContext(config.getServletContext());
\r
146 configuration = MirPropertiesConfiguration.instance();
\r
148 catch (PropertiesConfigExc e) {
\r
149 throw new ServletException(e);
\r
152 String dbUser = configuration.getString("Database.Username");
\r
153 String dbPassword = configuration.getString("Database.Password");
\r
154 String dbHost = configuration.getString("Database.Host");
\r
155 String dbAdapName = configuration.getString("Database.Adaptor");
\r
156 String dbName = configuration.getString("Database.Name");
\r
158 DatabaseAdaptor adaptor;
\r
160 adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();
\r
162 catch (Exception e) {
\r
163 throw new ServletException("Could not load DB adapator: " +
\r
170 dbDriver = adaptor.getDriver();
\r
171 dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);
\r
173 catch (Exception e) {
\r
174 throw new ServletException(e);
\r
177 JDBCPoolMetaData meta = new JDBCPoolMetaData();
\r
178 meta.setDbname(dbName);
\r
179 meta.setDriver(dbDriver);
\r
180 meta.setURL(dbUrl);
\r
181 meta.setUserName(dbUser);
\r
182 meta.setPassword(dbPassword);
\r
183 meta.setJNDIName("mir");
\r
184 meta.setMaximumSize(10);
\r
185 meta.setMinimumSize(1);
\r
186 meta.setPoolPreparedStatements(false);
\r
187 meta.setCacheEnabled(false);
\r
188 meta.setCacheSize(15);
\r
189 meta.setDebugging(false);
\r
190 // meta.setLogFile(dblogfile+".pool");
\r
192 SQLManager manager = SQLManager.getInstance();
\r
193 JDBCPool pool = null;
\r
194 if (manager != null) {
\r
195 pool = manager.createPool(meta);
\r
200 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\r
201 doPost(request, response);
\r
204 protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\r
205 if ( (configuration.getString("RootUri") == null) ||
\r
206 configuration.getString("RootUri").equals("")) {
\r
207 configuration.setProperty("RootUri", request.getContextPath());
\r
209 process(request, response);
\r
212 abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
\r