IBM Research

MegObject Example

The MegObjectExamplePlugin demonstrates the use of WBI's new MegObject API. MegObjects enable Megs to pass certain kinds of objects rather than simply streamed binary or character data. This ability can be useful, for example, if all your Megs can work on the same Document Object Model (DOM), as it eliminates the need for continual parsing and reparsing of the stream. MegObjects work very much like like MegStreams: On the input side, you get a MegObject from the RequestEvent (just like you'd read from a MegInputStream or MegReader), and on the output side you put your MegObject on the RequestEvent (just like you'd write to a MegOutputStream or MegReader). Moreover, streams and objects can work together: If one Meg puts a MegObject, but the next requires stream data, WBI will handle the conversion from object to streamed data (however, translating the other way, from streamed data to object is the responsibility of the Meg author).

Try It Out

  1. Setup the Plugin
    • Start WBI. Setup your web browser to use WBI as a proxy.
    • Register the MegObjectExamplePlugin. At the WBI console, type (on one line)
            com/ibm/wbi/ examples/megobject/mobtest.reg
    • Check to see whether the plugin is registered and enabled. Go to the WBI Setup page. The MegObjectExample plugin should be listed in the table with a checkmark next to its name. If the plugin is not listed, try registering it again. If the checkmark is not there, click on the box to the left of the plugin name.
    • Open another browser window. Use that window to try out the plugin, and use this window to display the documentation. (To open another window using Microsoft Internet Explorer, go to File -> New -> Window. To open a window using Netscape Navigator, go to File -> New -> Navigator Window.)
  2. Visit a Web Page
    • Access the WBI Homepage. The Megs of the plugin will print some messages to the console, indicating the use of the MegObject concept.
  3. Having Trouble?

What It Does

The MegObjectExamplePlugin sets up four Megs that demonstrate how to use MegObjects. The Megs will print messages such as "MegObject created from MegInputStream" to indicate what they are doing with the document data.

How It Works

The MegObjectExamplePlugin does not change the contents of the retrieved document, but it does change the form of the data as it runs through a series of Editors.

MEG Model

The following diagram shows how the MegObjectExamplePlugin processes a response from a web server: the four HttpEditors alternatingly read and write binary ("b") and object ("o") data. The HttpEditors marked with "BE" work exclusively on binary (or character) data, while the HttpEditors marked with "OE" use MegObjects.

WBI processing path

Implementation Details

To use a MegObject, you must create a class implements the MegObject interface. To implement a MegObject, methods for transforming the object into streamed data, and for copying or cloning the object must be written. In this case, ExampleMegObject implements MegObject, simply reading data from the input stream and saving it in a StringBuffer. In this case, the getInputStream() method is implemented simply by wrapping a ByteArrayInputStream around the bytes of the characters in the StringBuffer.

TestEditor1 tries to retrieve a MegObject from the RequestEvent passed to the handleRequest() method. Specifically, it first tests whether the MegObject is of the correct type using the RequestEvent.getMegObjectType() method. If it is, then it is passed along to the next Meg in the chain using RequestEvent.putMegObject(). If there is no MegObject or if the object is not of the proper type (i.e., its class is not ExampleMegObject, this editor creates an ExampleMegOject and passes it along.

TestEditor2 simply retrieves the MegInputStream and passes the data from it to the MegOutputStream. This demonstrates that even when a MegObject has been passed from the previous Meg in the chain, WBI will automatically use the MegObject.getInputStream() method to convert the object back into streamed data. In this way, the use of objects and streams can be mixed seamlessly from the programmers point of view. Some key WBI classes that were used:
HttpEditor extended by TestEditor1 and TestEditor2
HttpPlugin extended by MegObjectExamplePlugin
RequestEvent used by TestEditor1 and TestEditor2
MegObject implemented by ExampleMegObject

Known Problems

There are no known problems with this example plugin. If you should happen to experience any, please inform us!).

Source Files

Contains the code necessary to register the plugin.
Contains the class definitions for ExampleMegObject, TestEditor1, and TestEditor2.