internationalized admin templates
[mir.git] / source / mir / servlet / AbstractServlet.java
1 package mir.servlet;
2
3 import mir.misc.Logfile;
4 import mir.misc.MirConfig;
5 import mir.misc.StringUtil;
6 import mir.storage.StorageObjectException;
7
8 import javax.servlet.UnavailableException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpSession;
12 import java.util.Locale;
13
14 /**
15  * Title:        Mir
16  * Description:  Abstract servlet-class
17  * Copyright:    Copyright (c) 2001
18  * Company:      Indymedia
19  * @author       idfx
20  * @version 1.0
21  */
22
23 public abstract class AbstractServlet extends HttpServlet {
24     protected static String lang;
25     protected static Logfile theLog;
26
27     /**
28      * the configration
29      */
30     protected boolean getConfig(HttpServletRequest req)
31             throws UnavailableException {
32
33         String RealPath = super.getServletContext().getRealPath("/");
34         String Uri = req.getRequestURI();
35         String Name = super.getServletName();
36         String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, "");
37
38         // init config
39         MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config"));
40
41         theLog = Logfile.getInstance(MirConfig.getPropWithHome(Name + ".Logfile"));
42         theLog.printInfo(Name + " started.");
43         theLog.printInfo("Path is: " + MirConfig.getProp("Home"));
44         theLog.printInfo("Root URI is: " + MirConfig.getProp("RootUri"));
45         theLog.printInfo("StandardLanguage is: " + MirConfig.getProp("StandardLanguage"));
46         try {
47             MirConfig.initDbPool();
48         }
49         catch (StorageObjectException e) {
50             throw new UnavailableException(
51                     "Could not initialize database pool. -- "
52                     + e.toString(), 0);
53         }
54         super.getServletContext().setAttribute("mir.confed", new Boolean(true));
55         return true;
56     }
57
58     /**
59      * Bind the language to the session
60      */
61     protected void setLanguage(HttpSession session, String language) {
62         session.setAttribute("Language", language);
63     }
64
65     protected void setLocale(HttpSession session, Locale loc) {
66         session.setAttribute("Locale", loc);
67     }
68
69     /**
70      * Get the session-bound language
71      */
72     protected String getLanguage(HttpServletRequest req, HttpSession session) {
73         String lang = (String) session.getAttribute("Language");
74         if (lang == null || lang.equals("")) {
75             return getAcceptLanguage(req);
76         }
77         else {
78             return lang;
79         }
80     }
81
82     /**
83      * get the locale either from the session or the accept-language header ot the request
84      * this supersedes getLanguage for the new i18n
85      */
86     public Locale getLocale(HttpServletRequest req) {
87         Locale loc=null;
88         HttpSession session = req.getSession(false);
89         if (session!=null) {
90             // session can be null in case of logout
91             loc = (Locale) session.getAttribute("Locale");
92         }
93         // if there is nothing in the session get it fron the accept-language
94         if (loc == null) {
95             loc = req.getLocale();
96         }
97         return loc;
98     }
99
100     /**
101      * Checks the Accept-Language of the client browser.
102      * If this language is available it returns its country-code,
103      * else it returns the standard-language
104      */
105     protected String getAcceptLanguage(HttpServletRequest req) {
106         Locale loc = req.getLocale();
107         lang = loc.getLanguage();
108         /* not needed anymore due to new i18n
109           File f = new File(HTMLTemplateProcessor.templateDir+"/"+lang);
110         //is there an existing template-path?
111         if(!f.isDirectory()){
112           //no there isn't. we use standard-language
113           lang = MirConfig.getProp("StandardLanguage");
114           theLog.printDebugInfo("language not existing");
115         }
116         theLog.printDebugInfo("Language: " + lang);
117         */
118         return lang;
119     }
120 }