Skip to main content

jMaki - Accessing External Services

Posted by arungupta on July 27, 2007 at 10:13 AM PDT

A jMaki widget in a page can
communicate with:

  • Other widgets on the same page using

    publish/subscribe mechanism
  • Services in the application domain using service="data.jsp".
  • Services outside the application domain using proxy (xhp).

This blog entry explains how external services can be accessed by jMaki
widgets.

This image shows a Web application with multiple pages (Page1, Page2, Page3)
where one of the page (Page2) contain at least one jMaki widget. For a BlockList
widget, the generated code fragment looks like:

<a:widget name="jmaki.blockList" value="[

    {title : 'jMaki Project Home', link : 'https://ajax.dev.java.net',
description : 'Where to go for the latest jMaki.' },

    {title : 'jMaki Widgets Home', link : 'https://widgets.dev.java.net',
description : 'The source for the latest jMaki widgets.' },

    {title : 'jMaki-Charting Home', link : 'https://jmaki-charting.dev.java.net',
description : 'Enables complex charts rendered on the client in any modern
browser.' }

]" />

This code fragment populates the widget with static data specified in the
value attribute. In order to populate the widget with data from an
external service, such as an RSS feed, the value attribute needs to be replaced
with service="/xhp?id=rss". Let me explain how that works.

If any page consists of a jMaki widget then the jMaki runtime is bundled with
the application. The runtime consists of jmaki.xhp.XmlHttpProxyServlet
class that maps to "/xhp" URL pattern within the application
context. The id specified in the URL, "rss", is configured in a
configuration file named "xhp.json". This file consists of a list
of external services that can be accessed by the widgets in a page. Each entry
in this file can can be specified using up to 5 different parameters:

  • ID (required parameter, unique identifier for the entry)
  • URL (required parameter, location of external service)
  • Parameters (optional parameters specifying the default values passed to
    URL)
  • API Key (optional parameter to invoke the service with a specific key)
  • Stylesheet (optional parameter to process the response)

So if BlockList widget needs to access the RSS feed, then the code fragment
needs to look like:

<a:widget name="jmaki.blockList" service="/xhp?id=rss" />

The corresponding entry in xhp.json is:

{"id": "rss",

    "url":"http://weblogs.java.net/blog/ludo/index.rdf",

    "xslStyleSheet": "rss.xsl"

}

The tag and the default entry tells the jMaki runtime to fetch the RSS feed
from http://weblogs.java.net/blog/ludo/index.rdf, apply the 'rss.xsl'
stylesheet (that understand the multiple RSS/Atom formats) to the received
response and convert the data into into a common JSON data format of the type
"dataType" : "jMakiRSS". The BlockList widget knows how to convert
jmakiRSS data to its specific data model.

If any other external service needs to be accessed by a widget then it needs
to be specified in xhp.json and accessed using service
attribute. This approach allows all widgets, that take a "value" attribute,
to consume data from external services.

Technorati: jmaki
rss
services
web2.0

Related Topics >>