context.put("preset_realm", realm);
    private Template genLoginTemplate(HttpServletRequest request, HttpServletResponse response, Context context, Session session, String errorType) throws ParseErrorException, ResourceNotFoundException, MissingResourceException, Exception {        String bundleName = "login";        String acceptLanguage = request.getHeader("Accept-Language");        String sessionID = null;        ResourceBundle bundle = null;        ResourceBundle fallback = null;        if (session != null) {            sessionID = session.getID();            if (!session.authenticationInitiated()) {                log.warning("User tried to authenticate without requesting login page first. "+session.getID());                throw new Exception("Login page has to be requested before attempting login.");            }        }        if (acceptLanguage == null || acceptLanguage.equals(""))             acceptLanguage = "no";        StringTokenizer tokenizer = new StringTokenizer(acceptLanguage, ",");        Locale locale = null;        /* Find fallback resource bundle. */        try {            fallback = ResourceBundle.getBundle(bundleName, new Locale("bogus"));        }        catch (MissingResourceException e) {            /* No fallback */        }                    /* Parse Accept-Language and find matching resource bundle */        while (tokenizer.hasMoreTokens()) {            String lang = tokenizer.nextToken();            int index;            /* Languages are devided by ";" */            if ((index = lang.indexOf(";")) != -1) {                lang = lang.substring(0, index);            }            lang = lang.trim();            /* Language and country is devided by "-" (optional) */            if ((index = lang.indexOf("-")) != -1) {                lang = lang.substring(0, index);            }                        locale = new Locale(lang);            bundle = ResourceBundle.getBundle(bundleName, locale);            /* Abort search if a bundle (not fallback) is found */            if (bundle != fallback)                 break;            /* About search if the fallback bundle is actually requested */            else if (bundle == fallback && locale.getLanguage().equals(Locale.getDefault().getLanguage()))                break;        }        /* Should never happen, but just in case. */        if (bundle == null)            bundle = ResourceBundle.getBundle(bundleName, new Locale("no"));               String wsName = null;        String wsURL  = null;        if (session != null) {            wsName = session.getWebService().getName();            wsURL  = session.getWebService().getUrl();        }        /* Set template-variables from properties */        for (Enumeration e = bundle.getKeys(); e.hasMoreElements();) {            String key = (String) e.nextElement();            String value = bundle.getString(key);            int index;                        /* This changes WS_NAME to hyperlink in all property             * strings from the bundle.*/            if ((index = value.indexOf("WS_NAME")) != -1 && wsName != null) {                value = value.substring(0, index)+"<A href=\""+wsURL+"\">"+wsName+"</A>"+value.substring(index+7, value.length());            }            context.put(key, value);        }           /* Get realm from cookie. */        Cookie[] cookies = request.getCookies();        context.put("preset_realm", "");        if (cookies != null) {            for (int i = 0; i < cookies.length; i++) {                if (cookies[i].getName().equals("realm")) {                    context.put("preset_realm", cookies[i].getValue());                }            }        }        /* Set or reset error messages */        if (errorType != null) {            context.put("errorMessage", context.get("error_"+errorType));            context.put("errorDescription", context.get("error_"+errorType+"_desc"));        }             else {            context.remove("errorMessage");            context.remove("errorDescription");        }        if (sessionID != null) {             context.put("loginURL", loginURL+"?id="+sessionID);            String secLevel = session.getAttributesSecLevel().toLowerCase();            context.put("expl_data", context.get("expl_data_"+secLevel));            /* Detailed list of attributes */            if (showAllAttributes) {                Vector attrNames = new Vector();                HashMap attributes = session.getWebService().getAttributes();                for (Iterator it = attributes.keySet().iterator(); it.hasNext();) {                    attrNames.add(context.get("ldap_"+it.next()));                }                context.put("attrNames", attrNames);                /* Link to page with detailed attribute list. */                if (context.get("attrs_hide") != null)                    context.put("showHideLink", "<A href=\""+loginURL+"?id="+sessionID+"\">"+context.get("attrs_hide")+"</A>");                else                     context.put("showHideLink", "");            }            else if (context.get("attrs_show") != null) {                context.put("showHideLink", "<A href=\""+loginURL+"?id="+sessionID+"&showAttrs=yes\">"+context.get("attrs_show")+"</A>");            }            else                 context.put("showHideLink", "");        }        else             /* If no sessionID then remove loginURL */            context.remove("loginURL");                return getTemplate("login.vtl");    }