ServletModule exception cleanup + different error templates for admin + open postings...
[mir.git] / source / mir / servlet / AbstractServlet.java
index d47b9c3..281118d 100755 (executable)
-package mir.servlet;
-
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
-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;
-
-/**
- * Title:        Mir
- * Description:  Abstract servlet-class
- * Copyright:    Copyright (c) 2001
- * Company:      Indymedia
- * @author       idfx
- * @version 1.0
- */
-
-public abstract class AbstractServlet extends HttpServlet {
-  protected static String lang;
-  protected static Logfile theLog;
-
-  /**
-   * the configration
-   */
-  protected boolean getConfig(HttpServletRequest req) {
-
-    String RealPath = super.getServletContext().getRealPath("/");
-    String Uri = req.getRequestURI();
-    String Name = super.getServletName();
-    String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, "");
-
-
-    MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config"));
-    theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Mir.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"));
-    return true;
-  }
-
-  /**
-   * Bind the language to the session
-   */
-  protected void setLanguage(HttpSession session, String language){
-    session.setAttribute("Language",language);
-  }
-
-  /**
-   * Get the session-binded language
-   */
-  protected String getLanguage(HttpServletRequest req, HttpSession session){
-    String lang = (String)session.getAttribute("Language");
-    if(lang==null || lang==""){
-      return getAcceptLanguage(req);
-    } else {
-      return lang;
-    }
-  }
-
-  /**
-   * Checks the Accept-Language of the clients browser.
-   * 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);
-    return lang;
-  }
-}
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mir.servlet;\r
+\r
+import java.util.Locale;\r
+\r
+import javax.servlet.ServletConfig;\r
+import javax.servlet.ServletException;\r
+import javax.servlet.http.HttpServlet;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+import javax.servlet.http.HttpSession;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
+import mir.log.LoggerWrapper;\r
+import mir.storage.DatabaseAdaptor;\r
+\r
+import com.codestudio.util.JDBCPool;\r
+import com.codestudio.util.JDBCPoolMetaData;\r
+import com.codestudio.util.SQLManager;\r
+\r
+/**\r
+ * Title:        Mir\r
+ * Description:  Abstract servlet-class\r
+ * Copyright:    Copyright (c) 2001, 2002\r
+ * Company:      Mir-coders group\r
+ * @author       idfx, the Mir-coders group\r
+ * @version      $Id: AbstractServlet.java,v 1.23 2003/03/06 05:40:39 zapata Exp $\r
+ */\r
+\r
+public abstract class AbstractServlet extends HttpServlet {\r
+  protected static String lang;\r
+  protected LoggerWrapper logger;\r
+  protected MirPropertiesConfiguration configuration;\r
+\r
+  /**\r
+   * Constructor for AbstractServlet.\r
+   */\r
+  public AbstractServlet() {\r
+    super();\r
+    logger = new LoggerWrapper("Servlet");\r
+  }\r
+\r
+  protected void setNoCaching(HttpServletResponse aResponse) {\r
+    //nothing in Mir can or should be cached as it's all dynamic...\r
+    //\r
+    //this needs to be done here and not per page (via meta tags) as some\r
+    //browsers have problems w/ it per-page -mh\r
+    aResponse.setHeader("Pragma", "no-cache");\r
+    aResponse.setDateHeader("Expires", 0);\r
+    aResponse.setHeader("Cache-Control", "no-cache");\r
+  }\r
+\r
+  /**\r
+   * Bind the language to the session\r
+   */\r
+  protected void setLanguage(HttpSession session, String language) {\r
+    session.setAttribute("Language", language);\r
+  }\r
+\r
+  protected void setLocale(HttpSession session, Locale loc) {\r
+    session.setAttribute("Locale", loc);\r
+  }\r
+\r
+  /**\r
+   * Get the session-bound language\r
+   */\r
+  protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {\r
+    String lang = (String) session.getAttribute("Language");\r
+\r
+    if (lang == null || lang.length()==0) {\r
+      lang = getAcceptLanguage(aRequest);\r
+    }\r
+\r
+    return lang;\r
+  }\r
+\r
+  /**\r
+   * get the locale either from the session or the accept-language header ot the request\r
+   * this supersedes getLanguage for the new i18n\r
+   */\r
+  public Locale getLocale(HttpServletRequest aRequest) {\r
+    Locale loc = null;\r
+    HttpSession session = aRequest.getSession(false);\r
+    if (session != null) {\r
+      // session can be null in case of logout\r
+      loc = (Locale) session.getAttribute("Locale");\r
+    }\r
+    // if there is nothing in the session get it fron the accept-language\r
+    if (loc == null) {\r
+      loc = aRequest.getLocale();\r
+    }\r
+    return loc;\r
+  }\r
+\r
+  /**\r
+   * Checks the Accept-Language of the client browser.\r
+   * If this language is available it returns its country-code,\r
+   * else it returns the standard-language\r
+   */\r
+  protected String getAcceptLanguage(HttpServletRequest aRequest) {\r
+    Locale loc = aRequest.getLocale();\r
+    lang = loc.getLanguage();\r
+    return lang;\r
+  }\r
+\r
+  /**\r
+   * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)\r
+   */\r
+  public void init(ServletConfig config) throws ServletException {\r
+    super.init(config);\r
+    MirPropertiesConfiguration.setContext(config.getServletContext());\r
+    try {\r
+      configuration = MirPropertiesConfiguration.instance();\r
+    }\r
+    catch (PropertiesConfigExc e) {\r
+      throw new ServletException(e);\r
+    }\r
+\r
+    String dbUser = configuration.getString("Database.Username");\r
+    String dbPassword = configuration.getString("Database.Password");\r
+    String dbHost = configuration.getString("Database.Host");\r
+    String dbAdapName = configuration.getString("Database.Adaptor");\r
+    String dbName = configuration.getString("Database.Name");\r
+\r
+    DatabaseAdaptor adaptor;\r
+    try {\r
+      adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();\r
+    }\r
+    catch (Exception e) {\r
+      throw new ServletException("Could not load DB adapator: " +\r
+                                 e.toString());\r
+    }\r
+\r
+    String dbDriver;\r
+    String dbUrl;\r
+    try {\r
+      dbDriver = adaptor.getDriver();\r
+      dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);\r
+    }\r
+    catch (Exception e) {\r
+      throw new ServletException(e);\r
+    }\r
+\r
+    JDBCPoolMetaData meta = new JDBCPoolMetaData();\r
+    meta.setDbname(dbName);\r
+    meta.setDriver(dbDriver);\r
+    meta.setURL(dbUrl);\r
+    meta.setUserName(dbUser);\r
+    meta.setPassword(dbPassword);\r
+    meta.setJNDIName("mir");\r
+    meta.setMaximumSize(10);\r
+    meta.setMinimumSize(1);\r
+    meta.setPoolPreparedStatements(false);\r
+    meta.setCacheEnabled(false);\r
+    meta.setCacheSize(15);\r
+    meta.setDebugging(false);\r
+//    meta.setLogFile(dblogfile+".pool");\r
+\r
+    SQLManager manager = SQLManager.getInstance();\r
+    JDBCPool pool = null;\r
+    if (manager != null) {\r
+      pool = manager.createPool(meta);\r
+    }\r
+  }\r
+}\r