Monday, 29 July 2013

IPC (Inter Portlet Communication)

IPC (Inter Portlet Communication)

àIntroduction
The first version of the portlet specification, JSR-168/portlet1.0, did not include any support for Inter Portlet Communication. The second version, JSR-286/portlet2.0, which is supported for IPC  Mechanism.
IPC  is made easy  with JSR-286 to share the data between two portlets. Using IPC mechanisms, we can share the data from ACTION to VIEW phase and  VIEW-VIEW Phase.

There are 4  ways  to  share  the  data   between  2  portlets.
1.     Portlet Session
2.     Public Render Parameters
3.     Event
4.     Cookies

1. Session Based IPC
By default, Each war has its own session and will not be shared with other wars. Liferay provides a mechanism by which Portlets can share session attributes across WARs.
    A PortletSession is created for each user per portlet application. This makes the PortletSession useful for communicating all user related information among different portlets in the same portal application.

Note: We have 2 scopes at session level.. 1.Portlet Scope and 2.Application Scope

Example:
liferay-portlet.xml :
<portlet>
<portlet-name>JobDetails</portlet-name>
<icon>/icon.png</icon>
<instanceable>true</instanceable>
<private-session-attributes>false</private-session-attributes>
<render-weight>1</render-weight>
<ajaxable>false</ajaxable>
<header-portlet-css>/css/test.css</header-portlet-css>
<header-portlet-javascript>/js/test.js</header-portlet-javascript>
</portlet>

The private session attributes above allows the portlet to read and share session attributes with Liferay portal and other portlets.
    If you set some session attributes in say Pre-auth or post auth in the extension environment your portlet will be able to read it from the portlet session if the above is set in the liferay portlet.xml

PORTLET-1

Steps
1. Create Portlet
2. Prepare JSP (with drop down items), prepare renderURL
3. Once we click on the button, doView() is called..
    a.   Create portletSession instance
    b.   Get the selected value from JSP
    c.   Setting the value into session scope
4. It is showing same JSP as result page.
5.  <private session attribute> false </private session attribute>

PortletOne.java
package com.test.sample.action;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.IOException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class PortletOneAction extends GenericPortlet {

    public void init() {
    viewJSP = getInitParameter("view-jsp");
    }
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
    throws IOException, PortletException
{
String deptInfo = renderRequest.getParameter("deptInfo");
System.out.println("Dept Info :"+deptInfo);
PortletSession session  = renderRequest.getPortletSession();
session.setAttribute("deptInfo", deptInfo, PortletSession.APPLICATION_SCOPE);
include(viewJSP, renderRequest, renderResponse);
}





protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException
{
PortletRequestDispatcher portletRequestDispatcher =getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(PortletOneAction.class);
}

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<portlet:renderURL  var="deptURL"/>
<form action="<%=deptURL.toString() %>" method="post">
    <select name="deptInfo" id="deptInfo">
          <option>Select Department</option>
          <option value=hr>HR</option>
          <option value=finance>Finance</option>
          <option value=admin>Admin</option>
    </select>
<input type="submit" name="go" value="Go"/>
</form>

PORTLET-2

PortletTwo.java

package com.test.sample.action;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;




import java.io.IOException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class PortletTwoAction extends GenericPortlet {
public void init()
{
viewJsp = getInitParameter("view-jsp");
}
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException
{
System.out.println("Portlet - 2 ... Do View invoked");
PortletSession session  = renderRequest.getPortletSession();
String deptInfo = (String)session.getAttribute("deptInfo", PortletSession.APPLICATION_SCOPE);
//System.out.println("Dept Info :"+deptInfo);
if(Validator.isNotNull(deptInfo)){
if(deptInfo.equalsIgnoreCase("hr")){
include(hrJsp, renderRequest, renderResponse);
}else if(deptInfo.equalsIgnoreCase("admin")){
include(adminJsp, renderRequest, renderResponse);
}else if(deptInfo.equalsIgnoreCase("finance")){
include(financeJsp, renderRequest, renderResponse);
}
}
else{
include(viewJsp, renderRequest, renderResponse);      
}
}
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
PortletSession session  = renderRequest.getPortletSession();





PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
session.removeAttribute("deptInfo", PortletSession.APPLICATION_SCOPE);
}
}
protected String hrJsp = "/html/jsp/hr.jsp";
protected String adminJsp = "/html/jsp/admin.jsp";
protected String financeJsp = "/html/jsp/finance.jsp";
protected String viewJsp;
private static Log _log = LogFactoryUtil.getLog(PortletTwoAction.class);
}

View.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
String deptInfo = (String)request.getAttribute("deptInfo");
%>
<h2> Welcome to Dept Info Portlet</h2>

admin.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<h2>Welcome to Admin Page</h2>

finance.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<h2>Welcome to Finance Page</h2>

hr.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />

<h2>Welcome to HR Page</h2>

No comments:

Post a Comment