java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/LongTermSessionStorage.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
     1 /* ================================================================
     2  * Cewolf : Chart enabling Web Objects Framework
     3  * ================================================================
     4  *
     5  * Project Info:  http://cewolf.sourceforge.net
     6  * Project Lead:  Guido Laures (guido@laures.de);
     7  *
     8  * (C) Copyright 2002, by Guido Laures
     9  *
    10  * This library is free software; you can redistribute it and/or modify it under the terms
    11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
    12  * either version 2.1 of the License, or (at your option) any later version.
    13  *
    14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  * See the GNU Lesser General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU Lesser General Public License along with this
    19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    20  * Boston, MA 02111-1307, USA.
    21  */
    22 package de.laures.cewolf.storage;
    23 
    24 import java.io.Serializable;
    25 
    26 import javax.servlet.ServletContext;
    27 import javax.servlet.http.HttpServletRequest;
    28 import javax.servlet.http.HttpSession;
    29 import javax.servlet.jsp.PageContext;
    30 
    31 import de.laures.cewolf.CewolfException;
    32 import de.laures.cewolf.ChartImage;
    33 import de.laures.cewolf.Storage;
    34 import de.laures.cewolf.taglib.util.KeyGenerator;
    35 
    36 /**
    37  * Storage stores images in session, but expires them after a certain time. 
    38  * This expiration time defaults to 300 seconds, and can be changed by adding 
    39  * the timeout="xxx" parameter to <cewolf:img> and <cewolf:legend> tags.
    40  * 
    41  * @author brianf
    42  */
    43 public class LongTermSessionStorage implements Storage
    44 {
    45  
    46   public final String getKey( ChartImage cid )
    47   {
    48     return String.valueOf(KeyGenerator.generateKey((Serializable) cid));
    49   }
    50 
    51   /*
    52    * (non-Javadoc)
    53    * 
    54    * @see de.laures.cewolf.Storage#storeChartImage(de.laures.cewolf.ChartImage,
    55    *      javax.servlet.jsp.PageContext)
    56    */
    57   public String storeChartImage( ChartImage chartImage, PageContext pageContext ) throws CewolfException
    58   {
    59     HttpSession session = pageContext.getSession();
    60     SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
    61     if ( ssg == null )
    62     {
    63       ssg = new SessionStorageGroup();
    64       session.setAttribute("CewolfCharts", ssg);
    65     }
    66     String cid = getKey(chartImage);
    67     SessionStorageItem ssi = new SessionStorageItem(chartImage, cid, chartImage.getTimeoutTime());
    68     ssg.put(cid, ssi);
    69 
    70     return cid;
    71   }
    72 
    73   /*
    74    * (non-Javadoc)
    75    * 
    76    * @see de.laures.cewolf.Storage#getChartImage(java.lang.String,
    77    *      javax.servlet.http.HttpServletRequest)
    78    */
    79   public ChartImage getChartImage( String id, HttpServletRequest request )
    80   {
    81     HttpSession session = request.getSession();
    82     ChartImage chart = null;
    83     SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
    84     if ( ssg != null )
    85     {
    86       SessionStorageItem ssi = (SessionStorageItem) ssg.get(id);
    87       if ( ssi != null )
    88       {
    89         chart = ssi.getChart();
    90       }
    91     }
    92 
    93     return chart;
    94   }
    95   /*
    96    * (non-Javadoc)
    97    * 
    98    * @see de.laures.cewolf.Storage#init(javax.servlet.ServletContext)
    99    */
   100   public void init( ServletContext servletContext ) throws CewolfException {
   101   }
   102 
   103   /**
   104    * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
   105    */
   106   public String removeChartImage(String cid, HttpServletRequest request) throws CewolfException {
   107 	  HttpSession session = request.getSession();
   108 	  // No session exit
   109 	  if (session == null)
   110 	  {
   111 		  return cid;
   112 	  }
   113 	  SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
   114 	  if ( ssg == null )
   115 	  {
   116 		  // No group exit
   117 		  return cid;
   118 	  }
   119 	  ssg.remove(cid);
   120 	  return cid;  
   121   }
   122 }