import java.io.*;
import java.net.URL;
import java.util.*;

import javax.security.auth.login.LoginException;
import javax.servlet.*;
import javax.servlet.http.*;
import net.mar.RetrieverManager;
import net.mar.RetrieverMapper;

import org.apache.log4j.Logger;
import org.apache.xpath.operations.Bool;
import org.astrogrid.community.common.exception.CommunityIdentifierException;
import org.astrogrid.community.common.exception.CommunitySecurityException;
import org.astrogrid.community.common.exception.CommunityServiceException;
import org.astrogrid.community.common.ivorn.CommunityAccountIvornFactory;
import org.astrogrid.community.common.security.data.SecurityToken;
import org.astrogrid.community.resolver.*;
import org.astrogrid.community.resolver.exception.CommunityResolverException;
import org.astrogrid.common.*;
import org.astrogrid.config.Config;
import org.astrogrid.config.SimpleConfig;
import org.astrogrid.registry.RegistryException;
import org.astrogrid.store.Ivorn;


public class HelloWorld extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    String user = req.getParameter("user");
    String community = req.getParameter("community");
    String pass = req.getParameter("pass");
    Ivorn ivorn = null;
    Logger logger=Logger.getLogger("wsa.simple");
    logger.debug("here");
    Boolean boo=new Boolean(true);
   
    
    try {
        ivorn = CommunityAccountIvornFactory.createIvorn(community, user);
    } catch (CommunityIdentifierException e1) {
    //    log.error("Unable to create ivorn", e1);
      //?? throw new LoginException("Invalid user name or community - unable to create an Ivorn from those parameters", e1);
    }
    final String name = ivorn.toString();
    out.println(name+" xyz");
    String endPoint = SimpleConfig.getProperty("org.astrogrid.registry.query.endpoint");
    out.println(endPoint);
    URL endPointURL = new URL(endPoint);
    HttpSession session = req.getSession();
    out.println(session.getAttribute("login")+" abc");
    session.setAttribute("login", "true");
    final CommunityPasswordResolver
    passwordResolver = new CommunityPasswordResolver(endPointURL);    
    SecurityToken token=null;
 
    try {
        token = passwordResolver.checkPassword(name, pass);
 
    } catch (CommunityServiceException e) {
        out.println("cservicee");
    } catch (CommunitySecurityException e) {
        out.println("csercuritye");
    } catch (CommunityIdentifierException e) {
        out.println("cidente");
    } catch (CommunityResolverException e) {
        out.println("cresolvere "+e);
        e.printStackTrace(out);
    } catch (RegistryException e) {
        out.println("crege");
       // log.error("RegistryException from security delegate",e);
      //  throw new LoginException("RegistryException from security delegate",e);
    }
    if (token != null){
    out.println(token.toString());
    }
    if (name != "abx"){
        return;
        }
    
    // Get the current session object, create one if necessary
    

    // Increment the hit count for this page. The value is saved
    // in this client's session under the name "tracker.count".

    session.setAttribute("login", "true");
String ID  = req.getParameter("id");
String end = "";
if (req.getParameter("end") != null){
end  = req.getParameter("end");
}
RetrieverMapper retMapp = RetrieverMapper.INSTANCE;
RetrieverManager retMgr = RetrieverManager.getInstance();
if (end.equals("yes"))
{ retMapp.setQueryTerminated(ID);
}
out.println("roes "+retMapp.getNumberRetriever()+"<p>");
out.println("roes "+retMapp.rowsSoFar(ID)+"<p>");
out.println(retMgr.isRunning(ID));
    out.println("Logged in");
session.setMaxInactiveInterval(3600);
out.println("<p> ID"+session.getId()+"<p>");
out.println("<p> timeout"+session.getMaxInactiveInterval()+"<p>");
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    // Data received by an HTTP POST is handled the same way
    // as data sent with a HTTP GET request... simply pass on to above...
    doGet(req, res);
  }; // END of doPost()

}
