You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2002/02/03 02:42:16 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/java session.xsl

vgritsenko    02/02/02 17:42:16

  Modified:    src/java/org/apache/cocoon/components/language/markup/xsp/java
                        session.xsl
  Added:       src/java/org/apache/cocoon/components/language/markup/xsp
                        XSPSessionHelper.java
  Log:
  Move session code into XSPSessionHelper
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/XSPSessionHelper.java
  
  Index: XSPSessionHelper.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.cocoon.components.language.markup.xsp;
  
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.Session;
  
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.AttributesImpl;
  
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.List;
  import java.util.Map;
  
  /**
   * The <code>Session</code> object helper
   *
   * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
   * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/02/03 01:42:16 $
   */
  public class XSPSessionHelper {
  
      /**
       * FIXME (VG): Remove
       */
      private static final String URI = "http://apache.org/xsp/session/2.0";
      private static final String PREFIX = "session";
  
      /**
       * Sets the given session attribute value
       *
       * @param objectModel The Map objectModel
       * @param name The parameter name
       * @param content The parameter value
       */
      public static void setSessionAttribute(Map objectModel, String name, Object content) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          Session session = request.getSession(false);
          session.setAttribute(name, content);
      }
  
      /**
       * Return the given session attribute value or a user-provided default if
       * none was specified.
       *
       * @param objectModel The Map objectModel
       * @param name The parameter name
       */
      public static Object getSessionAttribute(Map objectModel, String name) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          Session session = request.getSession(false);
          return session.getAttribute(name);
      }
  
      /**
       * Return the given session attribute value or a user-provided default if
       * none was specified.
       *
       * @param objectModel The Map objectModel
       * @param name The parameter name
       * @param defaultValue Value to substitute in absence of a parameter value
       */
      public static Object getSessionAttribute(Map objectModel, String name,
                                               Object defaultValue) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          Session session = request.getSession(false);
          Object value = null;
  
          if (session != null) {
              value = session.getAttribute(name);
          }
  
          if (value == null) {
              value = defaultValue;
          }
  
          return value;
      }
  
      /**
       * Get the session attribute names.
       *
       * @param objectModel The Map objectModel
       */
      public static List getSessionAttributeNames(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          ArrayList v = new ArrayList();
          Enumeration e = request.getSession().getAttributeNames();
  
          while (e.hasMoreElements()) {
              v.add(e.nextElement());
          }
          return v;
      }
  
      /**
       * Output the given session attribute value or a user-provided default if
       * none was specified.
       *
       * @param objectModel The Map objectModel
       * @param contentHandler The SAX content handler
       * @param name The parameter name
       * @param defaultValue Value to substitute in absence of a parameter value
       * @exception SAXException If a SAX error occurs
       */
      public static void getSessionAttribute(Map objectModel, ContentHandler contentHandler,
                                             String name, Object defaultValue)
              throws SAXException {
          AttributesImpl attr = new AttributesImpl();
          XSPObjectHelper.addAttribute(attr, "name", name);
  
          XSPObjectHelper.elementData(
                  URI, PREFIX, contentHandler, "attribute",
                  (String) getSessionAttribute(objectModel, name, defaultValue),
                  attr
          );
      }
  
      /**
       * Get the session creation time
       *
       * @param objectModel The Map objectModel
       */
      public static long getSessionCreationTime(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          return request.getSession().getCreationTime();
      }
  
      /**
       * Get the session id
       *
       * @param objectModel The Map objectModel
       */
      public static String getSessionId(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          return request.getSession().getId();
      }
  
      /**
       * Get the session last accessed time
       *
       * @param objectModel The Map objectModel
       */
      public static long getSessionLastAccessedTime(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          return request.getSession().getLastAccessedTime();
      }
  
      /**
       * Get the session max inactive interval
       *
       * @param objectModel The Map objectModel
       */
      public static long getSessionMaxInactiveInterval(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          return request.getSession().getMaxInactiveInterval();
      }
  
      /**
       * Set the session max inactive interval
       * @param objectModel The Map objectModel
       * @param interval max inactive interval
       */
      public static void setSessionMaxInactiveInterval(Map objectModel, int interval) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          request.getSession().setMaxInactiveInterval(interval);
      }
  
      /**
       * Invalidate the session
       * @param objectModel The Map objectModel
       */
      public static void invalidateSession(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          request.getSession().invalidate();
      }
  
      /**
       * Checks the isNew flag
       * @param objectModel The Map objectModel
       */
      public static boolean isSessionNew(Map objectModel) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          return request.getSession().isNew();
      }
  
      /**
       * Remove the specified attribute
       * @param objectModel The Map objectModel
       * @param name The parameter name
       */
      public static void removeSessionAttribute(Map objectModel, String name) {
          Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
          request.getSession().removeAttribute(name);
      }
  }
  
  
  
  1.3       +29 -30    xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/java/session.xsl
  
  Index: session.xsl
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/java/session.xsl,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- session.xsl	12 Jan 2002 04:08:13 -0000	1.2
  +++ session.xsl	3 Feb 2002 01:42:16 -0000	1.3
  @@ -1,6 +1,5 @@
   <?xml version="1.0"?>
   <!--
  -
    ============================================================================
                      The Apache Software License, Version 1.1
    ============================================================================
  @@ -57,8 +56,8 @@
   <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xsp="http://apache.org/xsp"
  -  xmlns:session="http://apache.org/xsp/session/2.0"
  ->
  +  xmlns:session="http://apache.org/xsp/session/2.0">
  +
     <!-- *** ServletSession Templates *** -->
   
     <xsl:template match="session:get-attribute">
  @@ -81,7 +80,7 @@
         <xsl:when test="$as = 'xml'">
           <xsp:element name="session:attribute">
             <xsp:expr>
  -            XSPRequestHelper.getSessionAttribute(objectModel,
  +            XSPSessionHelper.getSessionAttribute(objectModel,
               String.valueOf(<xsl:copy-of select="$name"/>),
               <xsl:copy-of select="$default"/>)
             </xsp:expr>
  @@ -89,7 +88,7 @@
         </xsl:when>
         <xsl:when test="$as = 'object'">
           <xsp:expr>
  -          XSPRequestHelper.getSessionAttribute(objectModel,
  +          XSPSessionHelper.getSessionAttribute(objectModel,
             String.valueOf(<xsl:copy-of select="$name"/>),
             <xsl:copy-of select="$default"/>)
           </xsp:expr>
  @@ -107,7 +106,7 @@
       <xsl:choose>
         <xsl:when test="$as = 'xml'">
           <xsp:logic>
  -          List v = XSPRequestHelper.getSessionAttributeNames(objectModel);
  +          List v = XSPSessionHelper.getSessionAttributeNames(objectModel);
           </xsp:logic>
   
           <xsp:element name="session:attribute-names">
  @@ -123,7 +122,7 @@
   
         <xsl:when test="$as = 'array'">
           <xsp:expr>
  -          XSPRequestHelper.getSessionAttributeNames(objectModel)
  +          XSPSessionHelper.getSessionAttributeNames(objectModel)
           </xsp:expr>
         </xsl:when>
       </xsl:choose>
  @@ -140,18 +139,18 @@
         <xsl:when test="$as = 'xml'">
           <xsp:element name="session:creation-time">
             <xsp:expr>
  -            new Date(XSPRequestHelper.getSessionCreationTime(objectModel))
  +            new Date(XSPSessionHelper.getSessionCreationTime(objectModel))
             </xsp:expr>
           </xsp:element>
         </xsl:when>
         <xsl:when test="$as = 'string'">
           <xsp:expr>
  -          new Date(XSPRequestHelper.getSessionCreationTime(objectModel))
  +          new Date(XSPSessionHelper.getSessionCreationTime(objectModel))
           </xsp:expr>
         </xsl:when>
         <xsl:when test="$as = 'long'">
           <xsp:expr>
  -          XSPRequestHelper.getSessionCreationTime(objectModel)
  +          XSPSessionHelper.getSessionCreationTime(objectModel)
           </xsp:expr>
         </xsl:when>
       </xsl:choose>
  @@ -167,11 +166,11 @@
       <xsl:choose>
         <xsl:when test="$as = 'xml'">
           <xsp:element name="session:id">
  -          <xsp:expr>XSPRequestHelper.getSessionId(objectModel)</xsp:expr>
  +          <xsp:expr>XSPSessionHelper.getSessionId(objectModel)</xsp:expr>
           </xsp:element>
         </xsl:when>
         <xsl:when test="$as = 'string'">
  -        <xsp:expr>XSPRequestHelper.getSessionId(objectModel)</xsp:expr>
  +        <xsp:expr>XSPSessionHelper.getSessionId(objectModel)</xsp:expr>
         </xsl:when>
       </xsl:choose>
     </xsl:template>
  @@ -187,18 +186,18 @@
         <xsl:when test="$as = 'xml'">
           <xsp:element name="session:last-accessed-time">
             <xsp:expr>
  -            new Date(XSPRequestHelper.getSessionLastAccessedTime(objectModel))
  +            new Date(XSPSessionHelper.getSessionLastAccessedTime(objectModel))
             </xsp:expr>
           </xsp:element>
         </xsl:when>
         <xsl:when test="$as = 'string'">
           <xsp:expr>
  -          new Date(XSPRequestHelper.getSessionLastAccessedTime(objectModel))
  +          new Date(XSPSessionHelper.getSessionLastAccessedTime(objectModel))
           </xsp:expr>
         </xsl:when>
         <xsl:when test="$as = 'long'">
           <xsp:expr>
  -          XSPRequestHelper.getSessionLastAccessedTime(objectModel)
  +          XSPSessionHelper.getSessionLastAccessedTime(objectModel)
           </xsp:expr>
         </xsl:when>
       </xsl:choose>
  @@ -215,19 +214,19 @@
         <xsl:when test="$as = 'xml'">
           <xsp:element name="session:max-inactive-interval">
             <xsp:expr>
  -            XSPRequestHelper.getSessionMaxInactiveInterval(objectModel)
  +            XSPSessionHelper.getSessionMaxInactiveInterval(objectModel)
             </xsp:expr>
           </xsp:element>
         </xsl:when>
         <xsl:when test="$as = 'string'">
           <xsp:expr>
  -          String.valueOf(XSPRequestHelper.getSessionMaxInactiveInterval(
  +          String.valueOf(XSPSessionHelper.getSessionMaxInactiveInterval(
             objectModel))
           </xsp:expr>
         </xsl:when>
         <xsl:when test="$as = 'int'">
           <xsp:expr>
  -          XSPRequestHelper.getSessionMaxInactiveInterval(objectModel)
  +          XSPSessionHelper.getSessionMaxInactiveInterval(objectModel)
           </xsp:expr>
         </xsl:when>
       </xsl:choose>
  @@ -235,7 +234,7 @@
   
     <xsl:template match="session:invalidate">
       <xsp:logic>
  -      XSPRequestHelper.invalidateSession(objectModel);
  +      XSPSessionHelper.invalidateSession(objectModel);
       </xsp:logic>
     </xsl:template>
   
  @@ -250,16 +249,16 @@
         <xsl:choose>
           <xsl:when test="$as = 'xml'">
             <xsp:element name="session:is-new">
  -            <xsp:expr>XSPRequestHelper.isSessionNew(objectModel)</xsp:expr>
  +            <xsp:expr>XSPSessionHelper.isSessionNew(objectModel)</xsp:expr>
             </xsp:element>
           </xsl:when>
           <xsl:when test="$as = 'string'">
             <xsp:expr>
  -            String.valueOf(XSPRequestHelper.isSessionNew(objectModel))
  +            String.valueOf(XSPSessionHelper.isSessionNew(objectModel))
             </xsp:expr>
           </xsl:when>
           <xsl:when test="$as = 'boolean'">
  -          <xsp:expr>XSPRequestHelper.isSessionNew(objectModel)</xsp:expr>
  +          <xsp:expr>XSPSessionHelper.isSessionNew(objectModel)</xsp:expr>
           </xsl:when>
         </xsl:choose>
       </xsp:expr>
  @@ -271,7 +270,7 @@
       </xsl:variable>
   
       <xsp:logic>
  -      XSPRequestHelper.removeSessionAttribute(objectModel,
  +      XSPSessionHelper.removeSessionAttribute(objectModel,
         String.valueOf(<xsl:copy-of select="$name"/>)
         );
       </xsp:logic>
  @@ -289,7 +288,7 @@
       </xsl:variable>
   
       <xsp:logic>
  -      XSPRequestHelper.setSessionAttribute(objectModel,
  +      XSPSessionHelper.setSessionAttribute(objectModel,
         String.valueOf(<xsl:copy-of select="$name"/>),
         <xsl:copy-of select="$content"/>
         );
  @@ -299,7 +298,7 @@
     <xsl:template match="session:set-max-inactive-interval">
       <xsl:variable name="interval">
         <xsl:choose>
  -        <xsl:when test="@interval">"<xsl:value-of 
  +        <xsl:when test="@interval">"<xsl:value-of
               select="@interval"/>"</xsl:when>
           <xsl:when test="session:interval">
             <xsl:call-template name="get-nested-content">
  @@ -319,13 +318,13 @@
   
     <!-- encode an URL with the session ID -->
     <xsl:template match="session:encode-url">
  -    <xsl:variable name="href">"<xsl:value-of 
  +    <xsl:variable name="href">"<xsl:value-of
           select="@href"/>"</xsl:variable>
   
       <xsp:element name="a">
         <xsp:attribute name="href">
           <xsp:expr>
  -          XSPResponseHelper.encodeURL(objectModel, 
  +          XSPResponseHelper.encodeURL(objectModel,
             String.valueOf(<xsl:copy-of select="$href"/>))
           </xsp:expr>
         </xsp:attribute>
  @@ -335,11 +334,11 @@
   
     <!-- encode an URL with the session ID as a form-->
     <xsl:template match="session:form-encode-url">
  -    <xsl:variable name="action">"<xsl:value-of 
  +    <xsl:variable name="action">"<xsl:value-of
           select="@action"/>"</xsl:variable>
  -    <xsl:variable name="method">"<xsl:value-of 
  +    <xsl:variable name="method">"<xsl:value-of
           select="@method"/>"</xsl:variable>
  -    <xsl:variable name="onsubmit">"<xsl:value-of 
  +    <xsl:variable name="onsubmit">"<xsl:value-of
           select="@onsubmit"/>"</xsl:variable>
   
       <xsp:element name="form">
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org