Using PortletStateManagerService for creating URLs from within the portlet

In the Creating Portal/Portlet URL from outside the portal entry i built a sample for how you can use PortalStateManagerServiceHome for creating URL to other portal pages or portlets from either outside the portal or theme and skin.

But what if you want to create a URL from inside a portlet to other portal page, WPS provides PortletStateManagerService, that you can use from inside the portlet to create URL to other portal pages. I wanted to learn how it works so i built a sample portlet that can be used to ful-fill different use cases, you can download that sample portlet from here


package com.webspherenots.popup;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.PortletStateManager;
import com.ibm.portal.state.URLFactory;
import com.ibm.portal.state.accessors.exceptions.CannotInsertSelectionNodeException;
import com.ibm.portal.state.accessors.exceptions.MissingUniqueNameException;
import com.ibm.portal.state.accessors.exceptions.StateNotInRequestException;
import com.ibm.portal.state.accessors.exceptions.UnknownUniqueNameException;
import com.ibm.portal.state.accessors.portlet.PortletAccessorController;
import com.ibm.portal.state.accessors.portlet.PortletAccessorFactory;
import com.ibm.portal.state.accessors.portlet.PortletTargetAccessorController;
import com.ibm.portal.state.accessors.portlet.PortletTargetAccessorFactory;
import com.ibm.portal.state.accessors.selection.SelectionAccessorController;
import com.ibm.portal.state.accessors.selection.SelectionAccessorFactory;
import com.ibm.portal.state.accessors.solo.SoloAccessorController;
import com.ibm.portal.state.accessors.solo.SoloAccessorFactory;
import com.ibm.portal.state.accessors.themetemplate.ThemeTemplateAccessorController;
import com.ibm.portal.state.accessors.themetemplate.ThemeTemplateAccessorFactory;
import com.ibm.portal.state.accessors.url.URLAccessorFactory;
import com.ibm.portal.state.exceptions.CannotCloneDocumentModelException;
import com.ibm.portal.state.exceptions.CannotCreateDocumentException;
import com.ibm.portal.state.exceptions.CannotInstantiateAccessorException;
import com.ibm.portal.state.exceptions.CannotInstantiateURLFactoryException;
import com.ibm.portal.state.exceptions.InvalidConstantException;
import com.ibm.portal.state.exceptions.StateManagerException;
import com.ibm.portal.state.exceptions.UnknownAccessorTypeException;
import com.ibm.portal.state.service.PortletStateManagerService;

public class PortletStateManagerServicePortlet extends GenericPortlet {

PortletStateManagerService portletStateManagerService;

public void init() throws PortletException {
System.out.println("Entering PortletStateManagerServiceHome.init()");
try {
InitialContext context = new InitialContext();

final PortletServiceHome serviceHome = (PortletServiceHome) context
.lookup("portletservice/com.ibm.portal.state.service.PortletStateManagerService");
portletStateManagerService = (PortletStateManagerService) serviceHome
.getPortletService(PortletStateManagerService.class);

} catch (NamingException e) {
e.printStackTrace(System.out);
}
System.out.println("Exiting PortletStateManagerServiceHome.init()");
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering PortletStateManagerServiceHome.doView()");
response.setContentType("text/html");

String portalPageURL = getPortalPageURL(request, response);
request.setAttribute("portalPageURL", portalPageURL);
String portletRenderURL = getPortletRenderURLWithParams(request, response);
request.setAttribute("portletRenderURL", portletRenderURL);
String soloStateURL = getPortletSoloStateURL(request, response);
request.setAttribute("soloStateURL", soloStateURL);
String actionURL = getPortletActionURL(request, response);
request.setAttribute("actionURL", actionURL);

String editModeURL = getEditModeMaximizedURL(request,response);
request.setAttribute("editModeURL", editModeURL);

String themeURL = getDifferentThemeURL(request, response);
request.setAttribute("themeURL", themeURL);
getPortletContext().getRequestDispatcher("/index.jsp").include(request,
response);
System.out.println("Exiting PortletStateManagerServiceHome.doView()");
}

private String getPortalPageURL(PortletRequest request,
PortletResponse response) {
PortletStateManager portletStateManager = null;
try {
portletStateManager = portletStateManagerService
.getPortletStateManager(request, response);
final URLFactory urlFct = portletStateManager.getURLFactory();
final EngineURL url = urlFct.newURL(null);


final SelectionAccessorFactory selectionAccessorFactory = portletStateManager
.getAccessorFactory(SelectionAccessorFactory.class);
SelectionAccessorController selectionAccessorCOntroller = selectionAccessorFactory
.getSelectionController(url.getState());
selectionAccessorCOntroller
.setSelection("com.webspherenotes.popup.static");


String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotCloneDocumentModelException e) {
e.printStackTrace(System.out);
} catch (CannotCreateDocumentException e) {
e.printStackTrace(System.out);
} catch (StateNotInRequestException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException e) {
e.printStackTrace(System.out);
} catch (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (portletStateManager != null)
portletStateManager.dispose();
}
return null;
}
}


The PortletStateManagerServicePortlet.java has a getPortalPageURL() method, that is using SelectionAccessorFactory, for creating a URL to portal page, with unique name equal to com.webspherenotes.popup.static

This is same as that of the In the Creating Portal/Portlet URL from outside the portal, with the difference that i am using PortletStateManagerService from inside the portlet instead of PortalStateManagerService from servlet

5 comments:

Kareem Mahmoud said...

Hi Sunil,

Which is better from perfoamcne perspective, doView or doEdit ?

Subro said...

Hi,

When I click Page Properties in Edit Mode for WPS 9 , I see the below exception :

com.ibm.portal.state.accessors.exceptions.UnknownUniqueNameException


[15/3/18 13:26:48:781 IST] 00000160 ExceptionLogg 2 com.ibm.wps.logging.ExceptionLogger logThrowable Exception
com.ibm.portal.state.accessors.exceptions.UnknownUniqueNameException: EJPEI0158E: The unique name [com.ibm.wps.portlet.pageproperties.Portlet.window] is unknown and cannot be mapped to an ObjectID.


I enabled logging as well :

[15/3/18 11:07:21:281 IST] 000001bc ExceptionLogg W com.ibm.wps.logging.ExceptionLogger logThrowable An exception occurred: [EJPEI0158E: The unique name [com.ibm.wps.portlet.pageproperties.Portlet.window] is unknown and cannot be mapped to an ObjectID.]. Enable traces for [com.ibm.wps.logging.ExceptionLogger=all] to see the exception stack trace.
[15/3/18 11:07:21:282 IST] 000001bc ResolvedPrepr I com.ibm.wps.resolver.portal.ResolvedPreprocessor process The resolution process failed, for more details enable tracing for [com.ibm.wps.resolver.portal.ResolvedPreprocessor=all].

Kindly let me know how to fix.

I tried looking for the entry in Manage Pages with search Category : Unique Name Contains but did not find the result in my search as well..


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

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

EG MEDI said...

Egmedi.com 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