The Content Handler API and execution model allow an application to invoke registered J2ME and non-Java applications by URL, by content type, or by content handler ID. Content handlers can chain to other content handlers to respond to invocations. The classes of the API are shown below:

The functions of content handler classes are:

- {@link javax.microedition.content.Registry} - invokes, registers, unregisters and queries content handlers
- {@link javax.microedition.content.ContentHandler} - provides the details of a registration and to handle requests to an application
- {@link javax.microedition.content.Invocation} - contains the parameters passed to and returned from a content handler and used to select a content handler to invoke
- {@link javax.microedition.content.ContentHandlerServer} - processes requests to handle content
- {@link javax.microedition.content.RequestListener} - notifies the content handler of pending requests
- {@link javax.microedition.content.ResponseListener} - notifies the application of pending responses
- {@link javax.microedition.content.ActionNameMap} - maps actions to action names
- {@link javax.microedition.content.ContentHandlerException} - reports content handler errors

Applications use CHAPI in two ways, as an invoking application or as a content handler application. The invoking application is primarily concerned with the functions of the {@link javax.microedition.content.Registry Registry} and {@link javax.microedition.content.Invocation Invocation} classes. As a client, the application makes requests to display or act on some content. The application makes a request by supplying parameters that identifies the content by URL or the content type, or the ID of the content handler, and possibly an action to perform. If the application needs to know when a request is complete or to get a result it can require a response from the content handler. The application creates an `Invocation` instance and initializes it with the parameters. The application calls the `Registry.invoke` method to start the request. Only if the invoking application is expecting a result does it need to retrieve the response using `Registry.getResponse`. The application can register a listener with `Registry.setListener` to be notified when the response is available.

A more advanced client application may want to use the detailed information about content handlers stored in {@link javax.microedition.content.ContentHandler ContentHandler} objects including the types, suffixes, and actions, or information about the content handler application including its authorization, application name and version, and content handler ID. If the application allows the user to choose the action to be performed, it should use the handler's {@link javax.microedition.content.ActionNameMap}s to get locale appropriate action names.

As a server, the content handler application responds to invocation requests from clients. The content handler application uses the methods of {@link javax.microedition.content.ContentHandlerServer ContentHandlerServer} and {@link javax.microedition.content.Invocation}. New requests are retrieved from the `ContentHandlerServer.getRequest` method. The content handler can register a listener with `ContentHandlerServer.setListener` to be notified when new requests are available. The content handler uses the parameters in the `Invocation` to process the request. If a URL was supplied, the content can be retrieved with the `Invocation.open` method using the Generic Connection Framework. The content handler uses the URL, type, and action to determine how it should act on the content. When the content handler is done with each request it returns updated parameters and arguments and a status with the `ContentHandlerServer.finish` method.

A more advanced content handler application can both be a client and a server. Methods on the {@link javax.microedition.content.Registry Registry} are used for the advanced functions. New requests can be chained to previous requests with the `invoke` method. The content handler may restrict access and visibility to itself during registration. An detailed example is provided in Appendix A.

## Implementation Requirements

- The system property `microedition.chapi.version` can be used to determine whether CHAPI is implemented, and if it is, the version number. If this specification is implemented in a Java runtime environment, the property `microedition.chapi.version` MUST have the value `"1.0"` as returned from the `java.lang.System.getProperty` method.
- Support is required for the URL scheme "`http`", the `javax.microedition.io.HttpConnection` interface and corresponding protocol.
- The {@link javax.microedition.content.Invocation} class and invocation mechanism MUST support passing and returning arguments and data totaling at least 16384 bytes. Each character of a String is counted as two bytes. It must be able to pass and return at least 10 arguments in the arguments array.
- Content handler registrations that are successful MUST be preserved across soft and hard restarts of the device software including unexpected power interruptions.
- The security policy of the Java runtime environment MUST be used to enforce limitations on access to content using CHAPI. For MIDP, permissions and access control must be the same as though `Connector.open` were used to access the content.

## Installing Content Handlers

Content handlers may be installed on the device either before they are needed or when they are needed. The content handler API can be used to trigger the installation process to install the content handlers on the device. For at least one downloadable application packaging type supported by the device, the CHAPI implementation MUST provide and register a content handler to perform the installation. Typically, this includes `application/java-archive`.

An application can invoke CHAPI with the URL of an application package. The appropriate installation process for the device MUST be used to attempt to install the content handler. The installation MUST include all of the required security and user prompts as specified in the appropriate profile and download specification. If the installation process fails for any reason, including the user's decision to cancel the installation, then the Invocation will fail with the Invocation.CANCELLED status. The status response must be delivered to the invoking application, if required in the request.

## Interacting with the Application Life Cycle

Content handlers are applications and are subject to the same security, integrity, installation, upgrade, and other requirements as any other application in the applicable Java runtime environment. Authentication of applications and application classes MUST conform to the requirements of the applicable Java runtime environment.

Each Java runtime environment has its own application packaging and runtime requirements. Each content handler application MUST conform to these requirements. Each Java runtime specifies how applications are uniquely identified, for example, by a classname of the application class. For the MIDP Profile, a content handler MUST be packaged as a MIDlet suite and each content handler MUST implement the MIDlet life cycle. It is NOT necessary for the content handler to be registered as a user visible MIDlet with a `MIDlet-<n>` attribute. If the content handler MIDlet should be selectable by the user then the `MIDlet-<n>` attribute must be supplied.

Installation and removal of content handlers utilizes the normal mechanisms of the Java runtime environment. During installation, any content handlers MUST be registered using the attributes defined in the application packaging. Typically, they will be present in an application descriptor or JAR manifest. When a content handler application is removed, all of the static and dynamic registrations of the content handler application MUST be released.

## Handling Display Transitions - Foreground and Background

A goal of the Content Handler API is to promote a smooth or seamless user experience between the invoking application and content handler. Typically, if the invoking application has a user interface, one of its screens will be displayed to the user (that is, it will be in the foreground). If a user action causes an Invocation then the next screen the user sees should be the content handler for that invocation. If a foreground content handler finishes with an invocation that requires a response to the calling application that had the foreground immediately before the content handler was invoked, and there are no other requests buffered or being processed from the same calling application, then the foreground is returned to the calling application.

If there is a conflict between foreground transitions and external events or explicit user actions, then the external events or user actions should take precedence.

## Static Registration of Content Handlers

Content handler registration can, and generally should, be handled during the installation of the application. The following attributes can be included in the JAR manifest of the application. Each Java runtime environment may define their own application packaging extensions. For MIDP, the attributes can be included in the Java Application Descriptor (JAD). If the attributes are present, they MUST be used by the installation process to register the content handlers. If the application is being upgraded from a previous version then static registrations that are not replaced by new registrations are removed. See the Example of MIDlet Suite Java Application Descriptor section.

Each content handler is uniquely identified by an ID. Content handler and application IDs MUST NOT contain control characters (U+0000 thru U+001F) or spaces (U+00020). The ID for content handlers is supplied in the `MicroEdition-Handler-<n>-ID` attribute. If an ID is not supplied then a default ID MUST be generated by the implementation. The ID MUST NOT be equal to any other registered handler ID. Every other ID MUST NOT be a prefix of this ID. The ID MUST NOT be a prefix of any other registered ID. If the ID conflicts with another ID the installation MUST fail.

Each static content handler registration contains the following information. Fields are separated using Unicode comma ("," = U+002C). Whitespace is deleted from the beginning and end of each value where whitespace consists of sequences of one or more Unicode horizontal tab (HT = U+0009) or space (" " = U+0020) characters. Each registration consists of information from a `MicroEdition-Handler-<n>` attribute and optional attributes `MicroEdition-Handler-<n>-<locale>`, `MicroEdition-Handler-<n>-ID: <ID>`, and `MicroEdition-Handler-<n>-Access`. All of the attributes for a registration have the same value of `<n>`. The numeric value for `<n>` starts at 1 and MUST use consecutive ordinal numbers for additional entries. The attributes are: ``

    MicroEdition-Handler-<n>: <classname>, <type(s)>, <suffix(es)>, <action(s)>, <locale(s)>
    MicroEdition-Handler-<n>-<locale>: <localized action names>
    MicroEdition-Handler-<n>-ID: <ID>
    MicroEdition-Handler-<n>-Access: <ID(s)>

where:

- `MicroEdition-Handler-<n>` - Attribute for registrations. Multiple registrations can be provided in an application. The numeric value for `<n>` starts at 1 and MUST use consecutive ordinal numbers for additional entries. The first missing entry terminates the list. Any additional entries are ignored.

- `MicroEdition-Handler-<n>-<locale>` - Attribute to define the action names appropriate for the actions of the nth content handler for the indicated locale. The locale string should be properly formatted as defined in class {@link javax.microedition.content.ContentHandler}. If the corresponding `MicroEdition-Handler-<n>` attribute references this locale attribute then there MUST be an action name for each action in the corresponding `MicroEdition-Handler-<n>` attribute. If the number of action names does not match the number of actions, then the installation MUST fail. If there are multiple action names, separate them with a comma ("," = U+002C). Action names may contain embedded whitespace. Leading and trailing whitespace is deleted from each action. If a locale attribute is present but is not referenced by a `MicroEdition-Handler-<n>` attribute then it is not used for the registration.

- `MicroEdition-Handler-<n>-ID` - Attribute to declare the unique ID for this content handler. The content handler ID is set during installation and returned from the method {@link javax.microedition.content.ContentHandler#getID ContentHandler.getID}. If not provided, a unique value MUST be provided by the implementation.

- `MicroEdition-Handler-<n>-Access` - Attribute to control the access and visibility of the content handler. It contains a list of the IDs of content handlers or applications that are allowed access and visibility to the content handler. If omitted, all applications will be allowed access and visibility to the content handler. If there are multiple IDs, separate them with a space (" " = U+0020). IDs MUST NOT contain whitespace. Leading and trailing whitespace is deleted from each ID.

- `classname` - Application that is responsible for handling the content. Leading and trailing whitespace is deleted.

- `type(s)` - Content type(s). If there are multiple types, separate them with a space (" " = U+0020). Types MUST NOT contain a space. Leading and trailing whitespace is deleted from each type.

- `suffix(es)` - Zero or more suffix strings. If there are multiple suffixes, separate them with space (" " = U+0020). Suffixes MUST NOT contain a space. Leading and trailing whitespace is deleted from each suffix.

- `action(s)` - Zero or more actions. If there are multiple actions, separate them with a space (" " = U+0020). Actions MUST NOT contain a space. Leading and trailing whitespace is deleted from each action.

- `locale(s)` - Zero or more locales. If there are multiple locales, separate them with a space (" " = U+0020). Locales MUST NOT contain a space. Leading and trailing whitespace is deleted from each locale string. Each locale string is used to identify the `MicroEdition-Handler-<n>-<locale>` attribute that contains the action names.

- `ID` - Content handler or application ID. Leading and trailing whitespace is deleted.

### BNF syntax for parsing attribute values

The values of the attributes are parsed according to the BNF below. Whitespace (WSP) may be present around delimiters and is deleted from the beginning and end of each value.

>     ; Syntax of the MicroEdition-Handler-<n> attribute
>     handler-value = classname ["," types [ "," suffixes ["," actions ["," locales]]]]

>     classname =  value-chars

>     types =  *type / type WSP types
>     type = value-chars

>     suffixes = *suffix / suffix WSP suffixes
>     suffix = value-chars

>     actions =  *action / action WSP actions
>     action = value-chars

>     locales =  *locale / locale WSP locales
>     locale = value-chars

>     value-chars = 1*(<any Unicode character except CTLs or WSP or COMMA>)

>     ; Syntax of the MicroEdition-Handler-<n>-locale attribute
>     action-names =  action-name / (action-name "," action-names)
>     action-name = value-chars / (value-chars WSP action-name)

>     ; Syntax of the MicroEdition-Handler-<n>-ID attribute
>     id-value =  1*(value-chars / COMMA)

>     ; Syntax of the MicroEdition-Handler-<n>-Access attribute
>     access-values = id-value / (id-value WSP access-values)

>     ; Delimiters
>     HT = %x09        ; Horizontal tab
>     SP = %x20        ; Space
>     COMMA = %x2C     ; Comma
>     WSP = *(SP / HT) ; Whitespace
>     CTLs = *(%x00 - %x1F)

### Using the parsed values to register

The values parsed from the attributes are used to register the content handler. If there are multiple types, suffixes, actions, or locales, the order is significant; the values must appear in the same order when passed to the `Registry.register` method. Each locale string and the sequence of actions and action names in each `MicroEdition-Handler-<n>-<locale>` attribute are used to make a new {@link javax.microedition.content.ActionNameMap ActionNameMap}. The arrays of types, suffixes, actions, action name maps, ID, and access controls are used to {@link javax.microedition.content.Registry#register register} the classname as a content handler.

### Access Restrictions and Application IDs

Content Handlers can control whether they are visible to and can be invoked by other applications and content handlers. The ID of the invoking application is matched against the list of IDs that are allowed access to the handler.

Note that the invoking application or content handler provides its own ID. The veracity of the ID can be cross checked with the authority information that provided for content handlers and invoking applications. If there is no authority information then the ID cannot be trusted.

Each Java runtime environment MUST provide a mechanism to associate an ID with each application. If an ID is not provided, then a default value must be provided. For MIDP, the default value is defined below.

### Content Handlers and the Mobile Information Device Profile

For the MIDP Profile, the implementation MUST support download and installation of MIDlet suites that include content handlers. The implementation MUST provide and register content handler(s) to perform the installation for the type `text/vnd.sun.j2me.app-descriptor` and may support the type `application/java-archive` for JAR-only installation.

During the installation of a MIDlet suite, each content handler MUST be registered using the attributes defined in the Java Application Descriptor and/or in the Manifest. For untrusted application, attributes in the JAD override corresponding attributes in the Manifest. For trusted applications, according to the MIDP specification, if attributes appear in both the JAD and manifest then the corresponding attribute values MUST be equal.

Content handlers MUST be installable using the MIDP OTA installation mechanism. Any failure related to content handler registration attributes or incorrect or invalid information related to the MIDlet suite must cause the installation to fail. If the `MIDlet-Install-Notify` attribute defined by MIDP OTA is present, then an installation status report MUST be sent. The installation notifications are:

- `938 Content handler conflicts with other handlers`
  This CHAPI specific notification is sent if there is a conflict between the ID of any handler in the MIDlet suite being installed and handlers of existing MIDlet suites so the required content handler registrations cannot be performed, then the installation MUST fail.
- `939 Content handler install failed`
  This CHAPI specific notification is sent if any of the content handler specific attributes are missing or cannot be parsed correctly or have values that are not valid to register the content handler then the installation must fail. This includes at least the values for types, suffixes, IDs, and actions.
- `910 Application authorization failure`
  This MIDP OTA defined notification is sent if there are static registration attributes present but the permission `javax.microedition.content.ContentHandler` is not requested or not granted.
- `905 Attribute Mismatch`
  This MIDP OTA defined notification is sent if an attribute value in the application descriptor is not equal to the value of the corresponding attribute in the manifest.

If the content handler or application ID is not provided by the application then the implementation is required to provide a value. The default ID value is formed as the concatenation of the value of the `MIDlet-Vendor` attribute followed by a hyphen ("-" = U+002D) and the value of the `MIDlet-Name` attribute followed by a hyphen ("-" = U+002D) and the `classname`. All spaces (" " = U+0020) must be translated to underscore ("_" = U+005F).

The `MicroEdition-Handler-<n>-ID` attribute contains the content handler ID. If the MIDlet suite is trusted, then this attribute and value must be present in both the manifest and application descriptor and the values must be the equal or it must not appear at all. If not present in the application descriptor or manifest, the implementation MUST supply a default ID as defined above using the classname from the `MicroEdition-Handler-<n>` attribute.

MIDP applications that do not register as content handlers need an application ID to be allowed access to restricted content handlers. The `MIDlet-<n>-ID` attribute contains the application ID for the MIDlet registered with the `MIDlet-<n>` attribute. When the application has gained access to the Registry the ID is available using the {@link javax.microedition.content.Registry#getID Registry.getID} method. If the MIDlet suite is trusted, then this attribute and value must be present in both the manifest and application descriptor and the values must be the equal or it must not appear at all. If an ID is not provided, the implementation MUST supply a default ID as defined above using the `classname` in the `MIDlet-<n>` attribute.

The user friendly application name MUST be provided by {@link javax.microedition.content.ContentHandler#getAppName ContentHandler.getAppName}. If the content handler's classname is also registered as a MIDlet using a `MIDlet-<n>` attribute then the application name is the `name` (first) value of the attribute, otherwise, the application name is the value of the `MIDlet-Name` attribute.

Updates to content handlers are treated the same as updates to any MIDlet suite. The updated content handlers MUST be successfully registered. If there are any dynamic registrations, then they MUST be retained across the upgrade unless superceded by a new static registration for the same classname.

A trusted MIDlet suite MUST request and be granted the `javax.microedition.content.ContentHandler` permission to perform either static or dynamic registrations consistent with the device's security policy. The `Registry.register` method MUST throw a `java.lang.SecurityException` if the permission is not granted.

The permission, `javax.microedition.content.ContentHandler`, for registering content handlers SHOULD be included in the `auto-invocation` function group as defined in the MIDP Recommended Security Policy. The policy allows an untrusted application to register handlers with a user prompt each time (oneshot). For trusted applications, the user can also choose blanket and session interaction modes.

The MIDP specification defines the concept of a Trusted MIDlet suite in which the AMS is able to authenticate the application. The mechanism defined as "Trusted MIDlet Suites Using X.509 PKI" is one mechanism for authenticating a MIDlet suite. When the AMS can authenticate the MIDlet suite, it MUST be considered authenticated for the purposes of this API. The authority for a signed MIDlet suite is the value of the Subject field of the signing certificate. An application gets the authority using the {@link javax.microedition.content.ContentHandler#getAuthority ContentHandler.getAuthority} method. The format of the authority for X.509 certificates is defined by the MIDP Printable Representation of X.509 Distinguished Names as defined in class `javax.microedition.pki.Certificate`.

### Example of MIDlet Suite Java Application Descriptor (JAD)

A JAD file is a text file that you can create with any text editor. Its lines have this syntax:

attribute`:`value

The following is an example of the contents of a MIDlet suite Java application descriptor file. The bold text indicates additions to support content handlers.

    MIDlet-1: Image Viewer,, example.imageviewer.ImageViewer
    MicroEdition-Handler-1: example.imageviewer.ImageViewer, image/png, .png, open, en-US fr fi-FI
    MicroEdition-Handler-1-en-US: Open
    MicroEdition-Handler-1-fr: Voir
    MicroEdition-Handler-1-fi-FI: Avaa
    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

The JAD file has these attributes:

- `MIDlet-`n - Comma-separated list of the nth MIDlet’s name, icon, and class where n is the nth MIDlet in the JAR file. The lowest value of n is one (1), and n increments by one for each consecutive MIDlet. Supplying an icon is optional.
- `MicroEdition-Handler-`n - A content handler registration for this MIDlet suite.
- `MicroEdition-Handler-<n>-<locale>` - The localized names of actions.
- `MicroEdition-Handler-<n>-ID` - The ID of the content handler application.
- `MicroEdition-Handler-<n>-Access` - The IDs of applications that are allowed access and visibility to the content handlers.
- `MIDlet-Jar-Size` - Number of bytes in the JAR file.
- `MIDlet-Jar-URL` - Location of the JAR file.
- `MIDlet-Name` - Name of the MIDlet that will be presented to the user.
- `MIDlet-Vendor` - Creator of the MIDlet.
- `MIDlet-Version` - Version number of the MIDlet in the major.minor.micro format described in the Java Product Versioning Specification at http://java.sun.com/j2se/1.4.2/docs/guide/versioning/index.html.
- `MicroEdition-Configuration` - J2ME platform configuration that the MIDlet requires. For MIDP, the value should be `CLDC-1.0` or a later version.
- `MicroEdition-Profile` - Comma-separated list of J2ME platform profiles that the MIDlet requires. For MIDP, the value should be `MIDP-2.0`.

@since CHAPI 1.0