Package org.hindusingles.thf.server

Source Code of org.hindusingles.thf.server.InsultServlet

package org.hindusingles.thf.server;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlCenter;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

@SuppressWarnings("serial")
public class InsultServlet extends HttpServlet
{
  static WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
 
  static
  {
    client.setCssEnabled(false);
    client.setJavaScriptEnabled(false);
  }
 
  private static Logger logger = Logger.getLogger("org.hindusingles.thf.server.InsultServlet");
 
  private static void logIn( )
  {
    try
    {
      String userName = null;
      String password = null;
     
      HtmlForm form = client.<HtmlPage>getPage("http://www.thehaloforum.com").getForms( ).get(0);
     
      form.<HtmlTextInput>getInputByName("vb_login_username").setText(userName);
      form.<HtmlPasswordInput>getInputByName("vb_login_password").setText(password);
      form.<HtmlSubmitInput>getInputByValue("Log in").<HtmlPage>click( );
    }
    catch (Exception e)
    {
      logger.log
      (
        Level.WARNING,
        "Login failed.",
        e
      );
    }
  }
 
  /*static
  {
    logIn( );
  }*/
 
  @SuppressWarnings("unused")
  private static void post(int threadID, String message) throws Exception
  {
    if (client.getPage("http://www.thehaloforum.com/login.php").getUrl( ).toString( ) != "http://www.thehaloforum.com/index.php")
    {
      client.closeAllWindows( );
      logIn( );
    }
   
    HtmlPage page;
    HtmlForm form;
    HtmlInput submit;
   
    page =
    (
      client.<HtmlPage>getPage
      (
        String.format
        (
          "http://www.thehaloforum.com/newreply.php?t=%d",
          threadID
        )
      )
    );
   
    form = page.getFormByName("vbform");
   
    form.getTextAreaByName("message").setText(message);
   
    submit = form.getInputByName("sbutton");
   
    form.getSelectByName("emailupdate").getOptionByValue("9999").setSelected(true);
   
    submit.click( );
  }
 
  @SuppressWarnings("unused")
  private static String getInsult( ) throws Exception
  {
    String insult =
    (
      client.<HtmlPage>getPage("http://www.insultgenerator.org")
        .getBody( ).getHtmlElementsByTagName(HtmlCenter.TAG_NAME).get(0)
          .<HtmlTable>getHtmlElementsByTagName(HtmlTable.TAG_NAME).get(0)
            .getCellAt(0, 0).asText( )
    );
   
    if (insult == null || insult.isEmpty( ) || insult.startsWith("Error"))
    {
      throw new RuntimeException("Error getting insult.");
    }
    else
    {
      return insult;
    }
  }
 
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
  {
    try
    {
      response.setContentType("text/plain");
      response.getWriter( ).println("Getting and posting insults.");
     
      //post(208561, getInsult( ));
    }
    catch (Exception e)
    {
      logger.log
      (
        Level.WARNING,
        "Couldn't post insult.",
        e
      );
    }
  }
}
TOP

Related Classes of org.hindusingles.thf.server.InsultServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.