FileDocCategorySizeDatePackage
MovieClient.javaAPI DocExample3520Sun Jul 07 09:56:38 BST 2002None

MovieClient.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;

// Connection data binding classes
import javajaxb.generated.config.*;

// Arguments utility class
import javajaxb.util.Arguments;

// Jason Hunter's HttpMessage class
import com.oreilly.servlet.HttpMessage;

public class MovieClient {

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage:\n java javajaxb.MovieClient \n" +
                "    config=[XML configuration file] \n" +
                "    action=[list | addMovie | addActor] \n" +
                "    title=<movie title> \n" +
                "    director=<movie director> \n" +
                "    actor=<actor name> \n" +
                "    headliner=[true | false]");
            return;
        }
        
        Arguments arguments = new Arguments(args);

        try {
            File configFile = new File(arguments.getValue("config"));
            FileInputStream inputStream = 
                new FileInputStream(configFile);

            // Unmarshal the connection information
            Connection connection = Connection.unmarshal(inputStream);

            // Determine the data needed
            Host host = connection.getHost();
            Url configURL = connection.getUrl();
            String filename = new StringBuffer("/")
                .append(configURL.getContext())
                .append("/")
                .append(configURL.getServletPrefix())
                .append("/")
                .append(configURL.getServletName())
                .toString();

            // Connect to the servlet
            URL url = new URL("http", 
                              host.getHostname(),
                              Integer.parseInt(host.getPort()),
                              filename);
            HttpMessage msg = new HttpMessage(url);

            // Indicate the action desired
            Properties props = new Properties();
            String action = arguments.getValue("action");
            props.put("action", action);
            
            // Add any other required parameters
            if (action.equalsIgnoreCase("addMovie")) {
                String title = arguments.getValue("title");
                String director = arguments.getValue("director");
                
                props.put("title", title);
                props.put("director", director);
            } else if (action.equalsIgnoreCase("addActor")) {
                String title = arguments.getValue("title");
                String actor = arguments.getValue("actor");
                String headliner = arguments.getValue("headliner");
                
                props.put("title", title);
                props.put("actor", actor);
                props.put("headliner", headliner);
            }

            // Get response
            InputStream in = msg.sendPostMessage(props);
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));

            // Output response to screen
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}