www.pudn.com > j2mewireless_examples.zip > ConnectionManager.java


/*
 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
 */

package examples.netclient;
import java.lang.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;

/**
 * Manages network connection.
 *
 * This class established an HTTP POST connection
 * to a server defined by baseurl.
 * It sets the following HTTP headers:
 * User-Agent: to CLDC and MIDP version strings
 * Accept-Language: to microedition.locale or
 *                  to "en-US" if that is null
 * Content-Length:  to the length of msg
 * Content-Type:    to text/plain
 * Authorization:   to "Basic" + the base 64 encoding of
 *                  user:password
 */
class ConnectionManager {
    private HttpConnection con;
    private InputStream is;
    private OutputStream os;
    private String ua;
    private final String baseurl =
        "http://127.0.0.1:8080/Book/netserver";
    private String msg; 
    private String user;
    private String password;

    /**
     * Set the message to send to the server
     */
    void setMsg(String s) {
        msg = s;
    }
    
    /**
     * Set the user name to use to authenticate to server
     */
    void setUser(String s) {
        user = s;
    }
    /**
     * Set the password to use to authenticate to server
     */
    void setPassword(String s) {
        password = s;
    }


    /**
     * Open a connection to the server
     */
    private void open() throws IOException {
        int status = -1;
        String url = baseurl;
        String auth = null;
        is = null;
        os = null;
        con = null;


        // Loop until we get a connection (in case of redirects)
        while (con == null) {
            con = (HttpConnection)Connector.open(url);
            con.setRequestMethod(HttpConnection.POST);
            con.setRequestProperty("User-Agent", ua);
            String locale =
                System.getProperty("microedition.locale");
            if (locale == null) {
                locale = "en-US";
            }
            con.setRequestProperty("Accept-Language", locale);
            con.setRequestProperty("Content-Length",
                                   "" + msg.length());
            con.setRequestProperty("Content-Type", "text/plain");
            con.setRequestProperty("Accept", "text/plain");
            if (user != null && password != null) {
                con.setRequestProperty("Authorization",
                                       "Basic " + 
                                       BasicAuth.encode(user,
                                                     password));
            }


            // Open connection and write message
            os = con.openOutputStream();
            os.write(msg.getBytes());
            os.close();
            os = null;

            // check status code
            status = con.getResponseCode();
            switch (status) {
            case HttpConnection.HTTP_OK:
                // Success!
                break;
            case HttpConnection.HTTP_TEMP_REDIRECT:
            case HttpConnection.HTTP_MOVED_TEMP:
            case HttpConnection.HTTP_MOVED_PERM:
                // Redirect: get the new location
                url = con.getHeaderField("location");
                os.close();
                os = null;
                con.close();
                con = null;
                break;
            default:
                // Error: throw exception
                os.close();
                con.close();
                throw new IOException("Response status not OK:"+
                                      status);
            }
        }
        is = con.openInputStream();
    }

    /**
     * Constructor
    * Set up HTTP User-Agent header string to be the
     * CLDC and MIDP version.
     */
    ConnectionManager() {
        ua = "Profile/" +
            System.getProperty("microedition.profiles") +
            " Configuration/" +
            System.getProperty("microedition.configuration");
    }

    /**
     * Process an HTTP connection request
     */
    byte[] Process() {
        byte[] data = null;

        try {
            open();
            int n = (int)con.getLength();
            if (n != 0) {
                data = new byte[n];
                int actual = is.read(data);
            }
        } catch (IOException ioe) {
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException ioe) {}
            return data;
        }
    }
}