001package edu.pdx.cs410J.airlineweb;
002
003import edu.pdx.cs410J.ParserException;
004
005import java.io.IOException;
006import java.io.PrintStream;
007import java.io.StringWriter;
008import java.util.Map;
009
010/**
011 * The main class that parses the command line and communicates with the
012 * Airline server using REST.
013 */
014public class Project5 {
015
016    public static final String MISSING_ARGS = "Missing command line arguments";
017
018    public static void main(String... args) {
019        String hostName = null;
020        String portString = null;
021        String word = null;
022        String definition = null;
023
024        for (String arg : args) {
025            if (hostName == null) {
026                hostName = arg;
027
028            } else if ( portString == null) {
029                portString = arg;
030
031            } else if (word == null) {
032                word = arg;
033
034            } else if (definition == null) {
035                definition = arg;
036
037            } else {
038                usage("Extraneous command line argument: " + arg);
039            }
040        }
041
042        if (hostName == null) {
043            usage( MISSING_ARGS );
044            return;
045
046        } else if ( portString == null) {
047            usage( "Missing port" );
048            return;
049        }
050
051        int port;
052        try {
053            port = Integer.parseInt( portString );
054
055        } catch (NumberFormatException ex) {
056            usage("Port \"" + portString + "\" must be an integer");
057            return;
058        }
059
060        AirlineRestClient client = new AirlineRestClient(hostName, port);
061
062        String message;
063        try {
064            if (word == null) {
065                // Print all word/definition pairs
066                Map<String, String> dictionary = client.getAllDictionaryEntries();
067                StringWriter sw = new StringWriter();
068                PrettyPrinter pretty = new PrettyPrinter(sw);
069                pretty.dump(dictionary);
070                message = sw.toString();
071
072            } else if (definition == null) {
073                // Print all dictionary entries
074                message = PrettyPrinter.formatDictionaryEntry(word, client.getDefinition(word));
075
076            } else {
077                // Post the word/definition pair
078                client.addDictionaryEntry(word, definition);
079                message = Messages.definedWordAs(word, definition);
080            }
081
082        } catch (IOException | ParserException ex ) {
083            error("While contacting server: " + ex.getMessage());
084            return;
085        }
086
087        System.out.println(message);
088    }
089
090    private static void error( String message )
091    {
092        PrintStream err = System.err;
093        err.println("** " + message);
094    }
095
096    /**
097     * Prints usage information for this program and exits
098     * @param message An error message to print
099     */
100    private static void usage( String message )
101    {
102        PrintStream err = System.err;
103        err.println("** " + message);
104        err.println();
105        err.println("usage: java Project5 host port [word] [definition]");
106        err.println("  host         Host of web server");
107        err.println("  port         Port of web server");
108        err.println("  word         Word in dictionary");
109        err.println("  definition   Definition of word");
110        err.println();
111        err.println("This simple program posts words and their definitions");
112        err.println("to the server.");
113        err.println("If no definition is specified, then the word's definition");
114        err.println("is printed.");
115        err.println("If no word is specified, all dictionary entries are printed");
116        err.println();
117    }
118}