Package mod.HttpGet

Source Code of mod.HttpGet.HttpGet$HttpGetArgs

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


// BGet

// ************ package ******************************************************
package mod.HttpGet;

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

import KFM.HTML.HtmlLoader2;
import KFM.Converter;

/** HttpGet
* Simple UI for HttpLoader2.
*
*/
public class HttpGet
{

    // ************************************************************
    // Constants
    // ************************************************************


    // ************************************************************
    // Inner classes
    // ************************************************************
    public class HttpGetArgs
    {
        public static final String // For *all* the following stuff.
        url = "url",
        timeout = "timeout";
    }

    // ************************************************************
    // Variables
    // ************************************************************



    /**
    * Reads the arguments passed to the main method.
    * It sets the Properties aProps so that it contains all the parameters.
    */
    public static Properties processArguments(String[] aArgs)
    {
        Properties tProps = new Properties();
        // store all arguments in properties
        for (int i = 0; i < aArgs.length; i++){
            int tIndex = aArgs[i].indexOf("=");
            if (tIndex > 0){
                String tKey = aArgs[i].substring(0, tIndex);
                String tValue = aArgs[i].substring(tIndex + 1, aArgs[i].length());
                tProps.put(tKey, tValue);
            }
        }

        //examine the mandatory params
        if (tProps.getProperty(HttpGetArgs.url) == null ||
            tProps.getProperty(HttpGetArgs.timeout) == null) {
            printUsageAndExit();
        }

        return tProps;
    }


    /**
    * Print usage information and exit. Called when used wrong syntax.
    */
    private static void printUsageAndExit()
    {
        System.out.println(
              "Wrong Args. Please try it again :)\n\n"
            + "Syntax: HttpGet url=<the URL> timeout=<the timeout>\n"
            + "<url>     Absolute URL that you want to load\n"
            + "<timeout> Timeout in milliseconds\n\n"
            + "The result is written to StdOut, possible error messages to StdErr."
        );

        // Terminate program.
        System.exit(-1);
    }



    /**
    * *************************************************************************
    */
    public static void main(String[] args)
    {
        Properties tProps = processArguments(args);

        int tTimeout = Integer.parseInt(tProps.getProperty(HttpGetArgs.timeout));
        String tUrl = tProps.getProperty(HttpGetArgs.url);

        HtmlLoader2 tHtmlLoader = new HtmlLoader2();
        int tStatusCode = -1;

        try {
            boolean tResult = tHtmlLoader.get(new URL(tUrl), tTimeout);
            tStatusCode = tHtmlLoader.getStatusCode();

            if(tResult) {
                System.out.println(tHtmlLoader.getContent());
            }

        } catch(IOException e) {
            System.out.println("HTMLLoader2 got an Exception.");
        }

        // report HTTP status code if something went wrong
        if (tStatusCode > 200) {
            System.err.println("Warning: Got HTTP status code " + tStatusCode);
        }

        // if OK, exit with status 0, else with 1.
        System.exit(tStatusCode == 200 ? 0 : 1);
    }

}
TOP

Related Classes of mod.HttpGet.HttpGet$HttpGetArgs

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.