portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Sean Taylor <da...@bluesunrise.com>
Subject Re: get portlet instance's fragment id
Date Wed, 06 Aug 2008 18:10:12 GMT

On Jul 30, 2008, at 10:26 PM, xiaodingxiang wrote:

>
> How can I get a portlet instance’s fragment-id in the session, If  
> fragment-id is the instance’s only mark..


(1) one approach:

ContentFragment contentFragment = (ContentFragment)  
portletRequest 
.getAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE);
contentFragment.getId();

(2) another approach that does not use fragments:

Compile against:
portlet-bridges-common >= 1.0.2 (available at runtime from shared/lib  
classloader)

but do not put this jar in your portlet app's WEB-INF/lib directory

import  org.apache.portals.bridges.util.PortletWindowUtils

String id =   PortletWindoUtils.getPortletWindowId(session);

Note: this method uses a JSR-168 compliant "trick", JSR-286 (Portlet  
API 2.0) will provide a formal API method for it.

Here is the source in case you are interested to see how it works:

package org.apache.portals.bridges.util;

import java.util.Enumeration;

import javax.portlet.PortletSession;
import javax.portlet.PortletSessionUtil;

/**
  * PortletWindowUtils
  *
  * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
  * @version $Id: PortletWindowUtils.java 517068 2007-03-12 01:44:37Z  
ate $
  */
public class PortletWindowUtils
{
     public static String PORTLET_WINDOW_ID =  
"org.apache.portals.bridges.util.portlet_window_id";

     /**
      * Return the unique identification for the portlet window as  
assigned by the portal/portlet-container.
      * <br/>
      * This method makes use of the PortletSession to determine the  
window id as specified by the Portlet Specification 1.0, PLT.15.3,
      * as well as stores the determined value under the {@link  
#PORTLET_WINDOW_ID} in the portlet scope session.
      *
      * @param session the current PortletSession
      * @return the unique identification of the portlet window
      */
     public static String getPortletWindowId(PortletSession session)
     {
         String portletWindowId =  
(String)session.getAttribute(PORTLET_WINDOW_ID);
         if ( portletWindowId == null )
         {
             synchronized (session)
             {
                 Double value = new Double(Math.random());
                 session.setAttribute(PORTLET_WINDOW_ID, value);
                 Enumeration names =  
session.getAttributeNames(PortletSession.APPLICATION_SCOPE);
                 while (names.hasMoreElements())
                 {
                     String name = (String)names.nextElement();
                     if  
(PortletSessionUtil 
.decodeAttributeName(name).equals(PORTLET_WINDOW_ID) &&  
value 
.equals(session.getAttribute(name,PortletSession.APPLICATION_SCOPE)) )
                     {
                         portletWindowId =  
name.substring("javax.portlet.p.".length(),name.indexOf('?'));
                         session.setAttribute(PORTLET_WINDOW_ID,  
portletWindowId);
                         break;
                     }
                 }
             }
         }
         return portletWindowId;
     }

     /**
      * Returns the name an attribute is (or will be) encoded in the  
PortletSession APPLICATION_SCOPE.
      * @param session PortletSession
      * @param attributeName the attribute name to encode
      */
     public static String  
getApplicationScopeSessionAttributeName(PortletSession session, String  
attributeName)
     {
         return  
getApplicationScopeSessionAttributeName 
(getPortletWindowId(session),attributeName);
     }

     /**
      * Returns the name an attribute is (or will be) encoded in the  
PortletSession APPLICATION_SCOPE.
      *
      * @param portletWindowId the unique portlet window  
identification retrieved from {@link  
#getPortletWindowId(PortletSession)}.
      * @param attributeName the attribute name to encode
      */
     public static String  
getApplicationScopeSessionAttributeName(String portletWindowId, String  
attributeName)
     {
         return "javax.portlet.p."+portletWindowId+"?"+attributeName;
     }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message