Showing posts with label portleturlgeneration. Show all posts
Showing posts with label portleturlgeneration. Show all posts

Creating a URL to portlet in solo state

In the Using PortletStateManagerService for creating URLs from within the portlet blog i talked about how to create a URL to portal page from inside portlet.

But what if you want to create a URL to portlet in solo state, in the solo state WebSphere portal wont display navigation of the page and looks much better suited for opening portal page in dialog box. You can use following code to create a URL to portlet in solo state


private String getPortletSoloStateURL(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");

final PortletAccessorFactory portletFct = (PortletAccessorFactory) portletStateManager
.getAccessorFactory(PortletAccessorFactory.class);
final PortletAccessorController portletCtrl = portletFct
.getPortletController("com.webspherenotes.popup.control",
url.getState());
portletCtrl.getParameters().put("userName",
new String[] { "soloStateSample" });

final SoloAccessorFactory soloAccessorFactory = portletStateManager.getAccessorFactory(SoloAccessorFactory.class);
SoloAccessorController soloStateController = soloAccessorFactory.getSoloAccessorController(url.getState());
soloStateController.setSoloPortlet("com.webspherenotes.popup.control");

String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException 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 (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
e.printStackTrace(System.out);
}
return null;
}


You can use the SoloAccessorFactory for invoking a portlet in solo state. This is how my target page looks like in the solo state

Creating a URL to different portlet mode and window state

In the Using PortletStateManagerService for creating URLs from within the portlet blog i talked about how to create a URL to portal page from inside portlet.

But what if you want to create a URL that will invoke the target portlet in say Edit mode and maximized window state, in that case you can use following code


private String getEditModeMaximizedURL(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");

final PortletAccessorFactory portletFct =
(PortletAccessorFactory) portletStateManager
.getAccessorFactory(PortletAccessorFactory.class);
final PortletAccessorController portletCtrl =
portletFct
.getPortletController("com.webspherenotes.popup.control",
url.getState());
portletCtrl.getParameters().put("userName",
new String[] { "editModeSample" });

portletCtrl.setPortletMode(PortletMode.EDIT);
portletCtrl.setWindowState(WindowState.MAXIMIZED);


String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException 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 (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
e.printStackTrace(System.out);
}
return null;
}



You can use the PortletAccessorController for setting a different portlet mode and window state for a portlet. After setting this is how my target page looks like in the edit mode

Creating action URL with parameters using PortletStateManagerService

In the Using PortletStateManagerService for creating URLs from within the portlet blog i talked about how to create a URL to portal page from inside portlet.

But what if you want to create a action url pointing to portlet on particular page and you also want to send some parameters while calling the action, this will result in the processAction() method of the target method being called, you can use following method


private String getPortletActionURL(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");

final PortletAccessorFactory portletFct =
(PortletAccessorFactory) portletStateManager
.getAccessorFactory(PortletAccessorFactory.class);
final PortletAccessorController portletCtrl =
portletFct
.getPortletController("com.webspherenotes.popup.control",
url.getState());
portletCtrl.getParameters().put("userName",
new String[] { "soloStateSample" });


final PortletTargetAccessorFactory portletTargetAccessorFactory =
(PortletTargetAccessorFactory)portletStateManager.getAccessorFactory(PortletTargetAccessorFactory.class);
final PortletTargetAccessorController portletTargetAccessor =
portletTargetAccessorFactory.getPortletTargetAccessorController(url.getState());

portletTargetAccessor.setActionTarget("com.webspherenotes.popup.control");
portletTargetAccessor.getParameters().put("actionParam", new String[]{"actionParamValue" });


String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException 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 (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
e.printStackTrace(System.out);
}
return null;
}


You can use PortletTargetAccessor for creating action URLs pointing to particular portlet, it also allows you to set action parameters. This is how the target page looks like

Creating render URL with parameters using PortletStateManagerService

In the Using PortletStateManagerService for creating URLs from within the portlet blog i talked about how to create a URL to portal page from inside portlet.

But what if you want to create a RenderURL pointing to portlet on particular page and you also want to send some render parameters to that portlet. If thats the case you can use the following method


private String getPortletRenderURLWithParams(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");

final PortletAccessorFactory portletFct = (PortletAccessorFactory) portletStateManager
.getAccessorFactory(PortletAccessorFactory.class);
final PortletAccessorController portletCtrl = portletFct
.getPortletController("com.webspherenotes.popup.control",
url.getState());
portletCtrl.getParameters().put("userName",
new String[] { "renderParameterSample" });

String finalURL = url.toString();
System.out.println("Portal Page URL " + finalURL);
return finalURL;
} catch (InvalidConstantException e) {
e.printStackTrace(System.out);
} catch (CannotInsertSelectionNodeException 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 (MissingUniqueNameException e) {
e.printStackTrace(System.out);
} catch (UnknownUniqueNameException e) {
e.printStackTrace(System.out);
} catch (StateManagerException e) {
e.printStackTrace(System.out);
} catch (UnknownAccessorTypeException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateAccessorException e) {
e.printStackTrace(System.out);
} catch (CannotInstantiateURLFactoryException e) {
e.printStackTrace(System.out);
}
return null;
}


In this method first i am using SelectionAccessorFactory for creating a URL to page (com.webspherenotes.popup.static) then i am using PortalAccessorFactory to target portlet window whose unique name is com.webspherenotes.popup.control and then i am setting render parameters

Now when i click on the URL to open the portlet render url this is what i see

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