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 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;


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 =
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());
String portletName =getPortletName((ServletRequest)renderRequest, (ServletResponse)renderResponse, contentPage, container);
return portletName;
} catch (ModelException e) {
logger.error("Error in SPIUtil.getPortletName() " + e.getMessage(),e);
return null;

public void init(){
logger.debug("Entering SPIUtil.init()");
try {
InitialContext context = new InitialContext();
navigationSelectionModelHome = (NavigationSelectionModelHome) context
contentModelHome = (ContentModelHome) context
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 =;
Iterator itr2 = getAllChildPortlets((LayoutNode) o, lm)
while (itr2.hasNext()) {
Object o1 =;
} else {
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


Anonymous said...

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

Anonymous said...


srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store