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

1 comment:

Kareem Mahmoud said...

Hi Sunil,

Which is better from perfoamcne perspective, doView or doEdit ?