john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

ProxyTestCLI

// 2012-09-10 johnpfeiffer
// TODO: unit tests

import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;

public class ProxyTestCLI
{
    protected static final int MINIMUMPARAMETERS = 3;
    protected static final int MAXIMUMPARAMETERS = 3;
    public static final String CORRECTUSAGE = "Usage: java -jar ProxyTest-" + ProxyTest.CLASSVERSION
            + "http://domainname.com:80/index.htm proxyServer proxyServerPort";
    private static final int PROXYPORTLOCATION = 2;

    public static void main( String[] args ) throws Exception
    {
        long start = System.currentTimeMillis();
        try
        {
            if( argsAreInvalid( args ) )
            {
            }
        }catch( IllegalArgumentException e )
        {
            System.out.println( e + " " + CORRECTUSAGE );
            System.exit( 1 );
        }
        URL targetUrl = new URL( args[0] );
        String proxyServer = args[1];
        int proxyServerPort = Integer.parseInt( args[PROXYPORTLOCATION] );

        ProxyTest main = new ProxyTest.Builder().targetUrl( targetUrl ).proxyServer( proxyServer ).proxyServerPort( proxyServerPort ).build();
        System.out.println( "DEBUG: created ProxyTest-" + ProxyTest.CLASSVERSION + "  " + targetUrl + " " + proxyServer + ":" + proxyServerPort );
        try
        {
            main.Connect();
        }catch( ConnectException e )
        {
            System.err.println( e.getMessage() );
        }catch( IOException e )
        {
            System.err.println( e.getMessage() );

        }

        System.out.println( main.getResponse() );
        System.out.println( "DEBUG: finished in " + ( System.currentTimeMillis() - start ) + " ms" );
    }

    private static boolean argsAreInvalid( String args[] ) throws IllegalArgumentException
    {
        boolean areInvalid = true;
        if( args == null )
        {
            throw new IllegalArgumentException( "ERROR: cannot pass null arguments" );
        }
        if( args.length < MINIMUMPARAMETERS || args.length > MAXIMUMPARAMETERS )
        {
            throw new IllegalArgumentException( "ERROR: " + args.length + " Incorrect number of parameters, " );
        }
        try
        {
            Integer.parseInt( args[PROXYPORTLOCATION] );
            areInvalid = false;
        }catch( NumberFormatException e )
        {
            throw new IllegalArgumentException( "ERROR: targetPort and proxyServerPort must be an integer in range 0 to 65535" );
        }
        return ( areInvalid );
    }

} // end class

  • « ProxyTest
  • Password »

Published

Sep 10, 2012

Category

java-classes

~185 words

Tags

  • classes 92
  • java 252
  • proxytestcli 1