internationalized admin templates
[mir.git] / source / mir / servlet / AbstractServlet.java
index 9b3c400..0bbeb45 100755 (executable)
@@ -1,18 +1,15 @@
 package mir.servlet;
 
+import mir.misc.Logfile;
+import mir.misc.MirConfig;
+import mir.misc.StringUtil;
+import mir.storage.StorageObjectException;
+
+import javax.servlet.UnavailableException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 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;
 
 /**
  * Title:        Mir
@@ -24,74 +21,100 @@ import mir.storage.StorageObjectException;
  */
 
 public abstract class AbstractServlet extends HttpServlet {
-  protected static String lang;
-  protected static Logfile theLog;
+    protected static String lang;
+    protected static Logfile theLog;
+
+    /**
+     * the configration
+     */
+    protected boolean getConfig(HttpServletRequest req)
+            throws UnavailableException {
 
-  /**
-   * 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, "");
 
-    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"));
 
-    // 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("StandardLanguage 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;
+    }
+
+    /**
+     * Bind the language to the session
+     */
+    protected void setLanguage(HttpSession session, String language) {
+        session.setAttribute("Language", language);
+    }
 
-    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);
+    protected void setLocale(HttpSession session, Locale loc) {
+        session.setAttribute("Locale", loc);
     }
-    super.getServletContext().setAttribute("mir.confed", new Boolean(true));
-    return true;
-  }
 
-  /**
-   * Bind the language to the session
-   */
-  protected void setLanguage(HttpSession session, String language){
-    session.setAttribute("Language",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;
+        }
+    }
 
-  /**
-   * 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;
+    /**
+     * 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 req) {
+        Locale loc=null;
+        HttpSession session = req.getSession(false);
+        if (session!=null) {
+            // session can be null in case of logout
+            loc = (Locale) session.getAttribute("Locale");
+        }
+        // if there is nothing in the session get it fron the accept-language
+        if (loc == null) {
+            loc = req.getLocale();
+        }
+        return loc;
     }
-  }
 
-  /**
-   * Checks the Accept-Language of the client 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");
+    /**
+     * Checks the Accept-Language of the client 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();
+        /* not needed anymore due to new i18n
+          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;
     }
-    theLog.printDebugInfo("Language: " + lang);
-    return lang;
-  }
 }