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;
16 * Description: Abstract servlet-class
17 * Copyright: Copyright (c) 2001
23 public abstract class AbstractServlet extends HttpServlet {
24 protected static String lang;
25 protected static Logfile theLog;
30 protected boolean getConfig(HttpServletRequest req)
31 throws UnavailableException {
33 //String RealPath = super.getServletContext().getRealPath("/");
34 String Uri = req.getRequestURI();
35 String Name = super.getServletName();
36 String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, "");
39 //MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config"));
40 MirConfig.initConfig(super.getServletContext(), RootUri, Name,
41 getInitParameter("Config"));
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"));
49 MirConfig.initDbPool();
51 catch (StorageObjectException e) {
52 throw new UnavailableException(
53 "Could not initialize database pool. -- "
56 super.getServletContext().setAttribute("mir.confed", new Boolean(true));
61 * Bind the language to the session
63 protected void setLanguage(HttpSession session, String language) {
64 session.setAttribute("Language", language);
67 protected void setLocale(HttpSession session, Locale loc) {
68 session.setAttribute("Locale", loc);
72 * Get the session-bound language
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);
85 * get the locale either from the session or the accept-language header ot the request
86 * this supersedes getLanguage for the new i18n
88 public Locale getLocale(HttpServletRequest req) {
90 HttpSession session = req.getSession(false);
92 // session can be null in case of logout
93 loc = (Locale) session.getAttribute("Locale");
95 // if there is nothing in the session get it fron the accept-language
97 loc = req.getLocale();
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
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");
118 theLog.printDebugInfo("Language: " + lang);