/* * Copyright (C) 2003 Peter Jonathan Klauser. All rights reserved. * * This software is published under the terms of the Jumpi Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE-jumpi.txt file. * */ package org.jumpi.impl.connector.midp10.http; import java.util.Enumeration; import java.util.Hashtable; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; import org.jumpi.Destination; import org.jumpi.Handle; import org.jumpi.Jumpi; import org.jumpi.Status; import org.jumpi.spi.boot.JumpiFactory; /** * Send and Receive via Jumpi's MIDP10 HttpConnector. Enter the URL of the * HttpJmsBridgeServlet, enter parameters and their values to pass to the servlet * each separated by a linefeed, and then wait for the response. The parameters * should include a 'jms.queue.name' parameter to indicate to the Servlet which JMS * Queue to use. A JMS request response application must service requests on that * Queue. */ public class HttpConnectorMidlet extends MIDlet implements CommandListener { /** URL for HTTP Connector */ String url = "http://localhost:8080/jumpi-jdk14-servlet/HttpJmsBridge"; /** string buffer for assembling HTTP requests. */ StringBuffer buffer = new StringBuffer(); /** text box for editing the Url */ TextBox urltext = new TextBox("Url", "", 4096, 0);; /** user interface text box for the request parameters. */ TextBox request = new TextBox("Request", "", 4096, 0);; /** user interface text box for the contents of the fetched URL. */ TextBox response = new TextBox("Response", "", 4096, 0);; /** user interface command for indicating Exit request. */ Command exitCommand = new Command("Exit", Command.EXIT, 2); /** user interface command for indicating Next request. */ Command nextCommand = new Command("Next", Command.OK, 1); /** user interface screen for displaying progress gauge. */ Form form; /** * Create the progress form and gauge. This program is not interactive, it * will exit when done. */ public HttpConnectorMidlet() { urltext.setTitle("Url"); urltext.setString(url); urltext.addCommand(exitCommand); urltext.addCommand(nextCommand); urltext.setCommandListener(this); request.setTitle("Request"); request.setString("jms.queue.name\nEcho\nkey1\nvalue1\nkey2\nvalue2\n"); request.addCommand(exitCommand); request.addCommand(nextCommand); request.setCommandListener(this); response.setTitle("Response"); response.setString(""); response.addCommand(exitCommand); response.addCommand(nextCommand); response.setCommandListener(this); Display.getDisplay(this).setCurrent(urltext); } /** * Start a thread to run the examples. */ public void startApp() { Display.getDisplay(this).setCurrent(urltext); } /** * Respond to commands, including exit * * @param c user interface command requested * @param s screen object initiating the request */ public void commandAction(Command c, Displayable s) { try { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if ( c == nextCommand && s == urltext) { Display.getDisplay(this).setCurrent(request); } else if ( c == nextCommand && s == request) { try { response.setString(""); Hashtable data = new Hashtable(); StringBuffer buf = new StringBuffer().append(request.getString()); while( buf.length() > 0 ) { String key = HttpConnector.getLineFromBuffer(buf); String value = HttpConnector.getLineFromBuffer(buf); if ( key != null && value != null ) { data.put(key, value); } } postViaJumpi(urltext.getString(), data); } catch ( Exception e ) { response.setString(e.getMessage()); } Display.getDisplay(this).setCurrent(response); } else if ( c == nextCommand && s == response) { Display.getDisplay(this).setCurrent(urltext); } } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } } /** * Pause, discontinue with the http tests */ public void pauseApp() {} /** * Destroy must cleanup everything. The thread is signaled to stop and no * result is produced. * * @param unconditional true if forced shutdown. */ public void destroyApp(boolean unconditional) {} void postViaJumpi( String url, Hashtable data ) throws Exception { Hashtable config = new Hashtable(); for( int i = 1; i < 256; i++ ) { String key = getAppProperty("jumpi-key"+i); String value = getAppProperty("jumpi-value"+i); if ( key != null && value != null ) { System.out.println( key +" = " + value ); config.put(key, value); } } Jumpi j = new JumpiFactory().getJumpi(config); // startup the Jumpi instance. j.startup(); Destination dest = j.getDestination(url); Handle sendHdl = j.send(dest, data); if ( sendHdl.getStatusCode() != Status.STATUS_SUCCESS ) { response.setString("SEND FAILED: " + sendHdl.getStatusMessage()); return; } Handle recvHdl = j.recv(dest); if ( recvHdl.getStatusCode() != Status.STATUS_SUCCESS ) { response.setString("SEND FAILED: " + sendHdl.getStatusMessage()); return; } Hashtable reply = (Hashtable)recvHdl.getMessage(); if ( reply == null ) { response.setString("Missing Response"); return; } Enumeration parameters = reply.keys(); StringBuffer temp = new StringBuffer(); while ( parameters.hasMoreElements() ) { String key = (String)parameters.nextElement(); String value = (String)reply.get(key); temp.append( key ).append("\n"); temp.append( value ).append("\n"); } response.setString(temp.toString()); } } /* * Version History * $Log: HttpConnectorMidlet.java,v $ * Revision 1.1 2003/06/24 22:17:44 klp * initial version introduced in jumpi 1.1.0 * * */