Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Handling Varied Content in J2ME Applications

 
By Qusay H. Mahmoud , March 3, 2005  

Imagine a mobile application that can be registered to handle a MIME type, then present content of that type, and you've grasped the purpose of the Content Handler API (CHAPI): enabling applications based on the Java 2 Platform, Micro Edition (J2ME) to handle a lavish diversity of multimedia and web content. A key goal is the possibility of integrating microbrowsers with applications seamlessly.

Developed in the Java Community Process (JCP) as JSR 211, CHAPI defines an optional package of APIs and an execution model that enables applications to invoke J2ME and non-Java applications by URL, MIME type, or content handler ID. This tech tip introduces you to the Content Handler API and hints at some of the many uses to come.

The Content Handler API (JSR 211)

A content handler is any application that registers itself to handle some kind of content, responds to requests for such content, and serves it up to the user. The Content Handler API is an optional package, designed to work with version 2.0 of the Mobile Information Device Profile (MIDP 2.0), the Personal Basis Profile (PBP), and other compatible profiles. The JSR 211 specification defines functions to:

  • Register a content handler
  • Invoke a content handler and get a response
  • Query registrations
  • Respond to an invocation

A link to desired content might reach the user's device in an SMS message, a browser, or even from another application, at which point the device's software manager would dispatch the matching application.

Sample Scenarios

Perhaps the most interesting aspect of the Content Handler API is its goal of seamless integration between the device's browser and the applications. The browser is crucial to this optional package, as it can be used to invoke other content handlers.

A few of the many operating scenarios we can expect include:

  • Distributing new game levels: A user receives an email message containing a link to new levels of a favorite game, and clicks on the link. The game application downloads the new levels.
  • Remote device activation: A user who has just bought a new device turns it on, and receives an SMS message with a link for activation. When the link is selected, a local application collaborates with the provider's server to activate the new device.
  • Receiving images – and viewers to display them: A user receives an SMS message with a link to a photograph, and selects it. If the device already has a viewer for the image's type, it invokes that viewer to display the photo. If it doesn't, it contacts a provisioning server that offers a suitable viewer, downloads it, and uses it to display the photo.
Architecture

JSR 211 does not dictate CHAPI's position in a device's software architecture, but for reference and discussion it does provide a sample architecture that will look very familiar to users of J2ME optional packages. Figure 1, from the CHAPI specification, depicts the relationships between the Content Handler API, configurations and profiles, and other optional packages, and is suggestive of typical interface and behavior.

 
Figure 1: CHAPI's Place in the Reference Architecture
Figure 1: CHAPI's Place in the Reference Architecture
Contents of the Optional Package

The javax.microedition.content package comprises four interfaces and four classes, including one exception class:

Interface Description
ContentHandler Provides the details of a registration and specifies how to handle requests to an application
ContentHandlerServer Processes requests to handle content
RequestListener Notifies the content handler of pending requests
ResponseListener Notifies the content handler of pending responses

Class Description
ActionNameMap Maps actions to action names
Invocation Provides methods to retrieve the parameters passed to and returned from a content handler, and for selecting a content handler to invoke
Registry Provides methods to register, unregister, invoke, and query content handlers
ContentHandlerException Reports content-handling errors
Registration of Content Handlers

Registration should occur when the application is installed. You define registration behavior by setting attributes in the Java Application Descriptor (JAD) file. In this sample JAD file the attributes in bold text support content handlers:

MIDlet-1: Image Viewer,, example.imageviewer.ImageViewer
MicroEdition-Handler-1: example.imageviewer.ImageViewer, image/png, .png,open, en-US
MicroEdition-Handler-1-en-US: Open
MicroEdition-Handler-1-ID: com.sun.example.imageviewer
MicroEdition-Handler-1-Access: com.sun.example
MicroEdition-Handler-2: example.Browser, text/html, .html, open, en-US
MicroEdition-Handler-2-en-US: Open
MIDlet-Permissions: javax.microedition.io.Connector.http,javax.microedition.content.ContentHandler
MIDlet-Jar-Size: 2751
MIDlet-Jar-URL: imageviewer.jar
MIDlet-Name: Image Viewer
MIDlet-Vendor: Sun Microsystems, Inc.
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-2.0

At this writing, CHAPI has no reference implementation, but if you'd like to see what's involved in using the Content Handler API, look into Appendix A of the JSR 211 specification. It furnishes an example content handler for displaying images that can be found.

For More Information
About the Author
Qusay H. Mahmoud provides Java consulting and training services. He has published dozens of articles on Java, and is the author of Distributed Programming with Java (Manning Publications, 1999) and Learning Wireless Java (O'Reilly, 2002).