//
// HttpScan
//
// by Mark Nelson - 2001, 2002
//
// This Java console program scans a list of URLS stored in
// a database. Much of the setup is unique to my system, but
// the source listing contains a sample database you can use
// to test it on your system.
//
// Usage: java -Xbootclasspath/a:. HttpScan
//

import java.sql.*;
import java.net.*;
import java.io.*;

public class HttpScan
{
  public HttpScan( String database ) throws ClassNotFoundException,
                                            SQLException
  {
    try {
      Socket.setSocketImplFactory( new TimeoutSocketFactory( 30000 ) );
    }
    catch ( Exception e )
    {
      System.out.println( "Hijacking socket factory failed: " + e );
      System.exit( -1 );
    }
    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
    connection = DriverManager.getConnection( database, "", "");
    statement = connection.createStatement();
    String q1 = "SELECT * FROM Links WHERE Url LIKE 'http%'";
    results = statement.executeQuery( q1 );
    Statement tempStatement = connection.createStatement();
    String q2 = "SELECT COUNT(*) from Links WHERE Url LIKE 'http%'";
    ResultSet tempResultSet = tempStatement.executeQuery( q2 );
    if ( tempResultSet.next() )
      recordCount = tempResultSet.getInt( 1 );
    updateStatement = connection.createStatement();
  }
  void scan() throws SQLException
  {
    while ( results.next() )
    {
      int key = -1;
      String history = "";
      try {
        key = results.getInt( "Key" );
        String url = results.getString( "Url" );
        history = results.getString( "ScanHistory" );
        System.out.println( results.getRow() + "/" + recordCount +
                            ": Key=" + key + ", url: " + url );
        URL site = new URL( url );
        URLConnection connection = site.openConnection();
        System.out.println( "Opened connection" );
        if ( connection instanceof HttpURLConnection )
        {
          int responseCode =
               ( (HttpURLConnection) connection ).getResponseCode();
          System.out.println( "Response code = " + responseCode );
          if ( responseCode == 200 )
            Update( key, history, '-', null );
          else
            Update( key, history, 'F', "Response code " + responseCode);
        }
        else
        {
         Update(key, history, 'F', "Failed to achieve HTTP connection");
        }
      }
      catch (Exception e )
      {
        Update( key, history, 'F', "Exception: " + e );
      }
    }
  }
  void Update( int key, String history, char flag, String message )
  {
    if ( history == null )
      history = "";
    int length = history.length();
    if ( length >= 12 )
      history = history.substring( length - 11, length );
    history += flag;
    try
    {
      String updateString = "UPDATE Links SET ScanHistory = '" +
                            history + "', LastScanResult = ";
      if ( message == null )
        updateString += "NULL";
      else
        updateString += "'" + message + "'";
      if ( flag == '-' )
      {
       java.util.Date today = new java.util.Date();
       java.sql.Date sqlToday = new java.sql.Date(today.getTime());
       updateString += ", LastGoodScan = #" + sqlToday.toString() + "#";
      }
      updateString += " WHERE Key = " + key;
      System.out.println( "Update string = " + updateString );
      updateStatement.executeUpdate( updateString );
    }
    catch (Exception e )
    {
      System.out.println( "ERROR ACCESSING DATABASE: " + e );
      System.exit( 1 );
    }
  }
  public static void main(String[] args)
  {
    try
    {
      HttpScan scanner = new HttpScan( "jdbc:odbc:CompressionLinks" );
      scanner.scan();
    }
    catch ( Exception e )
    {
      System.out.println( "Failed to connect to database. Exception: "
                          + e );
    }
  }
  Connection connection;
  ResultSet results;
  Statement statement;
  Statement updateStatement;
  int recordCount;
}