coding style cleanup.
[mir.git] / source / Mir.java
index d4fca0c..1c0de3e 100755 (executable)
@@ -51,9 +51,10 @@ public class Mir extends AbstractServlet
     if (!confed){
       confed = getConfig(req);
     }
+    MirConfig.setServletName(getServletName());
 
     session = req.getSession(true);
-      
+
     if (req.getServerPort() == 443) http="https"; else http="http";
     res.setContentType("text/html");
     String moduleName = req.getParameter("module");
@@ -86,14 +87,14 @@ public class Mir extends AbstractServlet
         session.setAttribute("login.uid", userEntity);
         theLog.printDebugInfo("--login: trying to retrieve login.target");
         String target = (String)session.getAttribute("login.target");
-        
+
         if (target != null) {
           theLog.printDebugInfo("Redirect: "+target);
           int serverPort = req.getServerPort();
           String redirect = "";
           String redirectString ="";
-          
-          
+
+
           if(serverPort==80){
             redirect=res.encodeURL(http + "://" + req.getServerName() + target);
             redirectString = "<html><head><meta http-equiv=refresh content=\"1;URL="
@@ -106,10 +107,10 @@ public class Mir extends AbstractServlet
                     + "\"></head><body>going <a href=\"" + redirect + "\">Mir</a></body></html>";
           }
           res.getWriter().println(redirectString);
-          
-          
+
+
           //res.sendRedirect(redirect);
-          
+
         } else {
           // redirecting to default target
           theLog.printDebugInfo("--login: no target - redirecting to default");
@@ -131,40 +132,46 @@ public class Mir extends AbstractServlet
     if (userEntity == null) {
       // redirect to loginpage
       String redirectString = req.getRequestURI();
-      if (req.getQueryString()!=null) redirectString += "?" + req.getQueryString();
-      theLog.printDebugInfo("STORING: " + redirectString);
-      session.setAttribute("login.target", redirectString);
+      String queryString = req.getQueryString();
+      if (queryString!=null && !queryString.equals("")) {
+        redirectString += "?" + req.getQueryString();
+        theLog.printDebugInfo("STORING: " + redirectString);
+        session.setAttribute("login.target", redirectString);
+      }
       _sendLoginPage(res,req,res.getWriter());
       return;
     }
 
-    // Bei blossem Serveltaufruf redirect auf Standardstarttemplate
+    // If no module is specified goto standard startpage
     if (moduleName == null || moduleName.equals("")) {
       theLog.printDebugInfo("no module: redirect to standardpage");
       _sendStartPage(res,req, res.getWriter(),userEntity);
       return;
     }
 
-    //From now on normal Dispatching...
+    //From now on regular dispatching...
     try {
       try {
         theServletModule = Class.forName("mircoders.servlet.ServletModule" + moduleName);
       } catch (ClassNotFoundException e) {
-        // searching Servlet webdb.servlet-classes
+        // searching servletmodule in mir.servlet-classes
         theServletModule = Class.forName("mir.servlet.ServletModule" + moduleName);
       }
-      //Instanciate the ServletModule
+      //Instantiate the ServletModule
       Method m = theServletModule.getMethod("getInstance",null);
       smod = (ServletModule)m.invoke(null,null);
       ServletModuleDispatch.dispatch(smod,req,res);
     }
-    catch (NoSuchMethodException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " getInstance() nicht gefunden."); }
-    catch (InvocationTargetException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " target nicht gefunden."); }
-    catch (ClassNotFoundException e) { handleError(res, res.getWriter(), "ServletModule" + moduleName + " nicht gefunden."); }
-    catch (IllegalArgumentException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " nicht gefunden."); }
+    catch (NoSuchMethodException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " getInstance() not found."); }
+    catch (InvocationTargetException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " target not found."); }
+    catch (ClassNotFoundException e) { handleError(res, res.getWriter(), "ServletModule" + moduleName + " not found."); }
+    catch (IllegalArgumentException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " not found."); }
+    catch (ServletModuleUserException e) {
+      handleUserError(res,res.getWriter(), e.getMsg());
+    }
     catch (ServletModuleException e){ handleError(res,res.getWriter(), "ServletException in Module ServletModule" + moduleName + " -- " + e.toString()); }
     catch (IllegalAccessException e){
-        handleError(res,res.getWriter(), "Kein Zugriff auf Klasse ServletModule" + moduleName + " -- " + e.toString()); }
+        handleError(res,res.getWriter(), "No access to class ServletModule" + moduleName + " -- " + e.toString()); }
 
     // timing...
     sessionConnectTime = new java.util.Date().getTime() - startTime;
@@ -181,10 +188,25 @@ public class Mir extends AbstractServlet
       out.close();
     }
     catch (Exception e) {
-      System.err.println("Fehler in ErrorTemplate");
+      System.err.println("Error in ErrorTemplate");
     }
   }
 
+  private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) {
+
+    try {
+      theLog.printError(errorString);
+      SimpleHash modelRoot = new SimpleHash();
+      modelRoot.put("errorstring", new SimpleScalar(errorString));
+      modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
+      HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out);
+      out.close();
+    }
+    catch (Exception e) {
+      System.err.println("Fehler in UserErrorTemplate");
+    }
+
+  }
   /**
    *  evaluate login for user / password
    */
@@ -192,8 +214,11 @@ public class Mir extends AbstractServlet
     try {
       if (usersModule == null) usersModule = new ModuleUsers(DatabaseUsers.getInstance());
       return usersModule.getUserForLogin(user, password);
+    } catch(Exception e) {
+      theLog.printDebugInfo(e.toString());
+      e.printStackTrace();
+      return null;
     }
-    catch(Exception e) { theLog.printDebugInfo(e.toString()); return null; }
   }
 
   // Redirect-methods
@@ -210,7 +235,7 @@ public class Mir extends AbstractServlet
       mergeData.put("session",sessionUrl);
       HTMLTemplateProcessor.process(res,lang+"/"+loginTemplate, mergeData, out);
     } catch(HTMLParseException e) {
-      handleError(res, out, "fehler in logintemplate.");
+      handleError(res, out, "Error in logintemplate.");
     }
   }
 
@@ -221,7 +246,7 @@ public class Mir extends AbstractServlet
       // merge with logged in user and messages
       SimpleHash mergeData = new SimpleHash();
       mergeData.put("session",sessionUrl);
-      mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(userEntity));
+      mergeData.put("login_user", userEntity);
       if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance());
       mergeData.put("messages", HTMLTemplateProcessor.makeSimpleList(messageModule.getByWhereClause(null, "webdb_create desc",0,10)));
       HTMLTemplateProcessor.process(res,getLanguage(req,session)+"/"+startTemplate, mergeData,out);