Interportlet Communication in different War

Interportlet communication in JSR 168 can be achieved through PortletSession with APPLICATION SCOPE, and as session can't be shared across two different war so we can't achieve IPC in different war files.

We can make use of IBM's Advance URL Generation classes API is to achieve IPC between different war files.
Using this API we can generate a link to any portal page and target any particular portlet on that page. We can also pass some parameter to target portlet.

One obvious use case for this, there are two portlets ( from different war file) added on same page, but there could be possibility where in page A has portlet A and page B has portlet B.

On click of a link on portlet A, portlet B on page B should get render and show data based on parameter retrieved from Portlet A.
With Advance URL Generation classes, we can achieve both the scenarios...
 
If we consider second use case, here we go to achieve that,
Create a url to Portlet B on Page B
Write this code in processAction method of Portlet A,
//Code
String paramValue[] = new String[1];paramValue[0]="Neeraj Sidhaye";
HashMap map= new HashMap();map.put("UserName", paramValue);
String targetURLStr = ServletURLHelper.generateUrl("PageB", "PortletB", map, actionRequest, actionResponse);
//Explaination
where, PortletB is unique name of the portlet and unique name must be assigned through xml access only. PageB is the unique name of the page, which can be assigned through portal administration by editing page properties.MAP is used to pass parameter to targeted portlet to achieve InterPortlet Communication.
//Code
actionResponse.sendRedirect(targetURLStr);
//Explaination This will redirect to generated url that is to Portlet B on page B.
// Code for Portlet B.
// In doView method of Portlet B, you just need to retrieve the map and iterate it to get UserName value.
Another helper class to achieve the same directly from on click of a link on jsp
// code - write this code on jsp
String targetUrlStr = PortletURLHelper.generateUrl(pageName, portletName, params, request, response)
// and then use this targetUrlStr value on href of a link
Now i am thinking of how to achieve the same using JSR168 or JSR 286 API.

5 comments:

Sean C said...

I am trying to do something similar. From within Portlet A, I want to redirect to Portlet B.

So it appears that I want to use the ServletURLHelper class.

Which jar is the ServletURLHelper class in?

Thanks,
Sean

Sean C said...

Ok, I found the jar (http://www-01.ibm.com/support/docview.wss?rs=688&ca=portall2&uid=swg21265900).

Now, the big discrepancy between your code and the API exposed via the jar is that it only takes HttpServletRequest and HttpServletResponse objects when calling:

public static String generateUrlToPortlet(
String pageName,
String portletName,
HashMap params,
HttpServletRequest request,
HttpServletResponse response,
PortletType portletType,
String actionReference)


Is there another jar, or some other way to get around this difference?

Thanks,
Sean

Neeraj Sidhaye said...

Sean,

You can make use of
wp.l2.url.helper.jar and use PortletURLHelper class to generate a url to anoter portlet

see code below

PortletURLHelper.generateUrl(String pageName, String portletName, HashMap params, PortletRequest request, PortletResponse response);

Sean C said...

Thanks for the response both here and on DevWorks.

I have provided more information on the DevWorks thread:

https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14302826&#14302826

Parivesh Jain said...
This comment has been removed by the author.