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
32 private static ModuleUsers usersModule=null;
33 private static ModuleMessage messageModule=null;
34 //private static boolean confed=false;
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 = (new java.util.Date()).getTime();
46 Class theServletModule;
48 long sessionConnectTime=0;
51 // get the configration
52 if(getServletContext().getAttribute("mir.confed") == null) {
55 MirConfig.setServletName(getServletName());
57 session = req.getSession(true);
59 if (req.getServerPort() == 443) http="https"; else http="http";
60 res.setContentType("text/html");
61 String moduleName = req.getParameter("module");
63 if(session.getAttribute("Language")==null){
64 setLanguage(session,getAcceptLanguage(req));
67 /** @todo for cleanup and readability this should be moved to
68 * method loginIfNecessary() */
71 if (moduleName!=null && moduleName.equals("login")) {
72 String user=req.getParameter("login");
73 String passwd= req.getParameter("password");
74 String lang = req.getParameter("lang");
75 theLog.printDebugInfo("--login: evaluating for user: "+user);
76 EntityUsers userEntity = allowedUser(user,passwd);
77 if (userEntity==null) {
78 // login failed: redirecting to login
79 theLog.printWarning("--login: failed!");
80 _sendLoginPage(res,req,res.getWriter());
85 //if accept-language!=selected language change language
86 if(!getLanguage(req,session).equals(lang)){
87 setLanguage(session,lang);
90 theLog.printInfo("--login: successful! setting uid: " + userEntity.getId());
91 session.setAttribute("login.uid", userEntity);
92 theLog.printDebugInfo("--login: trying to retrieve login.target");
93 String target = (String)session.getAttribute("login.target");
96 theLog.printDebugInfo("Redirect: "+target);
97 int serverPort = req.getServerPort();
99 String redirectString ="";
103 redirect=res.encodeURL(http + "://" + req.getServerName() + target);
104 redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
106 + "\"></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);
119 // redirecting to default target
120 theLog.printDebugInfo("--login: no target - redirecting to default");
121 _sendStartPage(res,req,res.getWriter(),userEntity);
124 } // if login succesful
127 if (moduleName!=null && moduleName.equals("logout")) {
128 theLog.printDebugInfo("--logout");
129 session.invalidate();
130 _sendLoginPage(res,req,res.getWriter());
135 EntityUsers userEntity = (EntityUsers)session.getAttribute("login.uid");
136 if (userEntity == null) {
137 // redirect to loginpage
138 String redirectString = req.getRequestURI();
139 String queryString = req.getQueryString();
140 if (queryString!=null && !queryString.equals("")) {
141 redirectString += "?" + req.getQueryString();
142 theLog.printDebugInfo("STORING: " + redirectString);
143 session.setAttribute("login.target", redirectString);
145 _sendLoginPage(res,req,res.getWriter());
149 // If no module is specified goto standard startpage
150 if (moduleName == null || moduleName.equals("")) {
151 theLog.printDebugInfo("no module: redirect to standardpage");
152 _sendStartPage(res,req, res.getWriter(),userEntity);
156 //From now on regular dispatching...
158 /** @todo i do already have the code for a ServletModuleInstanceMap
159 * i will insert it soon. the code is 100times faster than doing
160 * a classForName-lookup with every execution. will be moved to
161 * getServletModuleForName //rk
166 theServletModule = Class.forName("mircoders.servlet.ServletModule" + moduleName);
167 } catch (ClassNotFoundException e) {
168 // searching servletmodule in mir.servlet-classes
169 theServletModule = Class.forName("mir.servlet.ServletModule" + moduleName);
171 //Instantiate the ServletModule
172 Method m = theServletModule.getMethod("getInstance",null);
173 smod = (ServletModule)m.invoke(null,null);
174 ServletModuleDispatch.dispatch(smod,req,res);
176 catch (NoSuchMethodException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " getInstance() not found."); }
177 catch (InvocationTargetException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " target not found."); }
178 catch (ClassNotFoundException e) { handleError(res, res.getWriter(), "ServletModule" + moduleName + " not found."); }
179 catch (IllegalArgumentException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " not found."); }
180 catch (ServletModuleUserException e) {
181 handleUserError(res,res.getWriter(), e.getMsg());
183 catch (ServletModuleException e){ handleError(res,res.getWriter(), "ServletException in Module ServletModule" + moduleName + " -- " + e.toString()); }
184 catch (IllegalAccessException e){
185 handleError(res,res.getWriter(), "No access to class ServletModule" + moduleName + " -- " + e.toString()); }
188 sessionConnectTime = new java.util.Date().getTime() - startTime;
189 theLog.printInfo("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
192 private void handleError(HttpServletResponse res, PrintWriter out, String errorString) {
194 theLog.printError(errorString);
195 SimpleHash modelRoot = new SimpleHash();
196 modelRoot.put("errorstring", new SimpleScalar(errorString));
197 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
198 HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.ErrorTemplate"),modelRoot,out);
201 catch (Exception e) {
202 System.err.println("Error in ErrorTemplate");
206 private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) {
209 theLog.printError(errorString);
210 SimpleHash modelRoot = new SimpleHash();
211 modelRoot.put("errorstring", new SimpleScalar(errorString));
212 modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
213 HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out);
216 catch (Exception e) {
217 System.err.println("Fehler in UserErrorTemplate");
222 * evaluate login for user / password
224 protected EntityUsers allowedUser(String user, String password) {
226 if (usersModule == null) usersModule = new ModuleUsers(DatabaseUsers.getInstance());
227 return usersModule.getUserForLogin(user, password);
228 } catch(Exception e) {
229 theLog.printDebugInfo(e.toString());
236 private void _sendLoginPage(HttpServletResponse res,HttpServletRequest req, PrintWriter out) {
237 String loginTemplate = "login.template";
238 String sessionUrl=res.encodeURL("");
239 //session = req.getSession(true);
241 theLog.printDebugInfo("login: "+lang);
243 lang=getAcceptLanguage(req);
245 SimpleHash mergeData = new SimpleHash();
246 mergeData.put("session",sessionUrl);
247 HTMLTemplateProcessor.process(res,lang+"/"+loginTemplate, mergeData, out);
248 } catch(HTMLParseException e) {
249 handleError(res, out, "Error in logintemplate.");
253 private void _sendStartPage(HttpServletResponse res, HttpServletRequest req,PrintWriter out, EntityUsers userEntity) {
254 String startTemplate = "start_admin.template";
255 String sessionUrl=res.encodeURL("");
257 // merge with logged in user and messages
258 SimpleHash mergeData = new SimpleHash();
259 mergeData.put("session",sessionUrl);
260 mergeData.put("login_user", userEntity);
261 if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance());
262 mergeData.put("messages", HTMLTemplateProcessor.makeSimpleList(messageModule.getByWhereClause(null, "webdb_create desc",0,10)));
263 HTMLTemplateProcessor.process(res,getLanguage(req,session)+"/"+startTemplate, mergeData,out);
266 handleError( res,out, "error while trying to send startpage. " + e.toString());
270 public String getServletInfo(){ return "Mir 1.0 rev02 multilanguage"; }