Finding out unique name of the portlet that is being rendered

In the Getting name of the page where your portlet is getting rendered, so i wanted to also build a sample to demonstrate how to get the unique name of the portlet that is being rendered.

This might not be of much use in Local portlet, but if your portlet is getting consumed using WSRP and unique name of the portlet on producer and consumer is different then you can use this SPIUtil.java class to get uniquename of the portlet that is being rendered


package com.webspherenotes.jsr286.filters;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ibm.portal.ModelException;
import com.ibm.portal.ObjectID;
import com.ibm.portal.content.ContentModel;
import com.ibm.portal.content.ContentNode;
import com.ibm.portal.content.ContentPage;
import com.ibm.portal.content.LayoutContainer;
import com.ibm.portal.content.LayoutControl;
import com.ibm.portal.content.LayoutModel;
import com.ibm.portal.content.LayoutNode;
import com.ibm.portal.model.ContentModelHome;
import com.ibm.portal.model.NavigationSelectionModelHome;
import com.ibm.portal.model.NavigationSelectionModelProvider;
import com.ibm.portal.model.PortletModelHome;
import com.ibm.portal.navigation.NavigationNode;
import com.ibm.portal.navigation.NavigationSelectionModel;
import com.ibm.portal.portletmodel.PortletDefinition;
import com.ibm.portal.portletmodel.PortletModel;
import com.ibm.portal.portletmodel.PortletWindow;

public class SPIUtil {

private static final Logger logger = LoggerFactory.getLogger(SPIUtil.class);

private NavigationSelectionModelHome navigationSelectionModelHome;
private ContentModelHome contentModelHome;
private PortletModelHome portletModelHome;

public String getPortletName(PortletRequest renderRequest, PortletResponse renderResponse){
try {
if (navigationSelectionModelHome != null) {
NavigationSelectionModelProvider provider =
navigationSelectionModelHome.getNavigationSelectionModelProvider();
NavigationSelectionModel model =
provider.getNavigationSelectionModel((ServletRequest)renderRequest, (ServletResponse)renderResponse);
ContentModel contentModel =(ContentModel)contentModelHome.getContentModelProvider().getContentModel((ServletRequest)renderRequest, (ServletResponse)renderResponse);

NavigationNode navigationNode = (NavigationNode) model.getSelectedNode();

ContentNode contentNode = navigationNode.getContentNode();
ContentPage contentPage =(ContentPage) contentModel.getLocator().findByID(contentNode.getObjectID());

LayoutModel layoutModel = contentModel.getLayoutModel(contentPage);
List portletList = getAllChildPortlets((LayoutNode)layoutModel.getRoot(), layoutModel);
String nameSpaceStr = renderResponse.getNamespace();
String nameSpacePortletId = nameSpaceStr.substring(3);

nameSpacePortletId = nameSpacePortletId.substring(0,nameSpacePortletId.length()-1);

for(int i = 0 ; i < portletList.size() ; i++){
LayoutControl container = (LayoutControl)portletList.get(i);
String currentWindowObjectId = getObjectIDStr(container.getObjectID());
if(nameSpacePortletId.equals(currentWindowObjectId)){
String portletName =getPortletName((ServletRequest)renderRequest, (ServletResponse)renderResponse, contentPage, container);
return portletName;
}
}
}
} catch (ModelException e) {
logger.error("Error in SPIUtil.getPortletName() " + e.getMessage(),e);
e.printStackTrace();
}
return null;
}

public void init(){
logger.debug("Entering SPIUtil.init()");
try {
InitialContext context = new InitialContext();
navigationSelectionModelHome = (NavigationSelectionModelHome) context
.lookup(NavigationSelectionModelHome.JNDI_NAME);
contentModelHome = (ContentModelHome) context
.lookup(ContentModelHome.JNDI_NAME);
portletModelHome = (PortletModelHome)context.lookup(PortletModelHome.JNDI_NAME);
} catch (NamingException e) {
logger.error("Error in SPIUtil.init() " + e.getMessage(),e);
}
logger.debug("Exiting SPIUtil.init()");
}

private List getAllChildPortlets(LayoutNode subtreeRoot,
LayoutModel lm) throws ModelException {
List aList = new LinkedList();
if (subtreeRoot instanceof LayoutContainer) {
Iterator itr = lm.getChildren(subtreeRoot);
while (itr.hasNext()) {
Object o = itr.next();
Iterator itr2 = getAllChildPortlets((LayoutNode) o, lm)
.iterator();
while (itr2.hasNext()) {
Object o1 = itr2.next();
aList.add(o1);
}
}
} else {
aList.add(subtreeRoot);
}
return aList;
}

private static String getObjectIDStr(ObjectID objectID){
String temp = objectID.toString();
int firstInd = temp.indexOf("'");
String result = temp.substring(firstInd+1, temp.indexOf("'", firstInd+1 ));
return result;
}

private String getPortletName(ServletRequest request, ServletResponse response, ContentPage contentPage,
LayoutControl layoutControl ){
logger.debug("Entering SPIUtil.getPortletName()");

try {
PortletModel portletModle = portletModelHome.getPortletModelProvider().getPortletModel(contentPage,request,response);
PortletWindow portletWindow = portletModle.getPortletWindow(layoutControl);
PortletDefinition portletDefinition = portletModle.getPortletDefinition(portletWindow);
logger.debug("Exiting SPIUtil.getPortletName()");
return portletDefinition.getObjectID().getUniqueName();
} catch (ModelException e) {
logger.error("Error in SPIUtil.getPortletName() " + e.getMessage(),e);
}
return null;
}

}


In order to use this class you will have to call its init method from the init method of your portlet then call its getPortletName method to get name of the portlet being displayed

2 comments:

Anonymous said...

This is very valuable, thank you so much for sharing!

Anonymous said...

Thanks