X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fservlet%2FAbstractServlet.java;h=55648f350c37a890e7c2f65b73e67008728915e3;hb=63e0ee1fb8038eb6d8f0190cf38c3b3ab2727216;hp=9b3c400a0b88cd84a5724e8a6b50503258f63f6d;hpb=c6a2204d8d75293256fd17c07e54971d7672359a;p=mir.git diff --git a/source/mir/servlet/AbstractServlet.java b/source/mir/servlet/AbstractServlet.java index 9b3c400a..55648f35 100755 --- a/source/mir/servlet/AbstractServlet.java +++ b/source/mir/servlet/AbstractServlet.java @@ -1,79 +1,121 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ package mir.servlet; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.sql.Connection; +import java.util.Locale; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import javax.servlet.UnavailableException; -import java.io.File; -import java.util.Locale; -import java.util.HashMap; -import mir.misc.HTMLTemplateProcessor; -import mir.misc.StringUtil; -import mir.misc.MirConfig; -import mir.misc.Logfile; -import mir.storage.StorageObjectException; +import mir.config.MirPropertiesConfiguration; +import mir.log.LoggerWrapper; +import mircoders.global.MirGlobal; /** * Title: Mir * Description: Abstract servlet-class - * Copyright: Copyright (c) 2001 - * Company: Indymedia - * @author idfx - * @version 1.0 + * Copyright: Copyright (c) 2001, 2002 + * Company: Mir-coders group + * @author idfx, the Mir-coders group + * @version $Id: AbstractServlet.java,v 1.30.2.7 2004/11/21 22:07:13 zapata Exp $ */ public abstract class AbstractServlet extends HttpServlet { - protected static String lang; - protected static Logfile theLog; + protected LoggerWrapper logger; + protected MirPropertiesConfiguration configuration; - /** - * the configration - */ - protected boolean getConfig(HttpServletRequest req) - throws UnavailableException { - - String RealPath = super.getServletContext().getRealPath("/"); - String Uri = req.getRequestURI(); - String Name = super.getServletName(); - String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, ""); - - // init config - MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config")); - - theLog = Logfile.getInstance(MirConfig.getPropWithHome(Name+".Logfile")); - theLog.printInfo(Name + " started."); - theLog.printInfo("Path is: " + MirConfig.getProp("Home")); - theLog.printInfo("Root URI is: " + MirConfig.getProp("RootUri")); - theLog.printInfo("Lang is: " + MirConfig.getProp("StandardLanguage")); - try { - MirConfig.initDbPool(); - } catch ( StorageObjectException e) { - throw new UnavailableException( - "Could not initialize database pool. -- " - + e.toString(), 0); - } - super.getServletContext().setAttribute("mir.confed", new Boolean(true)); - return true; + public AbstractServlet() { + super(); + } + + protected void setNoCaching(HttpServletResponse aResponse) { + //nothing in Mir can or should be cached as it's all dynamic... + // + //this needs to be done here and not per page (via meta tags) as some + //browsers have problems w/ it per-page -mh + + aResponse.setHeader("Pragma", "no-cache"); + aResponse.setDateHeader("Expires", 0); + aResponse.setHeader("Cache-Control", "no-cache"); } /** * Bind the language to the session */ - protected void setLanguage(HttpSession session, String language){ - session.setAttribute("Language",language); + protected void setLanguage(HttpSession session, String language) { + logger.debug("setting language to " + language); + + session.setAttribute("language", language); + session.setAttribute("locale", new Locale(language, "")); } /** * Get the session-bound language */ - protected String getLanguage(HttpServletRequest req, HttpSession session){ - String lang = (String)session.getAttribute("Language"); - if(lang==null || lang.equals("")){ - return getAcceptLanguage(req); - } else { - return lang; + protected String getLanguage(HttpServletRequest aRequest, HttpSession session) { + String language = (String) session.getAttribute("language"); + + if (language == null || language.length()==0) { + language = getAcceptLanguage(aRequest); } + + return language; + } + + /** + * get the locale either from the session or the accept-language header ot the request + * this supersedes getLanguage for the new i18n + */ + public Locale getLocale(HttpServletRequest aRequest) { + Locale locale = null; + HttpSession session = aRequest.getSession(false); + if (session != null) { + // session can be null in case of logout + locale = (Locale) session.getAttribute("locale"); + } + // if there is nothing in the session get it fron the accept-language + if (locale == null) { + locale = aRequest.getLocale(); + } + + logger.debug("getting locale: " + locale.getLanguage()); + + return locale; } /** @@ -81,17 +123,112 @@ public abstract class AbstractServlet extends HttpServlet { * If this language is available it returns its country-code, * else it returns the standard-language */ - protected String getAcceptLanguage(HttpServletRequest req){ - Locale loc = req.getLocale(); - lang = loc.getLanguage(); - File f = new File(HTMLTemplateProcessor.templateDir+"/"+lang); - //is there an existing template-path? - if(!f.isDirectory()){ - //no there isn't. we use standard-language - lang = MirConfig.getProp("StandardLanguage"); - theLog.printDebugInfo("language not existing"); - } - theLog.printDebugInfo("Language: " + lang); + protected String getAcceptLanguage(HttpServletRequest aRequest) { + Locale loc = aRequest.getLocale(); + String lang = loc.getLanguage(); return lang; } + + /** + * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) + */ + public void init(ServletConfig config) throws ServletException { + super.init(config); + + MirPropertiesConfiguration.setContext(config.getServletContext()); + try { + configuration = MirPropertiesConfiguration.instance(); + } + catch (Throwable t) { + throw new ServletException("can't read configuration: " + t.toString()); + } + + logger = new LoggerWrapper("Servlet"); + + try { + MirGlobal.localizer(); + } + catch (Throwable t) { + logger.fatal("can't get localizer: " + t.toString()); + throw new ServletException("can't get localizer: " + t.toString()); + } + + Connection connection; + try { + connection = MirGlobal.getDatabaseEngine().obtainConnection(); + MirGlobal.getDatabaseEngine().releaseConnection(connection); + } + catch (Throwable t) { + logger.fatal("Can't connect to database: " + t.toString()); + throw new ServletException("Can't connect to database: " + t.toString()); + } + } + + private void setEncoding(HttpServletRequest request){ + try { + logger.info("Request has encoding: " + request.getCharacterEncoding()); + logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset")); + Class reqClass = request.getClass(); + Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class}); + String encoding = configuration.getString("Mir.DefaultHTMLCharset"); + method.invoke(request, new Object[]{encoding}); + logger.info("Request now has encoding: " + request.getCharacterEncoding()); + } + catch (NoSuchMethodException e) { + // TODO set the encoding in a zapata-way + } + catch (SecurityException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (IllegalArgumentException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (IllegalAccessException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (InvocationTargetException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + } + + protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request, response); + } + + protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if ((configuration.getString("RootUri") == null) || configuration.getString("RootUri").equals("")) { + configuration.setProperty("RootUri", request.getContextPath()); + } + setEncoding(request); + process(request, response); + } + + abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; + + /** + * Selects the language for the response. + */ + protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) { + String requestLanguage = aRequest.getParameter("language"); + String sessionLanguage = (String) aSession.getAttribute("language"); + String acceptLanguage = aRequest.getLocale().getLanguage(); + String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en"); + + String language = requestLanguage; + + if (language==null) + language = sessionLanguage; + + if (language==null) + language = acceptLanguage; + + if (language==null) + language = defaultLanguage; + + setLanguage(aSession, language); + } }