stats.loginAttempt(session.getWebService().getId(), "SUCCESS");
    private Template attemptLogin(HttpServletRequest request, HttpServletResponse response, Context context) throws ParseErrorException, ResourceNotFoundException, Exception {        log.finer("attemptLogin(HttpServletRequest, HttpServletResponse, Context)");        Session session = null;        String id       = request.getParameter("id");        String username = request.getParameter("username");        String realm    = request.getParameter("realm");        String password = request.getParameter("password");        /* Concatinate realm with username */        if ((realm != null && !realm.equals("")) && (username != null && username.indexOf("@") == -1))            username += "@"+realm;        /* Get session */        try {            session = sessionStore.getSession(id);        }                catch (NoSuchSessionException e) {            return genLoginTemplate(request, response, context, null, NOSESSION);        }        catch (SessionException e) {            return genLoginTemplate(request, response, context, null, GENERIC);        }        String log_prefix = "Authentication attempt from "+username+": ";        if (!session.authenticationInitiated()) {            log.warning(log_prefix+"DENIED, Login page not requested SID="+session.getID());            throw new Exception("Premature login attempt.");        }        /* Authenticate */        try {            Credentials c = new Credentials(username, password);            if (!session.authenticateUser(c)) {                log.info(log_prefix+"FAILED");                        /* If the user has exceeded the maximum login                attempts, the session is now gone. */                try {                    session = sessionStore.getSession(id);                }                catch (NoSuchSessionException e) {                    return genLoginTemplate(request, response, context, null, MAXLOGIN);                }                return genLoginTemplate(request, response, context,                                         session, AUTHFAILED);            }        }                 catch (BackendException e) {            log.severe("BackendException caught and re-thrown as ServletException\n"+e);            return genLoginTemplate(request, response, context, null, GENERIC);        }         catch (SessionException e) {            log.severe("SessionException caught and re-thrown as ServletException\n"+e);                return genLoginTemplate(request, response, context, null, GENERIC);        }        /* Success; redirect to the original session URL and         * include the updated session ID in URL and HttpSession. */                log.info(log_prefix+"SUCCESS");        HttpSession httpSession =             ((HttpServletRequest)request).getSession(true);                httpSession.setAttribute("moriaID", session.getID());        /* Remember realm (cookie). */        Cookie cookie = new Cookie("realm", realm);        int validDays = new Integer(Configuration.getProperty("no.feide.moria.servlet.realmCookieValidDays")).intValue();        cookie.setMaxAge(validDays*24*60*60); // Days to seconds        cookie.setComment("Home organization");        cookie.setVersion(1);        response.addCookie(cookie);        redirectToWebService(response, session);            return null; // Do not use template for redirect.    }