+

Search Tips   |   Advanced Search

PortletWCMContextHelper

Use the PortletWCMContextHelper class to determine the web content context from within a portlet.


Preparation

To use the PortletWCMContextHelper class, configure the portlet to receive a public render parameter and a path-info parameter. Update the portlet.xml file for the portlet, and add the following entries inside the <portlet-app> tag:
<public-render-parameter>
  <description>WCM public context</description>
  <identifier>PUBLIC_CONTEXT</identifier>
  <qname xmlns:wcm="http://www.ibm.com/xmlns/prod/datatype/content">wcm:context</qname>
</public-render-parameter>
<public-render-parameter>
  <description>Shared path-info parameter of WebSphere Portal</description>
  <identifier>PATH_INFO</identifier>
  <qname xmlns:wcm="http://www.ibm.com/xmlns/prod/websphere/portal/publicparams">wcm:path-info</qname>
</public-render-parameter>

In addition, update the <portlet> tag with the following lines:

<supported-public-render-parameter>PUBLIC_CONTEXT</supported-public-render-parameter>
<supported-public-render-parameter>PATH_INFO</supported-public-render-parameter>


Implementation

After a new instance of the PortletWCMContextHelper is created, the portlet can invoke the getCurrentWCMContext method, passing in the parameters PortletRequest and PortletResponse:

public String getCurrentWCMContext(PortletRequest, PortletResponse)

This method returns a string containing the content path that defines the current web content context. To determine the content path, the getCurrentWCMContext method performs the following checks:

  1. The method reads the value of the path-info public render parameter. The method constructs the content path from the path-info parameter and the content association of the current page, when these conditions are true:

    • The path-info parameter is present.

    • The friendly.pathinfo.enabled property is enabled in the portal configuration service.

  2. If the path-info parameter is not present or if the friendly.pathinfo.enabled property is disabled, the method reads the public render parameter. If the public render parameter is present, the method returns the value of this parameter.

  3. If no public render parameter is present, the method evaluates the current page for a default content association. The method then returns the path of the content item that is mapped to the page.


Source of PortletWCMContextHelper

/******************************************************************
 * Copyright IBM Corp. 2010, 2011                                 *
 ******************************************************************/
package com.ibm.portal.extension;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.PortletServiceUnavailableException;
import com.ibm.portal.services.contentmapping.exceptions.ContentMappingDataBackendException;
import com.ibm.portal.state.PortletStateManager;
import com.ibm.portal.state.accessors.exceptions.InvalidSelectionNodeIdException;
import com.ibm.portal.state.accessors.exceptions.StateNotInRequestException;
import com.ibm.portal.state.exceptions.CannotInstantiateAccessorException;
import com.ibm.portal.state.exceptions.StateManagerException;
import com.ibm.portal.state.exceptions.UnknownAccessorTypeException;
import com.ibm.portal.state.service.PortletStateManagerService;
import com.ibm.workplace.wcm.api.exceptions.DocumentIdCreationException;
import com.ibm.workplace.wcm.api.exceptions.DocumentRetrievalException;
import com.ibm.workplace.wcm.api.exceptions.IllegalDocumentTypeException;
import com.ibm.workplace.wcm.api.exceptions.OperationFailedException;
import com.ibm.workplace.wcm.api.exceptions.ServiceNotAvailableException;

/**
 * Helper class to determine the current WCM context. This class can only be used from portlet code. 
 * From portal code (e.g. theme)  please use PortalWCMContextHelper instead. 
 */

public class PortletWCMContextHelper extends WCMContextHelper {

   private final PortletStateManagerService stateManagerService;

   /**
    * Initializes the PortletWCMContextHelper. 
    * 
    * @throws NamingException 
    * @throws PortletServiceUnavailableException 
    */

   public PortletWCMContextHelper() throws NamingException, PortletServiceUnavailableException {

      // this initialization needs to be done only once
      final Context ctx = new InitialContext();
      PortletServiceHome psh = (PortletServiceHome) ctx.lookup(PortletStateManagerService.JNDI_NAME);
      stateManagerService = psh.getPortletService(PortletStateManagerService.class); 
   }

   /**
    * Gets the WCM context of the current page. It checks path info, public render parameter and 
    * content mapping in this order. A WCM context defined as private render parameter or 
    * preference of the Web Content Viewer portlet is NOT returned.
    *  
    * @param request PortletRequest
    * @param response PortletResponse
    * 
    * @return String representation of a content path.
    * 
    * @throws StateManagerException 
    * @throws IllegalDocumentTypeException 
    * @throws DocumentRetrievalException 
    * @throws DocumentIdCreationException 
    * @throws OperationFailedException 
    * @throws ServiceNotAvailableException 
    * @throws ContentMappingDataBackendException 
    * @throws StateNotInRequestException 
    * @throws InvalidSelectionNodeIdException 
    * @throws CannotInstantiateAccessorException 
    * @throws UnknownAccessorTypeException 
    */

   public String getCurrentWCMContext(final RenderRequest request, final RenderResponse response) 
         throws StateManagerException, UnknownAccessorTypeException, CannotInstantiateAccessorException, 
         InvalidSelectionNodeIdException, StateNotInRequestException, ContentMappingDataBackendException, 
         ServiceNotAvailableException, OperationFailedException, DocumentIdCreationException, 
         DocumentRetrievalException, IllegalDocumentTypeException {
      String contentPath = null;
      Map<String, String[]> publicParameter = request.getPublicParameterMap();
      // check path info
      if (publicParameter.containsKey("PATH_INFO")) {
         final String[] pathInfo = publicParameter.get("PATH_INFO");
         if (pathInfo != null && pathInfo.length > 0) {
            final PortletStateManager portletStateManager = stateManagerService.getPortletStateManager(request, response);
            String contentMapping = getContentMapping(portletStateManager, portletStateManager.getStateHolder());
            contentPath = assembleContentPath(contentMapping, pathInfo);
         }
      }
      if (contentPath == null) {
         // check public WCM context 
         contentPath = request.getParameter("PUBLIC_CONTEXT");
         if (contentPath == null) {
            // check content mapping
            final PortletStateManager portletStateManager = stateManagerService.getPortletStateManager(request, response);
            contentPath = getContentMapping(portletStateManager, portletStateManager.getStateHolder());
         }
      }
      return contentPath;
   }
}


Parent: Helper class samples for web content context