support for storing the ServletContext in MirConfig for later use. Have to do this...
[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         MirConfig.initConfig(super.getServletContext(), RootUri, Name,
41                               getInitParameter("Config"));
42
43         theLog = Logfile.getInstance(MirConfig.getPropWithHome(Name + ".Logfile"));
44         theLog.printInfo(Name + " started.");
45         theLog.printInfo("Path is: " + MirConfig.getProp("Home"));
46         theLog.printInfo("Root URI is: " + MirConfig.getProp("RootUri"));
47         theLog.printInfo("StandardLanguage is: " + MirConfig.getProp("StandardLanguage"));
48         try {
49             MirConfig.initDbPool();
50         }
51         catch (StorageObjectException e) {
52             throw new UnavailableException(
53                     "Could not initialize database pool. -- "
54                     + e.toString(), 0);
55         }
56         super.getServletContext().setAttribute("mir.confed", new Boolean(true));
57         return true;
58     }
59
60     /**
61      * Bind the language to the session
62      */
63     protected void setLanguage(HttpSession session, String language) {
64         session.setAttribute("Language", language);
65     }
66
67     protected void setLocale(HttpSession session, Locale loc) {
68         session.setAttribute("Locale", loc);
69     }
70
71     /**
72      * Get the session-bound language
73      */
74     protected String getLanguage(HttpServletRequest req, HttpSession session) {
75         String lang = (String) session.getAttribute("Language");
76         if (lang == null || lang.equals("")) {
77             return getAcceptLanguage(req);
78         }
79         else {
80             return lang;
81         }
82     }
83
84     /**
85      * get the locale either from the session or the accept-language header ot the request
86      * this supersedes getLanguage for the new i18n
87      */
88     public Locale getLocale(HttpServletRequest req) {
89         Locale loc=null;
90         HttpSession session = req.getSession(false);
91         if (session!=null) {
92             // session can be null in case of logout
93             loc = (Locale) session.getAttribute("Locale");
94         }
95         // if there is nothing in the session get it fron the accept-language
96         if (loc == null) {
97             loc = req.getLocale();
98         }
99         return loc;
100     }
101
102     /**
103      * Checks the Accept-Language of the client browser.
104      * If this language is available it returns its country-code,
105      * else it returns the standard-language
106      */
107     protected String getAcceptLanguage(HttpServletRequest req) {
108         Locale loc = req.getLocale();
109         lang = loc.getLanguage();
110         /* not needed anymore due to new i18n
111           File f = new File(HTMLTemplateProcessor.templateDir+"/"+lang);
112         //is there an existing template-path?
113         if(!f.isDirectory()){
114           //no there isn't. we use standard-language
115           lang = MirConfig.getProp("StandardLanguage");
116           theLog.printDebugInfo("language not existing");
117         }
118         theLog.printDebugInfo("Language: " + lang);
119         */
120         return lang;
121     }
122 }