View Javadoc

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