package edu.pdx.cs410J.phonebillweb; import edu.pdx.cs410J.web.HttpRequestHelper; import java.io.IOException; import java.io.PrintStream; import java.net.HttpURLConnection; /** * The main class that parses the command line and communicates with the * Phone Bill 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; } PhoneBillRestClient client = new PhoneBillRestClient(hostName, port); HttpRequestHelper.Response response; try { if (key == null) { // Print all key/value pairs response = client.getAllKeysAndValues(); } else if (value == null) { // Print all values of key response = client.getValues(key); } else { // Post the key/value pair response = client.addKeyValuePair(key, value); } checkResponseCode( HttpURLConnection.HTTP_OK, response); } catch ( IOException ex ) { error("While contacting server: " + ex); return; } System.out.println(response.getContent()); 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); } }