001package edu.pdx.cs410J.whitlock;
002
003import edu.pdx.cs410J.web.HttpRequestHelper;
004
005import java.io.IOException;
006import java.io.PrintStream;
007import java.net.HttpURLConnection;
008
009/**
010 * The main class that parses the command line and communicates with the
011 * Appointment Book server using REST.
012 */
013public class Project4 {
014
015    public static final String MISSING_ARGS = "Missing command line arguments";
016
017    public static void main(String... args) {
018        String hostName = null;
019        String portString = null;
020        String key = null;
021        String value = null;
022
023        for (String arg : args) {
024            if (hostName == null) {
025                hostName = arg;
026
027            } else if ( portString == null) {
028                portString = arg;
029
030            } else if (key == null) {
031                key = arg;
032
033            } else if (value == null) {
034                value = arg;
035
036            } else {
037                usage("Extraneous command line argument: " + arg);
038            }
039        }
040
041        if (hostName == null) {
042            usage( MISSING_ARGS );
043
044        } else if ( portString == null) {
045            usage( "Missing port" );
046        }
047
048        int port;
049        try {
050            port = Integer.parseInt( portString );
051            
052        } catch (NumberFormatException ex) {
053            usage("Port \"" + portString + "\" must be an integer");
054            return;
055        }
056
057        AppointmentBookRestClient client = new AppointmentBookRestClient(hostName, port);
058
059        HttpRequestHelper.Response response;
060        try {
061            if (key == null) {
062                // Print all key/value pairs
063                response = client.getAllKeysAndValues();
064
065            } else if (value == null) {
066                // Print all values of key
067                response = client.getValues(key);
068
069            } else {
070                // Post the key/value pair
071                response = client.addKeyValuePair(key, value);
072            }
073
074            checkResponseCode( HttpURLConnection.HTTP_OK, response);
075
076        } catch ( IOException ex ) {
077            error("While contacting server: " + ex);
078            return;
079        }
080
081        System.out.println(response.getContent());
082
083        System.exit(0);
084    }
085
086    /**
087     * Makes sure that the give response has the expected HTTP status code
088     * @param code The expected status code
089     * @param response The response from the server
090     */
091    private static void checkResponseCode( int code, HttpRequestHelper.Response response )
092    {
093        if (response.getCode() != code) {
094            error(String.format("Expected HTTP code %d, got code %d.\n\n%s", code,
095                                response.getCode(), response.getContent()));
096        }
097    }
098
099    private static void error( String message )
100    {
101        PrintStream err = System.err;
102        err.println("** " + message);
103
104        System.exit(1);
105    }
106
107    /**
108     * Prints usage information for this program and exits
109     * @param message An error message to print
110     */
111    private static void usage( String message )
112    {
113        PrintStream err = System.err;
114        err.println("** " + message);
115        err.println();
116        err.println("usage: java Project4 host port [key] [value]");
117        err.println("  host    Host of web server");
118        err.println("  port    Port of web server");
119        err.println("  key     Key to query");
120        err.println("  value   Value to add to server");
121        err.println();
122        err.println("This simple program posts key/value pairs to the server");
123        err.println("If no value is specified, then all values are printed");
124        err.println("If no key is specified, all key/value pairs are printed");
125        err.println();
126
127        System.exit(1);
128    }
129}