package edu.pdx.cs410J.apptbookweb;

import edu.pdx.cs410J.web.HttpRequestHelper;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

/**
 * The main class that parses the command line and communicates with the
 * Appointment Book server using REST.
 */
public class Project4 {

    public static final String MISSING_ARGS = "Missing command line arguments";

    public static void main(String... args) {
        String hostName = null;
        String portString = null;
        String key = null;
        String value = null;

        for (String arg : args) {
            if (hostName == null) {
                hostName = arg;

            } else if ( portString == null) {
                portString = arg;

            } else if (key == null) {
                key = arg;

            } else if (value == null) {
                value = arg;

            } else {
                usage("Extraneous command line argument: " + arg);
            }
        }

        if (hostName == null) {
            usage( MISSING_ARGS );

        } else if ( portString == null) {
            usage( "Missing port" );
        }

        int port;
        try {
            port = Integer.parseInt( portString );
            
        } catch (NumberFormatException ex) {
            usage("Port \"" + portString + "\" must be an integer");
            return;
        }

        AppointmentBookRestClient client = new AppointmentBookRestClient(hostName, port);

        String message;
        try {
            if (key == null) {
                // Print all key/value pairs
                Map<String, String> keysAndValues = client.getAllKeysAndValues();
                StringWriter sw = new StringWriter();
                Messages.formatKeyValueMap(new PrintWriter(sw, true), keysAndValues);
                message = sw.toString();

            } else if (value == null) {
                // Print all values of key
                message = Messages.formatKeyValuePair(key, client.getValue(key));

            } else {
                // Post the key/value pair
                client.addKeyValuePair(key, value);
                message = Messages.mappedKeyValue(key, value);
            }

        } catch ( IOException ex ) {
            error("While contacting server: " + ex);
            return;
        }

        System.out.println(message);

        System.exit(0);
    }

    /**
     * Makes sure that the give response has the expected HTTP status code
     * @param code The expected status code
     * @param response The response from the server
     */
    private static void checkResponseCode( int code, HttpRequestHelper.Response response )
    {
        if (response.getCode() != code) {
            error(String.format("Expected HTTP code %d, got code %d.\n\n%s", code,
                                response.getCode(), response.getContent()));
        }
    }

    private static void error( String message )
    {
        PrintStream err = System.err;
        err.println("** " + message);

        System.exit(1);
    }

    /**
     * Prints usage information for this program and exits
     * @param message An error message to print
     */
    private static void usage( String message )
    {
        PrintStream err = System.err;
        err.println("** " + message);
        err.println();
        err.println("usage: java Project4 host port [key] [value]");
        err.println("  host    Host of web server");
        err.println("  port    Port of web server");
        err.println("  key     Key to query");
        err.println("  value   Value to add to server");
        err.println();
        err.println("This simple program posts key/value pairs to the server");
        err.println("If no value is specified, then all values are printed");
        err.println("If no key is specified, all key/value pairs are printed");
        err.println();

        System.exit(1);
    }
}
@@ -34,54 +34,8 @@
 b;%0A%0A
-import edu.pdx.cs410J.web.HttpRequestHelper;%0A%0A
 impo
@@ -2416,511 +2416,8 @@
  %7D%0A%0A
-    /**%0A     * Makes sure that the give response has the expected HTTP status code%0A     * @param code The expected status code%0A     * @param response The response from the server%0A     */%0A    private static void checkResponseCode( int code, HttpRequestHelper.Response response )%0A    %7B%0A        if (response.getCode() != code) %7B%0A            error(String.format(%22Expected HTTP code %25d, got code %25d.%5Cn%5Cn%25s%22, code,%0A                                response.getCode(), response.getContent()));%0A        %7D%0A    %7D%0A%0A
     

Remove unused method.