21 June, 2013

IPC using public render parameter

In my past post we have seen how to do IPC with evenaction see here.
No we will see IPC using public render parameter.

This is the very firstly introduced and simple way to achieve IPC given by JSR-286.
Description:

In JSR 168 (Portlet 1.0), the render parameters set in the processAction() method are available only in the render phase of the same portlet.

By using the public render parameters feature, the render parameters set in the processAction() method of one portlet are available in render parameters of the other portlets. Using public render parameters instead of events avoids the additional process event call. The public render parameters can also be set on the render URL.

To enable coordination of render parameters with other portlets within the same portlet application or across portlet applications, the portlet can declare public render parameters in its deployment descriptor using the public-render-parameter element in the portlet application section. Public render parameters can be viewed and changed by other portlets or components.

In the portlet section, each portlet can specify the public render parameters to be shared through the supported-public-render-parameter element. The supported-public-render-parameter element must reference the identifier of a public render parameter defined in the portlet application section in a public-render-parameter element.

Example: If we carry same old portlets which we have build in last example i.e for Event model IPC namely test1-portlet and test2-portlet.

Step 1:

Declare the render parameters to be shared in the portlet.xml file by setting the public render parameters at the portlet application level.

e.g:

<portlet-app ...>
<portlet> ... </portlet>
<public-render-parameter>
<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>
</public-render-parameter>
</portlet-app>

Note:
1.A developer can declare a list of public paramters for a portlet application in portlet.xml.
2.Parameter names are namespaced to avoid naming conflict.

Step 2:

Portlet must declare which public param they want to use.

e.g.
After adding declaration portlet.xml will look something like this.
<portlet-app ......>
<portlet>
<portlet-name>test1</portlet-name> <display-name>test1</display-name>
........ ........
<supported-public-render-parameter>
user-id
</supported-public-render-parameter>
</portlet>
<public-render-parameter>
<identifier>user-id</identifier> <qname xmlns:x="http://abc.com/userId">x:userId</qname>
</public-render-parameter>
</portlet-app>

Note: Public params are available in all lifecycle method like processAction , processEvent, render and serveResource.

Step 3: 

We can set render parameter in the processAction() method by using the defined public render parameter identifier as the key.

e.g.

public void processAction(ActionRequest request, ActionResponse response)
throws IOException, PortletException { ........ response.setRenderParameter("user-id", userId); ........
}

Step 4: 

A portlet can read public render parameter using follwing method

request.getPublicParameterMap()

Note: Public render parameters are merged with regular parameters so can also be read using

request.getParameter(name) or request.getParameterMap()

Step 5: 

A portlet can remove a public render parameter by invoking following methods.

response.removePublicRenderParameter(name)

or

portletURL.removePublicRenderParameter(name)

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...