2 import freemarker.template.SimpleHash;
3 import freemarker.template.SimpleScalar;
4 import mir.misc.HTMLParseException;
5 import mir.misc.HTMLTemplateProcessor;
6 import mir.misc.MirConfig;
7 import mir.misc.StringUtil;
9 import mircoders.entity.EntityUsers;
10 import mircoders.module.ModuleMessage;
11 import mircoders.module.ModuleUsers;
12 import mircoders.storage.DatabaseMessages;
13 import mircoders.storage.DatabaseUsers;
15 import javax.servlet.ServletException;
16 import javax.servlet.UnavailableException;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.lang.reflect.Method;
23 import java.util.GregorianCalendar;
24 import java.util.HashMap;
25 import java.util.Locale;
28 * Mir.java - main servlet, that dispatches to servletmodules
30 * @author RK 1999-2001
35 public class Mir extends AbstractServlet {
37 private static ModuleUsers usersModule = null;
38 private static ModuleMessage messageModule = null;
39 private final static HashMap servletModuleInstanceHash = new HashMap();
41 public HttpSession session;
43 public void doGet(HttpServletRequest req, HttpServletResponse res)
44 throws ServletException, IOException {
48 public void doPost(HttpServletRequest req, HttpServletResponse res)
49 throws ServletException, IOException, UnavailableException {
51 long startTime = System.currentTimeMillis();
52 long sessionConnectTime = 0;
55 // get the configration - this could conflict if 2 mirs are in the
56 // VM maybe? to be checked. -mh
57 if (getServletContext().getAttribute("mir.confed") == null) {
60 MirConfig.setServletName(getServletName());
62 session = req.getSession(true);
64 if (req.getServerPort() == 443) http = "https"; else http = "http";
65 res.setContentType("text/html; charset="
66 +MirConfig.getProp("Mir.DefaultEncoding"));
67 String moduleName = req.getParameter("module");
69 checkLanguage(session, req);
71 /** @todo for cleanup and readability this should be moved to
72 * method loginIfNecessary() */
75 if (moduleName != null && moduleName.equals("login")) {
76 String user = req.getParameter("login");
77 String passwd = req.getParameter("password");
78 theLog.printDebugInfo("--login: evaluating for user: " + user);
79 EntityUsers userEntity = allowedUser(user, passwd);
80 if (userEntity == null) {
81 // login failed: redirecting to login
82 theLog.printWarning("--login: failed!");
83 _sendLoginPage(res, req, res.getWriter());
89 theLog.printInfo("--login: successful! setting uid: " + userEntity.getId());
90 session.setAttribute("login.uid", userEntity);
91 theLog.printDebugInfo("--login: trying to retrieve login.target");
92 String target = (String) session.getAttribute("login.target");
95 theLog.printDebugInfo("Redirect: " + target);
96 int serverPort = req.getServerPort();
98 String redirectString = "";
101 if (serverPort == 80) {
102 redirect = res.encodeURL(http + "://" + req.getServerName() + target);
103 redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
105 + "\"></head><body>going <a href=\"" + redirect + "\">Mir</a></body></html>";
108 redirect = res.encodeURL(http + "://" + req.getServerName() + ":" + req.getServerPort() + target);
109 redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
111 + "\"></head><body>going <a href=\"" + redirect + "\">Mir</a></body></html>";
113 res.getWriter().println(redirectString);
116 //res.sendRedirect(redirect);
120 // redirecting to default target
121 theLog.printDebugInfo("--login: no target - redirecting to default");
122 _sendStartPage(res, req, res.getWriter(), userEntity);
125 } // if login succesful
128 if (moduleName != null && moduleName.equals("logout")) {
129 theLog.printDebugInfo("--logout");
130 session.invalidate();
132 //session = req.getSession(true);
133 //checkLanguage(session, req);
134 _sendLoginPage(res, req, res.getWriter());
139 EntityUsers userEntity = (EntityUsers) session.getAttribute("login.uid");
140 if (userEntity == null) {
141 // redirect to loginpage
142 String redirectString = req.getRequestURI();
143 String queryString = req.getQueryString();
144 if (queryString != null && !queryString.equals("")) {
145 redirectString += "?" + req.getQueryString();
146 theLog.printDebugInfo("STORING: " + redirectString);
147 session.setAttribute("login.target", redirectString);
149 _sendLoginPage(res, req, res.getWriter());
153 // If no module is specified goto standard startpage
154 if (moduleName == null || moduleName.equals("")) {
155 theLog.printDebugInfo("no module: redirect to standardpage");
156 _sendStartPage(res, req, res.getWriter(), userEntity);
161 // From now on regular dispatching...
163 // get servletmodule by parameter and continue with dispacher
164 ServletModule smod = getServletModuleForName(moduleName);
165 ServletModuleDispatch.dispatch(smod, req, res);
167 catch (ServletModuleException e) {
168 handleError(req, res, res.getWriter(),
169 "ServletException in Module " + moduleName + " -- " + e.toString());
171 catch (ServletModuleUserException e) {
172 handleUserError(req, res, res.getWriter(), "User error" + e.toString());
176 sessionConnectTime = System.currentTimeMillis() - startTime;
177 theLog.printInfo("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
182 * Private method getServletModuleForName returns ServletModule
185 * @return ServletModule
188 private static ServletModule getServletModuleForName(String moduleName)
189 throws ServletModuleException {
192 if (!servletModuleInstanceHash.containsKey(moduleName)) {
193 // was not found in hash...
195 Class theServletModuleClass = null;
197 // first we try to get ServletModule from stern.che3.servlet
198 theServletModuleClass = Class.forName("mircoders.servlet.ServletModule" + moduleName);
200 catch (ClassNotFoundException e) {
201 // on failure, we try to get it from lib-layer
202 theServletModuleClass = Class.forName("mir.servlet.ServletModule" + moduleName);
204 Method m = theServletModuleClass.getMethod("getInstance", null);
205 ServletModule smod = (ServletModule) m.invoke(null, null);
206 // we put it into map for further reference
207 servletModuleInstanceHash.put(moduleName, smod);
210 catch (Exception e) {
211 throw new ServletModuleException("*** error resolving classname for " +
212 moduleName + " -- " + e.toString());
216 return (ServletModule) servletModuleInstanceHash.get(moduleName);
220 private void handleError(HttpServletRequest req, HttpServletResponse res,
221 PrintWriter out, String errorString) {
224 theLog.printError(errorString);
225 SimpleHash modelRoot = new SimpleHash();
226 modelRoot.put("errorstring", new SimpleScalar(errorString));
227 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
228 HTMLTemplateProcessor.process(res, MirConfig.getProp("Mir.ErrorTemplate"), modelRoot, out, getLocale(req));
231 catch (Exception e) {
232 System.err.println("Error in ErrorTemplate");
236 private void handleUserError(HttpServletRequest req, HttpServletResponse res,
237 PrintWriter out, String errorString) {
239 theLog.printError(errorString);
240 SimpleHash modelRoot = new SimpleHash();
241 modelRoot.put("errorstring", new SimpleScalar(errorString));
242 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
243 HTMLTemplateProcessor.process(res, MirConfig.getProp("Mir.UserErrorTemplate"),
244 modelRoot, out, getLocale(req));
247 catch (Exception e) {
248 System.err.println("Fehler in UserErrorTemplate");
254 * evaluate login for user / password
256 protected EntityUsers allowedUser(String user, String password) {
258 if (usersModule == null) usersModule = new ModuleUsers(DatabaseUsers.getInstance());
259 return usersModule.getUserForLogin(user, password);
261 catch (Exception e) {
262 theLog.printDebugInfo(e.toString());
269 private void _sendLoginPage(HttpServletResponse res, HttpServletRequest req, PrintWriter out) {
270 String loginTemplate = MirConfig.getProp("Mir.LoginTemplate");//"login.template";
271 // theLog.printDebugInfo("login template: "+loginTemplate);
272 String sessionUrl = res.encodeURL("");
273 //session = req.getSession(true);
275 //theLog.printDebugInfo("login: "+lang);
277 // lang=getAcceptLanguage(req);
279 SimpleHash mergeData = new SimpleHash();
280 mergeData.put("session", sessionUrl);
281 HTMLTemplateProcessor.process(res, loginTemplate, mergeData, out, getLocale(req));
283 catch (HTMLParseException e) {
284 handleError(req, res, out, "Error in logintemplate.");
288 private void _sendStartPage(HttpServletResponse res, HttpServletRequest req, PrintWriter out, EntityUsers userEntity) {
289 String startTemplate = "admin/start_admin.template";
290 String sessionUrl = res.encodeURL("");
292 // merge with logged in user and messages
293 SimpleHash mergeData = new SimpleHash();
294 mergeData.put("session", sessionUrl);
295 mergeData.put("login_user", userEntity);
296 if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance());
297 mergeData.put("messages", messageModule.getByWhereClause(null, "webdb_create desc", 0, 10));
298 HTMLTemplateProcessor.process(res, startTemplate, mergeData, out, getLocale(req));
300 catch (Exception e) {
301 handleError(req, res, out, "error while trying to send startpage. " + e.toString());
305 public String getServletInfo() {
306 return "Mir "+MirConfig.getProp("Mir.Version");
309 private void checkLanguage(HttpSession session, HttpServletRequest req) {
311 // a lang parameter always sets the language
312 String lang = req.getParameter("lang");
314 theLog.printInfo("selected language "+lang+" overrides accept-language");
315 setLanguage(session, lang);
316 setLocale(session, new Locale(lang, ""));
318 // otherwise store language from accept header in session
319 else if (session.getAttribute("Language") == null) {
320 theLog.printInfo("accept-language is "+req.getLocale().getLanguage());
321 setLanguage(session, req.getLocale().getLanguage());
322 setLocale(session, req.getLocale());