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"));
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"));
47 MirConfig.initDbPool();
49 catch (StorageObjectException e) {
50 throw new UnavailableException(
51 "Could not initialize database pool. -- "
54 super.getServletContext().setAttribute("mir.confed", new Boolean(true));
59 * Bind the language to the session
61 protected void setLanguage(HttpSession session, String language) {
62 session.setAttribute("Language", language);
65 protected void setLocale(HttpSession session, Locale loc) {
66 session.setAttribute("Locale", loc);
70 * Get the session-bound language
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);
83 * get the locale either from the session or the accept-language header ot the request
84 * this supersedes getLanguage for the new i18n
86 public Locale getLocale(HttpServletRequest req) {
88 HttpSession session = req.getSession(false);
90 // session can be null in case of logout
91 loc = (Locale) session.getAttribute("Locale");
93 // if there is nothing in the session get it fron the accept-language
95 loc = req.getLocale();
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
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");
116 theLog.printDebugInfo("Language: " + lang);