Utility Class to generate single portlet url

I copied the sample code from Refreshing individual portlets and preferences using Single Portlet Refresh in WebSphere Portal V6.0.1 article into an self contained Utility class so that it can be used in other portlets.

I did create following 4 methods that let you handle the most common use cases of using the Single portlet refresh url


  • getPortletRefreshUrl: This method will give you render URL for the current portlet

  • getPortletActionUrl: This method will give you the action url pointing to the current portlet. Please note one important point that the action Url is valid for only one request so if you want to make action call using Ajax request you cant reuse the actionUrl, you will have to get a new action url from the server every time

  • getPortletEditModeUrl: You can use this method to get the markup for edit mode of the portlet. It will call the doEdit() method of your portlet and return markup generated by it

  • getPortletHelpModeUrl: You can use this method to get the markup for help mode of the portlet. It will call the doHelp() method of your portlet and return markup generated by it



Making a resource request is pretty straight forward, you dont have to do anything special for it.


package com.webspherenotes.ajax;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.WindowState;

import com.ibm.portal.ModelException;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.portlet.service.model.PortletModelProvider;
import com.ibm.portal.portletmodel.PortletWindow;
import com.ibm.portal.state.EngineURL;
import com.ibm.portal.state.PortletStateManager;
import com.ibm.portal.state.StateHolderController;
import com.ibm.portal.state.URLFactory;
import com.ibm.portal.state.accessors.portlet.PortletAccessorController;
import com.ibm.portal.state.accessors.portlet.PortletTargetAccessorController;
import com.ibm.portal.state.accessors.selection.SelectionAccessorController;
import com.ibm.portal.state.accessors.selection.SelectionAccessorFactory;
import com.ibm.portal.state.exceptions.StateException;
import com.ibm.portal.state.service.PortletStateManagerService;
import com.ibm.ws.install.configmanager.logging.LoggerFactory;

public class SPRUtil {

Logger logger = LoggerFactory.createLogger(SPRUtil.class);
/** JNDI name needed to lookup the state manager service */
private static final String JNDI_STATE_SERVICE =
"portletservice/com.ibm.portal.state.service.PortletStateManagerService";

/** JNDI name needed to lookup the portlet model provider */
private static final String JNDI_MODEL_PROVIDER =
"portletservice/com.ibm.portal.portlet.service.model.PortletModelProvider";

/** portlet state manager service */
protected PortletStateManagerService stateService;

/** portlet model provider */
protected PortletModelProvider modelProvider;

PortletWindow portletWindow;


public SPRUtil(){
init();
}


public void init() {
try {
final Context ctx = new InitialContext();
// lookup the portlet state manager service
PortletServiceHome psh = (PortletServiceHome) ctx.lookup(JNDI_STATE_SERVICE);
stateService = (PortletStateManagerService)
psh.getPortletService(PortletStateManagerService.class);
// lookup the portlet model provider
psh = (PortletServiceHome) ctx.lookup(JNDI_MODEL_PROVIDER);
modelProvider = (PortletModelProvider)
psh.getPortletService(PortletModelProvider.class);
} catch (NamingException e) {
e.printStackTrace();
} catch (PortletServiceUnavailableException e) {
e.printStackTrace();
}
}

public String getPortletRefreshUrl(PortletRequest request, PortletResponse response)throws PortletException{
logger.entering(SPRUtil.class.getName(),"getRenderUrl");
StringWriter writer = new StringWriter();
try {
// get the request-specific portlet state manager
final PortletStateManager stateManager =
stateService.getPortletStateManager(request, response);
// get the portlet window
final PortletWindow portletWindow =
modelProvider.getCurrentPortletWindow(request);

// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, portletWindow, state);

// if needed add other aspects to the URL
// ...
// write the URL to the markup

url.writeDispose(writer);
// dispose the objects that are no longer needed
urlFactory.dispose();
stateManager.dispose();
} catch (StateException e) {
throw new PortletException(e);
} catch (ModelException e) {
throw new PortletException(e);
} catch (IOException e) {
throw new PortletException(e);
}
logger.exiting(SPRUtil.class.getName(),"getRenderUrl");
return writer.toString();
}

public String getPortletActionUrl(PortletRequest request, PortletResponse response)throws PortletException{
logger.entering(SPRUtil.class.getName(),"getRenderUrl");
StringWriter writer = new StringWriter();


try {
// get the request-specific portlet state manager
final PortletStateManager stateManager =
stateService.getPortletStateManager(request, response);
// get the portlet window
final PortletWindow portletWindow =
modelProvider.getCurrentPortletWindow(request);

// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, portletWindow, state);
createActionURL(stateManager, portletWindow, null).writeDispose(writer);
// dispose the objects that are no longer needed
urlFactory.dispose();
stateManager.dispose();
} catch (StateException e) {
throw new PortletException(e);
} catch (ModelException e) {
throw new PortletException(e);
} catch (IOException e) {
throw new PortletException(e);
}
logger.exiting(SPRUtil.class.getName(),"getRenderUrl");
return writer.toString();
}

public String getPortletEditModeUrl(PortletRequest request, PortletResponse response)throws PortletException{
logger.entering(SPRUtil.class.getName(),"getRenderUrl");
StringWriter writer = new StringWriter();
try {
// get the request-specific portlet state manager
final PortletStateManager stateManager =
stateService.getPortletStateManager(request, response);
// get the portlet window
final PortletWindow portletWindow =
modelProvider.getCurrentPortletWindow(request);

// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, portletWindow, state);
setState(stateManager, portletWindow, state, PortletMode.EDIT, null, null);
// if needed add other aspects to the URL
// ...
// write the URL to the markup

url.writeDispose(writer);
// dispose the objects that are no longer needed
urlFactory.dispose();
stateManager.dispose();
} catch (StateException e) {
throw new PortletException(e);
} catch (ModelException e) {
throw new PortletException(e);
} catch (IOException e) {
throw new PortletException(e);
}
logger.exiting(SPRUtil.class.getName(),"getRenderUrl");
return writer.toString();
}


public String getPortletHelpModeUrl(PortletRequest request, PortletResponse response)throws PortletException{
logger.entering(SPRUtil.class.getName(),"getRenderUrl");
StringWriter writer = new StringWriter();
try {
// get the request-specific portlet state manager
final PortletStateManager stateManager =
stateService.getPortletStateManager(request, response);
// get the portlet window
final PortletWindow portletWindow =
modelProvider.getCurrentPortletWindow(request);

// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, portletWindow, state);
setState(stateManager, portletWindow, state, PortletMode.HELP, null, null);
// if needed add other aspects to the URL
// ...
// write the URL to the markup

url.writeDispose(writer);
// dispose the objects that are no longer needed
urlFactory.dispose();
stateManager.dispose();
} catch (StateException e) {
throw new PortletException(e);
} catch (ModelException e) {
throw new PortletException(e);
} catch (IOException e) {
throw new PortletException(e);
}
logger.exiting(SPRUtil.class.getName(),"getRenderUrl");
return writer.toString();
}

protected void setSelectionMapping(final PortletStateManager stateManager,
final PortletWindow window, final StateHolderController urlState)
throws StateException {
// get the selection accessor controller to set the selection mapping
final SelectionAccessorFactory factory = (SelectionAccessorFactory) stateManager
.getAccessorFactory(SelectionAccessorFactory.class);

final SelectionAccessorController selCtrl = factory
.getSelectionAccessorController(urlState);
try {
// set the mapping which maps the current state to our portlet window
selCtrl.setSelectionMapping(selCtrl.getSelection(), window.getObjectID());
} finally {
// release the selection controller
selCtrl.dispose();
}
}


protected EngineURL createRenderURL(final PortletStateManager stateManager,
final PortletWindow window, final Map renderParams, final PortletMode mode,
final WindowState winState) throws StateException {
// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
// get a new EngineURL object from the factory
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, window, state);
// set the render state
setState(stateManager, window, state, mode, winState, renderParams);
// release the URL factory
urlFactory.dispose();
// return the URL
return url;
}

protected void setState(final PortletStateManager stateManager,
final PortletWindow window, final StateHolderController urlState,
final PortletMode mode, final WindowState winState, final Map params)
throws StateException {
// get the portlet accessor controller
final PortletAccessorController portletCtrl =
stateManager.getPortletAccessorController(urlState);
try {
// set portlet mode (if any)
if (mode != null) {
portletCtrl.setPortletMode(mode);
}
// set window state (if any)
if (winState != null) {
portletCtrl.setWindowState(winState);
}
// set render /action parameters (if any)
if (params != null && !params.isEmpty()) {
portletCtrl.getParameters().putAll(params);
}
} finally {
// release portlet accessor controller
portletCtrl.dispose();
}
}

protected void setAction(final PortletStateManager stateManager,
final PortletWindow window, final StateHolderController urlState)
throws StateException {
// get the target accessor controller to set the action
final PortletTargetAccessorController targetCtrl = stateManager
.getPortletTargetAccessorController(urlState);
try {
// set the action to our portlet window
targetCtrl.setActionTarget(window.getObjectID());
} finally {
// release the target controller
targetCtrl.dispose();
}
}
protected EngineURL createActionURL(final PortletStateManager stateManager,
final PortletWindow window, final Map actionParams) throws StateException {
// get the URL factory
final URLFactory urlFactory = stateManager.getURLFactory();
// get a new EngineURL object from the factory
final EngineURL url = urlFactory.newURL(null);
// get the URL-specific state
final StateHolderController state = url.getState();
// make the URL a single portlet refresh URL
setSelectionMapping(stateManager, window, state);
// set the portlet action
setAction(stateManager, window, state);
// set the action parameters
setState(stateManager, window, state, null, null, actionParams);
// release the URL factory
urlFactory.dispose();
// return the URL
return url;
}

}