merged 1.1 branch into head
[mir.git] / source / mir / servlet / AbstractServlet.java
index 5cfc53e..47e6f57 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001-2006 The Mir-coders group
  *
  * This file is part of Mir.
  *
@@ -19,8 +19,6 @@
  *
  * 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
  */
 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 mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerWrapper;
+import mircoders.global.MirGlobal;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -41,10 +37,11 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
-
-import mir.config.MirPropertiesConfiguration;
-import mir.log.LoggerWrapper;
-import mircoders.global.MirGlobal;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.util.Locale;
 
 /**
  * Title:        Mir
@@ -52,7 +49,7 @@ import mircoders.global.MirGlobal;
  * Copyright:    Copyright (c) 2001, 2002
  * Company:      Mir-coders group
  * @author       idfx, the Mir-coders group
- * @version      $Id: AbstractServlet.java,v 1.32 2004/11/06 19:33:15 idfx Exp $
+ * @version      $Id: AbstractServlet.java,v 1.33 2007/04/08 21:46:44 idfx Exp $
  */
 
 public abstract class AbstractServlet extends HttpServlet {
@@ -64,11 +61,6 @@ public abstract class AbstractServlet extends HttpServlet {
   }
 
   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");
@@ -77,24 +69,9 @@ public abstract class AbstractServlet extends HttpServlet {
   /**
    * Bind the language to the session
    */
-  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 aRequest, HttpSession session) {
-    String language = (String) session.getAttribute("language");
-
-    if (language == null || language.length()==0) {
-      language = getAcceptLanguage(aRequest);
-    }
-
-    return language;
+  protected void setLanguage(HttpSession aRequest, String aLanguage) {
+    aRequest.setAttribute("language", aLanguage);
+    aRequest.setAttribute("locale", new Locale(aLanguage, ""));
   }
 
   /**
@@ -124,18 +101,18 @@ public abstract class AbstractServlet extends HttpServlet {
    * else it returns the standard-language
    */
   protected String getAcceptLanguage(HttpServletRequest aRequest) {
-    Locale loc = aRequest.getLocale();
-    String lang = loc.getLanguage();
-    return lang;
+    Locale locale = aRequest.getLocale();
+
+    return locale.getLanguage();
   }
 
   /**
    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
    */
-  public void init(ServletConfig config) throws ServletException {
-    super.init(config);
+  public void init(ServletConfig aConfiguration) throws ServletException {
+    super.init(aConfiguration);
 
-    MirPropertiesConfiguration.setContext(config.getServletContext());
+    MirPropertiesConfiguration.setContext(aConfiguration.getServletContext());
     try {
       configuration = MirPropertiesConfiguration.instance();
     }
@@ -153,9 +130,8 @@ public abstract class AbstractServlet extends HttpServlet {
       throw new ServletException("can't get localizer: " + t.toString());
     }
 
-    Connection connection;
     try {
-      connection = MirGlobal.getDatabaseEngine().obtainConnection();
+      Connection connection = MirGlobal.getDatabaseEngine().obtainConnection();
       MirGlobal.getDatabaseEngine().releaseConnection(connection);
     }
     catch (Throwable t) {
@@ -164,50 +140,48 @@ public abstract class AbstractServlet extends HttpServlet {
     }
   }
 
-  private void setEncoding(HttpServletRequest request){
+  private void setEncoding(HttpServletRequest aRequest) {
     try {
-      logger.info("Request has encoding: " + request.getCharacterEncoding());
+      logger.info("Request has encoding: " + aRequest.getCharacterEncoding());
       logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset"));
-      Class reqClass = request.getClass();
-      Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class});
+
+      Class requestClass = aRequest.getClass();
+      Method method = requestClass.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());
+      method.invoke(aRequest, new Object[]{encoding});
+      logger.info("Request now has encoding: " + aRequest.getCharacterEncoding());
     }
     catch (NoSuchMethodException e) {
-      // TODO set the encoding in a zapata-way
     }
     catch (SecurityException e) {
-      logger.error(e.getMessage());
-      e.printStackTrace();
+      logger.error(e.getMessage(), e);
     }
     catch (IllegalArgumentException e) {
-      logger.error(e.getMessage());
-      e.printStackTrace();
+      logger.error(e.getMessage(), e);
     }
     catch (IllegalAccessException e) {
-      logger.error(e.getMessage());
-      e.printStackTrace();
+      logger.error(e.getMessage(), e);
     }
     catch (InvocationTargetException e) {
-      logger.error(e.getMessage());
-      e.printStackTrace();
+      logger.error(e.getMessage(), e);
     }
   }
 
-  protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-    doPost(request, response);
+  protected final void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
+    doPost(aRequest, aResponse);
   }
 
-  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());
+  protected final void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {
+    if ((configuration.getString("RootUri") == null) ||
+         configuration.getString("RootUri").equals("")) {
+      configuration.setProperty("RootUri", aRequest.getContextPath());
     }
-    setEncoding(request);
-    process(request, response);
+
+    setEncoding(aRequest);
+    process(aRequest, aResponse);
   }
 
-  abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
+  public abstract void process(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException;
 
   /**
    * Selects the language for the response.