/*
 * @(#)Get.java
 *
 * Summary: simulates a browser posting a form to CGI via GET.
 *
 * Copyright: (c) 1998-2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.5+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  2.0 2009-02-20 - major refactoring. separate setParms and setPostParms. new send method. Post can have both types of parm.
 */
package com.mindprod.http;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * simulates a browser posting a form to CGI via GET.
 * <p/>
 * See com.mindprod.submitter for sample code to use this class.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 2.0 2009-02-20 - major refactoring. separate setParms and setPostParms. new send method. Post can have both types of parm.
 * @since 1998
 */
public final class Get extends Http
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * constructor
     */
    public Get()
        {
        }

    /**
     * Send a form full of data to the CGI host using GET.
     *
     * @param url      complete URL including get parms, pre-encoded.
     * @param encoding encoding of the byte stream result, usually UTF-8 or or ISO-8859-1.
     *
     * @return CGI host's response with headers and embedded length fields stripped.
     * @see com.mindprod.http.Get
     */
    @SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase", "WeakerAccess" } )
    public String send( URL url, String encoding )
        {
        try
            {
            // defaults
            responseCode = DEFAULT_RESPONSE_CODE;
            responseMessage = DEFAULT_RESPONSE_MESSAGE;

            // O P E N

            final HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection();
            urlc.setAllowUserInteraction( false );
            urlc.setDoInput( true );
            urlc.setDoOutput( false );// nothing beyond original request
            urlc.setUseCaches( false );
            urlc.setRequestMethod( "GET" );
            setStandardProperties( urlc );

            return processResponse( encoding, urlc );
            }
        catch ( ClassCastException e )
            {
            // was not an http: url
            return null;
            }
        catch ( IOException e )
            {
            return null;
            }
        }// end get

    /**
     * Send a form full of data to the CGI host using GET.
     * Must have previously specified the parms with setParms.
     *
     * @param host     host name of the website, Should be form:"mindprod.com", no lead http://.
     * @param port     -1 if default, 8081 for local echoserver.
     * @param action   action of form, page on website. Usually has a lead / and no trailing /.
     * @param encoding encoding of the byte stream result, usually UTF-8 or or ISO-8859-1.
     *
     * @return CGI host's response with headers and embedded length fields stripped.
     */
    @SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
    public String send( String host, int port, String action, String encoding )
        {
        try
            {
            // defaults
            responseCode = DEFAULT_RESPONSE_CODE;
            responseMessage = DEFAULT_RESPONSE_MESSAGE;

            // O P E N

            // URL will encode target and parms.
            URL url = new URI( "http",
                    null,
                    host,
                    port,
                    action,
                    null,
                    null ).toURL();
            url = new URL( url.toString() + getEncodedParms( encoding ) );
            return send( url, encoding );
            }
        catch ( URISyntaxException e )
            {
            // was not an http: url
            return null;
            }
        catch ( IOException e )
            {
            return null;
            }
        }// end get
    }