Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 33712)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 33713)
@@ -423,54 +423,9 @@
     logUsageInfo(request, queryMap);
 
-    if (queryMap != null)
-      {
-	Iterator<String> queryIter = queryMap.keySet().iterator();
-	boolean redirect = false;
-	String href = null;
-	String rl = null;
-	String el = null;
-
-	while (queryIter.hasNext())
-	  {
-	    String q = queryIter.next();
-	    if (q.equals(GSParams.EXTERNAL_LINK_TYPE))
-	      {
-		el = queryMap.get(q)[0];
-	      }
-	    else if (q.equals(GSParams.HREF))
-	      {
-		href = queryMap.get(q)[0];
-		href = StringUtils.replace(href, "%2f", "/");
-		href = StringUtils.replace(href, "%7e", "~");
-		href = StringUtils.replace(href, "%3f", "?");
-		href = StringUtils.replace(href, "%3A", "\\:");
-	      }
-	    else if (q.equals(GSParams.RELATIVE_LINK))
-	      {
-		rl = queryMap.get(q)[0];
-	      }
-	  }
-
-	//if query_string contains "el=direct", an href is specified, and its not a relative link, then the web page will be redirected to the external URl, otherwise a greenstone page with an external URL will be displayed
-	//"rl=0" this is an external link
-	//"rl=1" this is an internal link
-	if ((href != null) && (rl.equals("0")))
-	  {// This is an external link, 
-
-	    if (el.equals("framed"))
-	      {
-		//TODO **** how best to change to a=p&sa=html&c=collection&url=href
-		// response.setContentType("text/xml");
-		//response.sendRedirect("http://localhost:8383/greenstone3/gs3library?a=p&sa=html&c=external&url="+href);
-	      }
-	    else
-	      {
-		// el = '' or direct
-		//the web page is re-directed to the external URL (&el=&rl=0&href="http://...")
-		response.setContentType("text/xml");
-		response.sendRedirect(href);
-	      }
-	  }
-      }
+    if (processRedirectRequest(request, response, queryMap)) {
+	// this method will do the redirect if needed and return true if it has
+	// if el=direct/framed&rl=0&href=http://newurl.com
+	return;
+    }
 
     // Nested Diagnostic Configurator to identify the client for
@@ -493,43 +448,45 @@
 	  }
       }
+
+    // set the lang in the session
+    session.setAttribute(GSParams.LANGUAGE, lang);
+
+    String output = getFirstParam(GSParams.OUTPUT, queryMap);
+    if (output == null || output.equals(""))
+      {
+	output = "html"; // uses html by default
+      }
+
+    String requestedURL = request.getRequestURL().toString();
+    String baseURL = "";
+    if (requestedURL.indexOf(library_name) != -1)
+    {
+	baseURL = requestedURL.substring(0, requestedURL.indexOf(library_name));
+    }
+    String fullURL;
+    if (request.getQueryString() != null)
+    {
+	fullURL = requestedURL + "?" + request.getQueryString();
+    }
+    else
+    {
+	fullURL = requestedURL;
+    }
+
     UserContext userContext = new UserContext();
     userContext.setLanguage(lang);
     userContext.setUserID(uid);
-    logger.error("LS new user context");
+
+    if (!processLoginChanges(request, userContext, out, baseURL, queryMap, lang, output)) {
+	// any invalid login attempt will redirect to a new login page and return false
+      return; 
+    }
+
+ 
     if (request.getAuthType() != null)
-      {
-	//Get the username
-	userContext.setUsername(request.getUserPrincipal().getName());
-	logger.error("setting username = "+request.getUserPrincipal().getName());
-	//Get the groups for the user
-	Document msg_doc = XMLConverter.newDOM();
-	Element acquireGroupMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
-	Element acquireGroupRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, "GetUserInformation", userContext);
-	acquireGroupMessage.appendChild(acquireGroupRequest);
-
-	Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
-	acquireGroupRequest.appendChild(paramList);
-	paramList.appendChild(GSXML.createParameter(msg_doc, GSXML.USERNAME_ATT, request.getUserPrincipal().getName()));
-
-	Element aquireGroupsResponseMessage = (Element) this.recept.process(acquireGroupMessage);
-	Element aquireGroupsResponse = (Element) GSXML.getChildByTagName(aquireGroupsResponseMessage, GSXML.RESPONSE_ELEM);
-	Element param_list = (Element) GSXML.getChildByTagName(aquireGroupsResponse, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
-
-	if (param_list != null)
-	  {
-	    HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
-	    String groups = (String) params.get("groups");
-	    userContext.setGroups(groups.split(","));
-	  }
-      }
-
-    // set the lang in the session
-    session.setAttribute(GSParams.LANGUAGE, lang);
-
-    String output = getFirstParam(GSParams.OUTPUT, queryMap);
-    if (output == null || output.equals(""))
-      {
-	output = "html"; // uses html by default
-      }
+    {
+	// sets username, groups etc into usercontext
+	updateUserContextWithAuthenticatedInfo(request, userContext);
+    }
 
     // If server output, force a switch to traditional interface
@@ -559,11 +516,4 @@
       }
 
-    // the request to the receptionist
-    Document msg_doc = XMLConverter.newDOM();
-    Element xml_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
-    Element xml_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
-    xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
-
-    xml_message.appendChild(xml_request);
 
     String action = getFirstParam(GSParams.ACTION, queryMap);
@@ -574,4 +524,10 @@
     String specified_cache_key = getFirstParam(GSParams.CACHE_KEY, queryMap);
 
+    if (collection != null && !collection.equals("")) {
+	//is this user allowed to access this collection/document? 
+	if (!runCollectionSecurityCheck(request, userContext, out, baseURL, collection, document, lang, output)) {
+	    return;
+	}
+    }
     // We clean up the cache session_ids_table if system
     // commands are issued, and also don't need to do caching for these request requests
@@ -714,18 +670,31 @@
     }
 
+    // the request to the receptionist
+    Document msg_doc = XMLConverter.newDOM();
+    Element xml_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
+    Element xml_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
+    xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
+
+    xml_message.appendChild(xml_request);
+
+    if (request.getAuthType() != null) {
+	// lots of classes are using the <userInformation> element. But all its info is now in userContext, so maybe we can get rid of this one day?
+	appendUserInformation(xml_request, userContext);
+    }
+    
     if (action == null || action.equals(""))
-      {
+    {
 	// display the home page  - the default page
 	xml_request.setAttribute(GSXML.ACTION_ATT, "p");
 	xml_request.setAttribute(GSXML.SUBACTION_ATT, PageAction.HOME_PAGE);
-      }
+    }
     else
-      {
+    {
 	xml_request.setAttribute(GSXML.ACTION_ATT, action);
 	if (subaction != null)
-	  {
+	{
 	    xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
-	  }
-      }
+	}
+    }
 		
     //  create the param list for the greenstone request - includes
@@ -842,28 +811,8 @@
       }
 
-    String requestedURL = request.getRequestURL().toString();
-    String baseURL = "";
-    if (requestedURL.indexOf(library_name) != -1)
-      {
-	baseURL = requestedURL.substring(0, requestedURL.indexOf(library_name));
-	xml_request.setAttribute("baseURL", baseURL);
-      }
-    String fullURL;
-    if (request.getQueryString() != null)
-      {
-	fullURL = requestedURL + "?" + request.getQueryString();
-      }
-    else
-      {
-	fullURL = requestedURL;
-      }
 
     xml_request.setAttribute("remoteAddress", request.getRemoteAddr());
     xml_request.setAttribute("fullURL", fullURL.replace("&", "&amp;"));
-
-    if (!runSecurityChecks(request, xml_request, userContext, out, baseURL, collection, document, queryMap, lang))
-      {
-	return;
-      }
+    xml_request.setAttribute("baseURL", baseURL);
 
     logger.error("about to process this message");
@@ -901,121 +850,195 @@
   } //end of doGet(HttpServletRequest, HttpServletResponse)
 
-  private boolean runSecurityChecks(HttpServletRequest request, Element xml_request, UserContext userContext, PrintWriter out, String baseURL, String collection, String document, Map<String, String[]> queryMap, String lang) throws ServletException
-  {
-    logger.error("kk in run security");
-    //Check if we need to login or logout
-    String username = getFirstParam(GSParams.USERNAME, queryMap);
-    String password = getFirstParam(GSParams.PASSWORD, queryMap);
-    String logout = getFirstParam(GSParams.LOGOUT, queryMap);
-
-    if (logout != null)
-      {
-	request.logout();
-      }
-
-    if (username != null && password != null)
-      {
-	logger.error("kk u&p not null");
-	//We are changing to another user, so log out first
-	if (request.getAuthType() != null)
-	  {
+    private boolean processRedirectRequest(HttpServletRequest request, HttpServletResponse response, Map<String, String[]> queryMap) throws IOException
+    {
+    if (queryMap != null)
+    {
+	Iterator<String> queryIter = queryMap.keySet().iterator();
+	boolean redirect = false;
+	String href = null;
+	String rl = null;
+	String el = null;
+
+	while (queryIter.hasNext())
+	{
+	    String q = queryIter.next();
+	    if (q.equals(GSParams.EXTERNAL_LINK_TYPE))
+	    {
+		el = queryMap.get(q)[0];
+	    }
+	    else if (q.equals(GSParams.HREF))
+	    {
+		href = queryMap.get(q)[0];
+		href = StringUtils.replace(href, "%2f", "/");
+		href = StringUtils.replace(href, "%7e", "~");
+		href = StringUtils.replace(href, "%3f", "?");
+		href = StringUtils.replace(href, "%3A", "\\:");
+	    }
+	    else if (q.equals(GSParams.RELATIVE_LINK))
+	    {
+		rl = queryMap.get(q)[0];
+	    }
+	}
+
+	//if query_string contains "el=direct", an href is specified, and its not a relative link, then the web page will be redirected to the external URl, otherwise a greenstone page with an external URL will be displayed
+	//"rl=0" this is an external link
+	//"rl=1" this is an internal link
+	if ((href != null) && (rl.equals("0")))
+	  {// This is an external link, 
+
+	    if (el.equals("framed"))
+	      {
+		//TODO **** how best to change to a=p&sa=html&c=collection&url=href
+		// response.setContentType("text/xml");
+		//response.sendRedirect("http://localhost:8383/greenstone3/gs3library?a=p&sa=html&c=external&url="+href);
+	      }
+	    else
+	      {
+		// el = '' or direct
+		//the web page is re-directed to the external URL (&el=&rl=0&href="http://...")
+		//response.setContentType("text/xml");
+		//response.sendRedirect(href);
+	      }
+	    return true;
+	  }
+      }
+    return false;
+    }
+    
+    private void generateLoginPage(String query_string, String error_message, UserContext userContext, PrintWriter out, String baseURL, String output) {
+	
+	Document loginPageDoc = XMLConverter.newDOM();
+	Element loginPageMessage = loginPageDoc.createElement(GSXML.MESSAGE_ELEM);
+	Element loginPageRequest = GSXML.createBasicRequest(loginPageDoc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
+	loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
+	loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");    
+	loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, output);
+	loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
+	
+	loginPageMessage.appendChild(loginPageRequest);
+	
+	Element paramList = loginPageDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
+	loginPageRequest.appendChild(paramList);
+	
+	Element messageParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
+	messageParam.setAttribute(GSXML.NAME_ATT, LOGIN_MESSAGE_PARAM);
+	messageParam.setAttribute(GSXML.VALUE_ATT, error_message);
+	paramList.appendChild(messageParam);
+	
+	Element urlParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
+	urlParam.setAttribute(GSXML.NAME_ATT, REDIRECT_URL_PARAM);
+	urlParam.setAttribute(GSXML.VALUE_ATT, library_name + query_string);
+	paramList.appendChild(urlParam);
+	
+	Node loginPageResponse = this.recept.process(loginPageMessage);
+	out.println(XMLConverter.getPrettyString(loginPageResponse));
+	
+    }
+    
+    private boolean processLoginChanges(HttpServletRequest request, UserContext userContext, PrintWriter out, String baseURL, Map<String, String[]> queryMap, String lang, String output) throws ServletException {
+	
+	logger.error("kk in processLoginChanges");
+	//Check if we need to login or logout
+	String username = getFirstParam(GSParams.USERNAME, queryMap);
+	String password = getFirstParam(GSParams.PASSWORD, queryMap);
+	String logout = getFirstParam(GSParams.LOGOUT, queryMap);
+	
+	if (logout != null)
+	{
+	    logger.error("plc logging out");
 	    request.logout();
-	  }
-
-	//This try/catch block catches when the login request fails (e.g. The user enters an incorrect password).
-	try
-	  {
-	    //Try a global login first
-	    password = Authentication.hashPassword(password);
-	    request.login(username, password);
-	  }
-	catch (Exception ex)
-	  {
+	}
+	
+	if (username != null && password != null)
+	{
+	    logger.error("kk plc u&p not null");
+	    //We are changing to another user, so log out first
+	    if (request.getAuthType() != null)
+	    {
+		logger.error("plc logging out 2");
+		request.logout();
+	    }
+	    
+	    //This try/catch block catches when the login request fails (e.g. The user enters an incorrect password).
 	    try
-	      {
-		//If the global login fails then try a site-level login
-		String siteName = (String) this.recept.getConfigParams().get(GSConstants.SITE_NAME);
-		request.login(siteName + "-" + username, password);
-	      }
-	    catch (Exception exc)
-	      {
-		//The user entered in either the wrong username or the wrong password
-		Document loginPageDoc = XMLConverter.newDOM();
-		Element loginPageMessage = loginPageDoc.createElement(GSXML.MESSAGE_ELEM);
-		Element loginPageRequest = GSXML.createBasicRequest(loginPageDoc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
-		loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
-		loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
-		loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
-		loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
-		loginPageMessage.appendChild(loginPageRequest);
-
-		Element paramList = loginPageDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
-		loginPageRequest.appendChild(paramList);
-
-		Element messageParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
-		messageParam.setAttribute(GSXML.NAME_ATT, LOGIN_MESSAGE_PARAM);
-		messageParam.setAttribute(GSXML.VALUE_ATT, getTextString("auth.error.un_or_pw_err", lang));
-		paramList.appendChild(messageParam);
-
-		Element urlParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
-		urlParam.setAttribute(GSXML.NAME_ATT, REDIRECT_URL_PARAM);
-		String queryString = "";
-		if (request.getQueryString() != null)
-		  {
-		    queryString = "?" + request.getQueryString().replace("&", "&amp;");
-		  }
-		urlParam.setAttribute(GSXML.VALUE_ATT, library_name + queryString);
-		paramList.appendChild(urlParam);
-
-		Node loginPageResponse = this.recept.process(loginPageMessage);
-		out.println(XMLConverter.getPrettyString(loginPageResponse));
-
-		return false;
-	      }
-	  }
-      }
-
-    //If a user is logged in
-    if (request.getAuthType() != null)
-      {
-	logger.error("kk auth type not null "+ request.getAuthType());
-	Element userInformation = xml_request.getOwnerDocument().createElement(GSXML.USER_INFORMATION_ELEM);
-	userInformation.setAttribute(GSXML.USERNAME_ATT, request.getUserPrincipal().getName());
-	logger.error("setting user info username "+request.getUserPrincipal().getName());
+	    {
+		//Try a global login first
+		password = Authentication.hashPassword(password);
+		request.login(username, password);
+		logger.error("plc logged in");
+	    }
+	    catch (Exception ex)
+	    {
+		try
+		{
+		    //If the global login fails then try a site-level login
+		    String siteName = (String) this.recept.getConfigParams().get(GSConstants.SITE_NAME);
+		    request.login(siteName + "-" + username, password);
+		    logger.error("plc logged in site wide");
+		}
+		catch (Exception exc)
+		{
+		    //The user entered in either the wrong username or the wrong password
+		    String query_string = "";
+		    if (request.getQueryString() != null)
+		    {
+			query_string = "?" + request.getQueryString().replace("&", "&amp;");
+		    }
+		
+		    generateLoginPage(query_string, getTextString("auth.error.un_or_pw_err", lang), userContext, out, baseURL, output);
+		    return false;
+		}
+	    }
+	}
+	return true;
+
+
+    }
+
+    private void updateUserContextWithAuthenticatedInfo(HttpServletRequest request, UserContext userContext)
+    {
+	logger.error("in updateUserContext");
+	//Get the username
+	String user_name = request.getUserPrincipal().getName();
+	userContext.setUsername(user_name);
+	logger.error("setting username = "+request.getUserPrincipal().getName());
+	//Get the groups for the user
 	Document msg_doc = XMLConverter.newDOM();
 	Element userInfoMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
-	Element userInfoRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_SECURITY, "GetUserInformation", userContext);
+	Element userInfoRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, "GetUserInformation", userContext);
 	userInfoMessage.appendChild(userInfoRequest);
 
 	Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
 	userInfoRequest.appendChild(paramList);
-
-	Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
-	param.setAttribute(GSXML.NAME_ATT, GSXML.USERNAME_ATT);
-	param.setAttribute(GSXML.VALUE_ATT, request.getUserPrincipal().getName());
-	paramList.appendChild(param);
-
-	Element userInformationResponse = (Element) GSXML.getChildByTagName(this.recept.process(userInfoMessage), GSXML.RESPONSE_ELEM);
-	Element responseParamList = (Element) GSXML.getChildByTagName(userInformationResponse, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
-	if (responseParamList == null)
-	  {
-	    logger.error("Can't get the groups for user " + request.getUserPrincipal().getName());
-	  }
-	else
-	  {
-	    HashMap<String, Serializable> responseParams = GSXML.extractParams(responseParamList, true);
-	    String groups = (String) responseParams.get(GSXML.GROUPS_ATT);
-	    String editEnabled = (String) responseParams.get("editEnabled");
-
-	    userInformation.setAttribute(GSXML.GROUPS_ATT, groups);
-	    userInformation.setAttribute(GSXML.EDIT_ENABLED_ATT, (editEnabled != null) ? editEnabled : "false");
-	    xml_request.appendChild(userInformation);
-	  }
-      }
-
-    //If we are in a collection-related page then make sure this user is allowed to access it
-    if (collection != null && !collection.equals(""))
-      {
-	logger.error("kk in a coll"+collection);
+	paramList.appendChild(GSXML.createParameter(msg_doc, GSXML.USERNAME_ATT, user_name));
+
+	Element userInfoResponseMessage = (Element) this.recept.process(userInfoMessage);
+	Element userInfoResponse = (Element) GSXML.getChildByTagName(userInfoResponseMessage, GSXML.RESPONSE_ELEM);
+	Element respParamList = (Element) GSXML.getChildByTagName(userInfoResponse, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
+
+	if (respParamList != null)
+	{
+	    HashMap<String, Serializable> params = GSXML.extractParams(respParamList, false);
+	    String groups = (String) params.get("groups");
+	    String editEnabled = (String) params.get("editEnabled");
+	    userContext.setGroups(groups.split(","));
+	    userContext.setEditEnabled((editEnabled != null) ? editEnabled : "false");
+	}
+    }
+
+    private void appendUserInformation(Element xml_request, UserContext userContext)
+    {
+	Element userInformation = xml_request.getOwnerDocument().createElement(GSXML.USER_INFORMATION_ELEM);
+	userInformation.setAttribute(GSXML.USERNAME_ATT, userContext.getUsername());
+
+
+	userInformation.setAttribute(GSXML.GROUPS_ATT, userContext.getGroupsString());
+	userInformation.setAttribute(GSXML.EDIT_ENABLED_ATT, userContext.getEditEnabled());
+	xml_request.appendChild(userInformation);
+    }
+
+
+    private boolean runCollectionSecurityCheck(HttpServletRequest request, UserContext userContext, PrintWriter out, String baseURL, String collection, String document, String lang, String output) {
+    	logger.error("kk in a coll"+collection);
 	//Get the security info for this collection
 	Document msg_doc = XMLConverter.newDOM();
@@ -1024,87 +1047,57 @@
 	securityMessage.appendChild(securityRequest);
 	if (document != null && !document.equals(""))
-	  {
+	{
 	    securityRequest.setAttribute(GSXML.NODE_OID, document);
-	  }
+	}
 
 	Element securityResponse = (Element) GSXML.getChildByTagName(this.recept.process(securityMessage), GSXML.RESPONSE_ELEM);
 	if (securityResponse == null)
-	  {
+	{
 	    return false;
-	  }
-
+	}
+	
 	ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
 
 	//If guests are not allowed to access this page then check to see if the user is in a group that is allowed to access the page
 	if (!groups.contains(""))
-	  {
+	{
 	    boolean found = false;
 	    for (String group : groups)
-	      {
+	    {
 		if (request.isUserInRole(group))
-		  {
+		{
 		    found = true;
 		    break;
-		  }
-	      }
+		}
+	    }
 
 	    //The current user is not allowed to access the page so produce a login page
 	    if (!found)
-	      {
+	    {
 		logger.error("kk current user not allowed to access");
-		Document loginPageDoc = XMLConverter.newDOM();
-		Element loginPageMessage = loginPageDoc.createElement(GSXML.MESSAGE_ELEM);
-		Element loginPageRequest = GSXML.createBasicRequest(loginPageDoc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
-		loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
-		loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
-		loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
-		loginPageRequest.setAttribute(GSXML.BASE_URL, baseURL);
-		loginPageMessage.appendChild(loginPageRequest);
-
-		Element paramList = loginPageDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
-		loginPageRequest.appendChild(paramList);
-
-		Element messageParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
-		messageParam.setAttribute(GSXML.NAME_ATT, LOGIN_MESSAGE_PARAM);
+		String error_message = "";
 		if (request.getAuthType() == null)
-		  {
-		    messageParam.setAttribute(GSXML.VALUE_ATT, getTextString("auth.error.login", lang));
-		  }
+		{
+		    error_message = getTextString("auth.error.login", lang);
+		}
 		else
-		  {
-		    messageParam.setAttribute(GSXML.VALUE_ATT, getTextString("auth.error.incorrect_login", lang));
-		  }
-		paramList.appendChild(messageParam);
-
-		Element urlParam = loginPageDoc.createElement(GSXML.PARAM_ELEM);
-		urlParam.setAttribute(GSXML.NAME_ATT, REDIRECT_URL_PARAM);
+		{
+		    error_message = getTextString("auth.error.incorrect_login", lang);
+		}
+		String query_string = "";
 		if (request.getQueryString() != null && request.getQueryString().length() > 0)
-		  {
-		    urlParam.setAttribute(GSXML.VALUE_ATT, request.getRequestURL() + "?" + request.getQueryString().replace("&", "&amp;"));
-		  }
+		{
+		    query_string = request.getRequestURL() + "?" + request.getQueryString().replace("&", "&amp;");
+		}
 		else
-		  {
-		    urlParam.setAttribute(GSXML.VALUE_ATT, request.getRequestURL().toString());
-		  }
-		paramList.appendChild(urlParam);
-
-		// for debugging purposes, eg adding o=xml to the url
-		String output_p = getFirstParam(GSParams.OUTPUT, queryMap);
-		if (output_p !=null && !output_p.equals("")) {
-		  loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, output_p);
+		{
+		    query_string = request.getRequestURL().toString();
 		}
-					  
-		logger.error("login page request=");
-		logger.error(XMLConverter.getPrettyString(loginPageMessage));
-		Node loginPageResponse = this.recept.process(loginPageMessage);
-		out.println(XMLConverter.getPrettyString(loginPageResponse));
-
+		generateLoginPage(query_string, error_message, userContext, out, baseURL, output);
 		return false;
-	      }
-	  }
-      }
-    logger.error("kk reurned true");
-    return true;
-  }
+	    }
+	}
+	return true;
+    }
 
   private String getTextString(String key, String lang) {
