XmlBlaster Logo

REQUIREMENT

client.java.applet

XmlBlaster Logo


Type NEW
Priority LOW
Status CLOSED
Topic XmlBlaster provides a tiny java client library for applets
Des
cription

If you develop applets you can connect with our tiny client java library xmlBlasterAppletLib.jar to xmlBlaster.

Features

  • Supports complete xmlBlaster access including real time asynchronous callbacks.
  • This client library has a size of only 23 kBytes and the download time over ISDN is about 3 seconds.
  • The communication protocol is based on a persistent http connection which is used to push back callback messages
  • The connection tunnels internet proxies and firewalls
  • Reports exceptions nicely back to the client

Overview

The following illustration shows a typical use case, an applet running behind internet proxies (like squid) and firewalls. It contacts the Web Server, for example Apache, which directs the call to a servlet engine with xmlBlasters communication servlet. The servlet looks at the applet request and opens a connection to xmlBlaster to delegate the request.

Note the two clients, the Browser client is an example which runs without an applet namely with pure Javascript callbacks. This example is described in requirement client.browser

Supported applet methdos are

Example
Java

You can try the persistent http online applet demo first:

  1. The online HelloWorld3 applet demo
  2. The online persistent HTTP system info applet demo
  3. The online persistent HTTP xmlBlaster script access applet demo
  4. Overview of online persistent HTTP examples.

The applet demo is currently only tested in mozilla 1.6 with a squid proxy. Other browsers may or may not work but support will follow.

Example
Java

This example shows an applet which invokes the full set of supported methods.


 1 package http.applet;
 2 
 3 import org.xmlBlaster.client.protocol.http.applet.I_CallbackRaw;
 4 import org.xmlBlaster.client.protocol.http.applet.I_XmlBlasterAccessRaw;
 5 import org.xmlBlaster.client.protocol.http.applet.XmlBlasterAccessRaw;
 6 import org.xmlBlaster.client.protocol.http.applet.Msg;
 7 import java.applet.Applet;
 8 import java.awt.Graphics;
 9 import java.awt.TextArea;
10 import java.awt.Color;
11 import java.util.Map;
12 
13 /**
14  * An example applet which connects to xmlBlaster using a persistent
15  * http tunnel for callbacks. 
16  *
17  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
18  * @see <a href="http://www.xmlblaster.org/xmlBlaster/demo/http/index.html">
19  *       http://www.xmlblaster.org/xmlBlaster/demo/http/index.html</a>
20  * @see org.xmlBlaster.util.qos.MsgQosData#toJXPath()
21  * @see org.xmlBlaster.util.key.MsgKeyData#toJXPath()
22  */
23 public class HelloWorld3 extends Applet implements I_CallbackRaw
24 {
25    I_XmlBlasterAccessRaw xb;
26    TextArea textArea;
27 
28    public void init(){
29       System.out.println("HelloWorld3: Applet.init() called");
30       try {
31          setBackground(Color.white);
32          setForeground(Color.black);
33          this.textArea = new TextArea("", 12, 60);
34          this.textArea.setBackground(Color.white);
35          this.textArea.setForeground(Color.black);
36          add(this.textArea);
37          repaint();
38 
39          this.xb = new XmlBlasterAccessRaw(this);
40          this.xb.connect(null, this);
41          print("Connected to xmlBlaster");
42 
43          Map subReturnQos = this.xb.subscribe("<key oid='HELLO'/>", "<qos/>");
44          print("Subscribed, id=" + subReturnQos.get("/qos/subscribe/@id"));
45 
46          Map pubReturnQos = this.xb.publish("<key oid='HELLO'/>",
47                                  "Hello World".getBytes(), "<qos/>");
48          print("Published 'HELLO', returned status is " +
49                      pubReturnQos.get("/qos/state/@id"));
50 
51          Map[] unSubReturnQos = this.xb.unSubscribe("<key oid='" +
52                    subReturnQos.get("/qos/subscribe/@id")+"'/>", "<qos/>");
53          print("UnSubscribed " + unSubReturnQos.length + " topics");
54 
55          Msg[] msgs = this.xb.get("<key oid='HELLO'/>", "<qos/>");
56          for (int i=0; i<msgs.length; i++) {
57             print("Get returned key=" + msgs[i].getKey().get("/key/@oid") +
58                                     " content=" + msgs[i].getContentStr());
59          }
60 
61          Map[] eraseReturnQos=this.xb.erase("<key oid='HELLO'/>","<qos/>");
62          print("Erase " + eraseReturnQos.length + " topics");
63       }
64       catch (Exception e) {
65          print("No connection to xmlBlaster: " + e.toString());
66          e.printStackTrace();
67          showStatus("HelloWorld3: No connection to xmlBlaster");
68       }
69    }
70  
71    private void print(String text) {
72       this.textArea.append("HelloWorld3: " + text + "\n");
73    }
74 
75    public void destroy(){
76       print("Applet destroy ...");
77       if (this.xb != null) {
78          this.xb.disconnect("<qos/>");
79          this.xb = null;
80          print("Disconnected from xmlBlaster");
81       }
82    }
83 
84    /**
85     * Here you receive the callback messages from xmlBlaster. 
86     */
87    public String update(String cbSessionId, Map updateKey, byte[] content,
88                         Map updateQos) throws Exception {
89       print("---- START update received -----");
90       print("key=" + updateKey.get("/key/@oid") + " state=" +
91             updateQos.get("/qos/state/@id"));
92       print("update received: content=" + new String(content));
93       print("---- END   update received -----");
94       return "<qos/>";
95    }
96 }

In the init() method we connect to xmlBlaster and subscribe on a message which we later publish. This message is received asynchronous in the update method.

When the applet is destroyed we disconnect from xmlBlaster.

Finally we need to embed the applet into a html page:

   
<html>
 <head>
 </head>
 <body>
   <h2>Hello World 3 Applet</h2>

   <applet code="http.applet.HelloWorld3.class"
       width="700" height="200"
       name="XmlBlasterAccess"
       archive="appletDemo.jar,xmlBlasterAppletLib.jar"
       MAYSCRIPT>
 
     <param name="deliveredParamKeys" 
            value="protocol,xmlBlaster/logLevels" />
     <param name="protocol" value="SOCKET" />
     <param name="xmlBlaster/logLevels" value="ERROR,WARN,INFO" />
 
   </applet>
 
 </body>
</html>
   

Specifying ConnectQos

You can specify the ConnectQos as the first parameter of the connect() method, here is an example for a persistent login session called eduardo/1. Each applet restart will connect to this same session and queued messages will be delivered when the applet comes to live again. Even on xmlBlaster server restart no persistent messages will be lost:


   try {
      xb = new XmlBlasterAccessRaw(applet);
      String connectQos =
            "<qos>" +
            "   <securityService type='htpasswd' version='1.0'>" +
            "     <user>eduardo</user>" +
            "     <passwd>secret</passwd>" +
            "   </securityService>" +
            "   <session name='eduardo/1' timeout='-1'/>" +
            "   <persistent>true</persistent>" +
            "</qos>";
      xb.connect(connectQos, applet); // registers applet.update() callback method
      ...
Example
Java

Quick start installation of the above applet

You can try it with tomcat locally (no apache is necessary, i had tomcat 3.2.4, 4.1.29, 5.0.14 and 5.5 to test it, others should work as well):

1. Create a war file:

  cd xmlBlaster

  build  -DTOMCAT_HOME=/opt/jakarta_tomcat   deploy_war

Set TOMCAT_HOME to point to your installation and the xmlBlaster.war will be automatically deployed to $TOMCAT_HOME/webapps/xmlBlaster.war

2. Start it (keep port 8080 free for tomcat)

  java org.xmlBlaster.Main -plugin/xmlrpc/port 8081   
  
  java  http.dhtml.systemInfo.SystemInfoPublisher

 cd $TOMCAT_HOME/bin
 startup.sh

3. Start your browser and use this URL:

http://localhost:8080/xmlBlaster/HelloWorld3.html
http://localhost:8080/xmlBlaster/SystemInfoApplet.html
Configure

This client applet works without a XML parser to have a very small memory (download) footprint. The returned key and QoS are in a Map, the keys follow the JXPath syntax.

The interface I_XmlBlasterAccessRaw has two useful methods: getHtmlProperties() to access the HTML applet PARAM tags and setLogListener() to redirect logging output to your application.

The servlet configuration, like logging and setting xmlBlaster-queue or connection parameters is done in xmlBlaster/demo/http/WEB-INF/web.xml.

Note for developers:
xmlBlaster/src/java/org/xmlBlaster/protocol/http/appletproxy The server side code which handles applet requests.
xmlBlaster/src/java/org/xmlBlaster/client/protocol/http/applet The tiny applet library to ease xmlBlaster access (used by your applets).
xmlBlaster/demo/http/applet Demo applets to get you started.

NOTE: Configuration parameters are specified on command line (-someValue 17) or in the xmlBlaster.properties file (someValue=17). See requirement "util.property" for details.
Columns named Impl tells you if the feature is implemented.
Columns named Hot tells you if the configuration is changeable in hot operation.

Todo

This beta release is considered stable, but we need to:

  • Port the connect() return XML string to a JXPath Map
  • Probably port the remaining URLEncode to Base64
  • Check the code POST versus GET
  • Verify with other browsers and proxies, currently only mozilla and firefox with squid is tested
  • Port to apache httpClient to have proxy support outside the browser (as native java and not as applet)

See API org.xmlBlaster.client.protocol.http.common.I_XmlBlasterAccessRaw
See REQ interface
See REQ client.browser
See http://www.xmlblaster.org/xmlBlaster/demo/http/index.html
See http://jakarta.apache.org/tomcat/
See http://www.xmlBlaster.org:8080/xmlBlaster/index.html
See http://www.xmlBlaster.org:8080/xmlBlaster/HelloWorld3.html
See http://www.xmlBlaster.org:8080/xmlBlaster/SystemInfoApplet.html

This page is generated from the requirement XML file xmlBlaster/doc/requirements/client.java.applet.xml

Back to overview