View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.util;
17  
18  import java.util.Enumeration;
19  
20  import javax.portlet.PortletSession;
21  import javax.portlet.PortletSessionUtil;
22  
23  /***
24   * PortletWindowUtils
25   * 
26   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
27   * @version $Id: PortletWindowUtils.java 510753 2007-02-23 01:28:50Z ate $
28   */
29  public class PortletWindowUtils
30  {
31      public static String PORTLET_WINDOW_ID = "org.apache.portals.bridges.util.portlet_window_id";
32      
33      /***
34       * Return the unique identification for the portlet window as assigned by the portal/portlet-container.
35       * <br/>
36       * This method makes use of the PortletSession to determine the window id as specified by the Portlet Specification 1.0, PLT.15.3,
37       * as well as stores the determined value under the {@link #PORTLET_WINDOW_ID} in the portlet scope session.
38       * 
39       * @param session the current PortletSession
40       * @return the unique identification of the portlet window
41       */
42      public static String getPortletWindowId(PortletSession session)
43      {
44          String portletWindowId = (String)session.getAttribute(PORTLET_WINDOW_ID);
45          if ( portletWindowId == null )
46          {
47              synchronized (session)
48              {
49                  Double value = new Double(Math.random());
50                  session.setAttribute(PORTLET_WINDOW_ID, value);
51                  Enumeration names = session.getAttributeNames(PortletSession.APPLICATION_SCOPE);
52                  while (names.hasMoreElements())
53                  {
54                      String name = (String)names.nextElement();
55                      if (PortletSessionUtil.decodeAttributeName(name).equals(PORTLET_WINDOW_ID) && value.equals(session.getAttribute(name,PortletSession.APPLICATION_SCOPE)) )
56                      {
57                          portletWindowId = name.substring("javax.portlet.p.".length(),name.indexOf('?'));
58                          session.setAttribute(PORTLET_WINDOW_ID, portletWindowId);
59                          break;
60                      }                    
61                  }
62              }
63          }
64          return portletWindowId;
65      }
66      
67      /***
68       * Returns the name an attribute is (or will be) encoded in the PortletSession APPLICATION_SCOPE.
69       * @param session PortletSession
70       * @param attributeName the attribute name to encode
71       */
72      public static String getApplicationScopeSessionAttributeName(PortletSession session, String attributeName)
73      {
74          return getApplicationScopeSessionAttributeName(getPortletWindowId(session),attributeName);
75      }
76  
77      /***
78       * Returns the name an attribute is (or will be) encoded in the PortletSession APPLICATION_SCOPE.
79       * 
80       * @param portletWindowId the unique portlet window identification retrieved from {@link #getPortletWindowId(PortletSession)}.
81       * @param attributeName the attribute name to encode
82       */
83      public static String getApplicationScopeSessionAttributeName(String portletWindowId, String attributeName)
84      {
85          return "javax.portlet.p."+portletWindowId+"?"+attributeName;
86      }
87  }