4 import java.lang.reflect.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import javax.servlet.UnavailableException;
10 import freemarker.template.*;
14 import mir.storage.StorageObjectException;
16 import mircoders.servlet.*;
17 import mircoders.module.*;
18 import mircoders.entity.*;
19 import mircoders.storage.*;
22 * Mir.java - main servlet, that dispatches to servletmodules
24 * @author RK 1999-2001
29 public class Mir extends AbstractServlet {
31 private static ModuleUsers usersModule=null;
32 private static ModuleMessage messageModule=null;
33 private final static HashMap servletModuleInstanceHash = new HashMap();
35 public HttpSession session;
37 public void doGet(HttpServletRequest req, HttpServletResponse res)
38 throws ServletException, IOException {
42 public void doPost(HttpServletRequest req, HttpServletResponse res)
43 throws ServletException, IOException, UnavailableException {
45 long startTime = System.currentTimeMillis();
46 long sessionConnectTime=0;
49 // get the configration
50 if(getServletContext().getAttribute("mir.confed") == null) {
53 MirConfig.setServletName(getServletName());
55 session = req.getSession(true);
57 if (req.getServerPort() == 443) http="https"; else http="http";
58 res.setContentType("text/html");
59 String moduleName = req.getParameter("module");
61 if(session.getAttribute("Language")==null){
62 setLanguage(session,getAcceptLanguage(req));
65 /** @todo for cleanup and readability this should be moved to
66 * method loginIfNecessary() */
69 if (moduleName!=null && moduleName.equals("login")) {
70 String user=req.getParameter("login");
71 String passwd= req.getParameter("password");
72 String lang = req.getParameter("lang");
73 theLog.printDebugInfo("--login: evaluating for user: "+user);
74 EntityUsers userEntity = allowedUser(user,passwd);
75 if (userEntity==null) {
76 // login failed: redirecting to login
77 theLog.printWarning("--login: failed!");
78 _sendLoginPage(res,req,res.getWriter());
83 //if accept-language!=selected language change language
84 if(!getLanguage(req,session).equals(lang)){
85 setLanguage(session,lang);
88 theLog.printInfo("--login: successful! setting uid: " + userEntity.getId());
89 session.setAttribute("login.uid", userEntity);
90 theLog.printDebugInfo("--login: trying to retrieve login.target");
91 String target = (String)session.getAttribute("login.target");
94 theLog.printDebugInfo("Redirect: "+target);
95 int serverPort = req.getServerPort();
97 String redirectString ="";
101 redirect=res.encodeURL(http + "://" + req.getServerName() + target);
102 redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
104 + "\"></head><body>going <a href=\"" + redirect + "\">Mir</a></body></html>";
106 redirect=res.encodeURL(http + "://" + req.getServerName() + ":" +req.getServerPort() + target);
107 redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
109 + "\"></head><body>going <a href=\"" + redirect + "\">Mir</a></body></html>";
111 res.getWriter().println(redirectString);
114 //res.sendRedirect(redirect);
117 // redirecting to default target
118 theLog.printDebugInfo("--login: no target - redirecting to default");
119 _sendStartPage(res,req,res.getWriter(),userEntity);
122 } // if login succesful
125 if (moduleName!=null && moduleName.equals("logout")) {
126 theLog.printDebugInfo("--logout");
127 session.invalidate();
128 _sendLoginPage(res,req,res.getWriter());
133 EntityUsers userEntity = (EntityUsers)session.getAttribute("login.uid");
134 if (userEntity == null) {
135 // redirect to loginpage
136 String redirectString = req.getRequestURI();
137 String queryString = req.getQueryString();
138 if (queryString!=null && !queryString.equals("")) {
139 redirectString += "?" + req.getQueryString();
140 theLog.printDebugInfo("STORING: " + redirectString);
141 session.setAttribute("login.target", redirectString);
143 _sendLoginPage(res,req,res.getWriter());
147 // If no module is specified goto standard startpage
148 if (moduleName == null || moduleName.equals("")) {
149 theLog.printDebugInfo("no module: redirect to standardpage");
150 _sendStartPage(res,req, res.getWriter(),userEntity);
155 // From now on regular dispatching...
157 // get servletmodule by parameter and continue with dispacher
158 ServletModule smod = getServletModuleForName(moduleName);
159 ServletModuleDispatch.dispatch(smod,req,res);
161 catch (ServletModuleException e) {
162 handleError(res, res.getWriter(), "ServletException in Module " + moduleName + " -- " + e.toString());
164 catch (ServletModuleUserException e) {
165 handleUserError(res,res.getWriter(), "User error" + e.toString());
169 sessionConnectTime = System.currentTimeMillis() - startTime;
170 theLog.printInfo("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
175 * Private method getServletModuleForName returns ServletModule
178 * @return ServletModule
181 private static ServletModule getServletModuleForName(String moduleName)
182 throws ServletModuleException {
185 if (!servletModuleInstanceHash.containsKey(moduleName)) {
186 // was not found in hash...
188 Class theServletModuleClass=null;
190 // first we try to get ServletModule from stern.che3.servlet
191 theServletModuleClass = Class.forName("mircoders.servlet.ServletModule" + moduleName);
192 } catch (ClassNotFoundException e) {
193 // on failure, we try to get it from lib-layer
194 theServletModuleClass = Class.forName("mir.servlet.ServletModule"+ moduleName);
196 Method m = theServletModuleClass.getMethod("getInstance", null);
197 ServletModule smod = (ServletModule)m.invoke(null, null);
198 // we put it into map for further reference
199 servletModuleInstanceHash.put(moduleName,smod);
201 } catch (Exception e) {
202 throw new ServletModuleException("*** error resolving classname for " +
203 moduleName +" -- "+ e.toString());
206 else return (ServletModule)servletModuleInstanceHash.get(moduleName);
210 private void handleError(HttpServletResponse res, PrintWriter out, String errorString) {
213 theLog.printError(errorString);
214 SimpleHash modelRoot = new SimpleHash();
215 modelRoot.put("errorstring", new SimpleScalar(errorString));
216 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
217 HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"),modelRoot,out);
220 catch (Exception e) {
221 System.err.println("Error in ErrorTemplate");
225 private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) {
228 theLog.printError(errorString);
229 SimpleHash modelRoot = new SimpleHash();
230 modelRoot.put("errorstring", new SimpleScalar(errorString));
231 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
232 HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out);
235 catch (Exception e) {
236 System.err.println("Fehler in UserErrorTemplate");
241 * evaluate login for user / password
243 protected EntityUsers allowedUser(String user, String password) {
245 if (usersModule == null) usersModule = new ModuleUsers(DatabaseUsers.getInstance());
246 return usersModule.getUserForLogin(user, password);
247 } catch(Exception e) {
248 theLog.printDebugInfo(e.toString());
255 private void _sendLoginPage(HttpServletResponse res,HttpServletRequest req, PrintWriter out) {
256 String loginTemplate = "login.template";
257 String sessionUrl=res.encodeURL("");
258 //session = req.getSession(true);
260 theLog.printDebugInfo("login: "+lang);
262 lang=getAcceptLanguage(req);
264 SimpleHash mergeData = new SimpleHash();
265 mergeData.put("session",sessionUrl);
266 HTMLTemplateProcessor.process(res,lang+"/"+loginTemplate, mergeData, out);
267 } catch(HTMLParseException e) {
268 handleError(res, out, "Error in logintemplate.");
272 private void _sendStartPage(HttpServletResponse res, HttpServletRequest req,PrintWriter out, EntityUsers userEntity) {
273 String startTemplate = "start_admin.template";
274 String sessionUrl=res.encodeURL("");
276 // merge with logged in user and messages
277 SimpleHash mergeData = new SimpleHash();
278 mergeData.put("session",sessionUrl);
279 mergeData.put("login_user", userEntity);
280 if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance());
281 mergeData.put("messages", messageModule.getByWhereClause(null, "webdb_create desc",0,10));
282 HTMLTemplateProcessor.process(res,getLanguage(req,session)+"/"+startTemplate, mergeData,out);
285 handleError( res,out, "error while trying to send startpage. " + e.toString());
289 public String getServletInfo(){ return "Mir 1.0 rev02 multilanguage"; }