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