3 import mir.misc.Logfile;
4 import mir.misc.MirConfig;
5 import mir.misc.StringUtil;
6 import mir.storage.StorageObjectException;
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;
17 * Description: Abstract servlet-class
18 * Copyright: Copyright (c) 2001
24 public abstract class AbstractServlet extends HttpServlet {
25 protected static String lang;
26 protected static Logfile theLog;
31 protected boolean getConfig(HttpServletRequest req)
32 throws UnavailableException {
34 //String RealPath = super.getServletContext().getRealPath("/");
35 String Uri = req.getRequestURI();
36 String Name = super.getServletName();
37 String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, "");
40 //MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config"));
41 MirConfig.initConfig(super.getServletContext(), RootUri, Name,
42 getInitParameter("Config"));
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"));
50 MirConfig.initDbPool();
52 catch (StorageObjectException e) {
53 throw new UnavailableException(
54 "Could not initialize database pool. -- "
57 super.getServletContext().setAttribute("mir.confed", new Boolean(true));
62 * Bind the language to the session
64 protected void setLanguage(HttpSession session, String language) {
65 session.setAttribute("Language", language);
68 protected void setLocale(HttpSession session, Locale loc) {
69 session.setAttribute("Locale", loc);
73 * Get the session-bound language
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);
86 * get the locale either from the session or the accept-language header ot the request
87 * this supersedes getLanguage for the new i18n
89 public Locale getLocale(HttpServletRequest req) {
91 HttpSession session = req.getSession(false);
93 // session can be null in case of logout
94 loc = (Locale) session.getAttribute("Locale");
96 // if there is nothing in the session get it fron the accept-language
98 loc = req.getLocale();
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
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");
119 theLog.printDebugInfo("Language: " + lang);