dinsdag 23 september 2008

Resource URLs in Hippo/Jetspeed

Recently we wanted to implement a few Ajax calls in our Hippo portal, a Jetspeed based portal product. Since you are quite restricted within the context of a portal application with respect to accessing or modifying the response object, it turns out that you need a special kind of strategy in order to receive the ajax calls from the client and pass back the results: resourceURLs.
Our version of Hippo is based on Jetspeed 2.1.3 which does not yet comply with JSR-286 for portal standards. This JSR specifies how to handle ajax calls within a portal. Luckily this part of the specification has already been implemented in Jetspeed 2.1.3. So how does it work?

First we have to create a resourceURL factory :

org.apache.portals.bridges.common.PortletResourceURLFactory resourceURLFactory = new org.apache.jetspeed.portlet.PortletResourceURLFactoryImpl();

I've put this code in the init(config) method of a portlet and store this factory in a private instance variable.

Now in your portlet render template you must use an URL generated by this factory. We prepare this URL in the portlet code and store it in the Freemarker model:

model.put("ajaxurl", resourceURLFactory.createResourceURL(getPortletConfig(),
request, response, null));

Refer to this url in your form:

<form id="aForm" method="post" action="${ajaxurl}">
..
</form>

Now, when this form gets submitted, you will receive a hit on your doView method and you can inspect the request to check whether it was a resource request or a normal request:

org.apache.jetspeed.request.RequestContext jrc =
(org.apache.jetspeed.request.RequestContext)
request.getAttribute(org.apache.jetspeed.PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
if
(jrc.getPortalURL().getNavigationalState().getPortletWindowOfResource()!=null)
{
// it was a resource request
} else {
// it was a normal render request
}

Now you have full control over the response and can set its content type and get the output stream to pass back whatever content you like without going through the portal aggregating cycle. Pretty simple and works like a charm.

Geen opmerkingen: