java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/LongTermSessionStorage.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/LongTermSessionStorage.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of the License, or (at your option) any later version.
    1.16 + *
    1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.19 + * See the GNU Lesser General Public License for more details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.23 + * Boston, MA 02111-1307, USA.
    1.24 + */
    1.25 +package de.laures.cewolf.storage;
    1.26 +
    1.27 +import java.io.Serializable;
    1.28 +
    1.29 +import javax.servlet.ServletContext;
    1.30 +import javax.servlet.http.HttpServletRequest;
    1.31 +import javax.servlet.http.HttpSession;
    1.32 +import javax.servlet.jsp.PageContext;
    1.33 +
    1.34 +import de.laures.cewolf.CewolfException;
    1.35 +import de.laures.cewolf.ChartImage;
    1.36 +import de.laures.cewolf.Storage;
    1.37 +import de.laures.cewolf.taglib.util.KeyGenerator;
    1.38 +
    1.39 +/**
    1.40 + * Storage stores images in session, but expires them after a certain time. 
    1.41 + * This expiration time defaults to 300 seconds, and can be changed by adding 
    1.42 + * the timeout="xxx" parameter to <cewolf:img> and <cewolf:legend> tags.
    1.43 + * 
    1.44 + * @author brianf
    1.45 + */
    1.46 +public class LongTermSessionStorage implements Storage
    1.47 +{
    1.48 + 
    1.49 +  public final String getKey( ChartImage cid )
    1.50 +  {
    1.51 +    return String.valueOf(KeyGenerator.generateKey((Serializable) cid));
    1.52 +  }
    1.53 +
    1.54 +  /*
    1.55 +   * (non-Javadoc)
    1.56 +   * 
    1.57 +   * @see de.laures.cewolf.Storage#storeChartImage(de.laures.cewolf.ChartImage,
    1.58 +   *      javax.servlet.jsp.PageContext)
    1.59 +   */
    1.60 +  public String storeChartImage( ChartImage chartImage, PageContext pageContext ) throws CewolfException
    1.61 +  {
    1.62 +    HttpSession session = pageContext.getSession();
    1.63 +    SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
    1.64 +    if ( ssg == null )
    1.65 +    {
    1.66 +      ssg = new SessionStorageGroup();
    1.67 +      session.setAttribute("CewolfCharts", ssg);
    1.68 +    }
    1.69 +    String cid = getKey(chartImage);
    1.70 +    SessionStorageItem ssi = new SessionStorageItem(chartImage, cid, chartImage.getTimeoutTime());
    1.71 +    ssg.put(cid, ssi);
    1.72 +
    1.73 +    return cid;
    1.74 +  }
    1.75 +
    1.76 +  /*
    1.77 +   * (non-Javadoc)
    1.78 +   * 
    1.79 +   * @see de.laures.cewolf.Storage#getChartImage(java.lang.String,
    1.80 +   *      javax.servlet.http.HttpServletRequest)
    1.81 +   */
    1.82 +  public ChartImage getChartImage( String id, HttpServletRequest request )
    1.83 +  {
    1.84 +    HttpSession session = request.getSession();
    1.85 +    ChartImage chart = null;
    1.86 +    SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
    1.87 +    if ( ssg != null )
    1.88 +    {
    1.89 +      SessionStorageItem ssi = (SessionStorageItem) ssg.get(id);
    1.90 +      if ( ssi != null )
    1.91 +      {
    1.92 +        chart = ssi.getChart();
    1.93 +      }
    1.94 +    }
    1.95 +
    1.96 +    return chart;
    1.97 +  }
    1.98 +  /*
    1.99 +   * (non-Javadoc)
   1.100 +   * 
   1.101 +   * @see de.laures.cewolf.Storage#init(javax.servlet.ServletContext)
   1.102 +   */
   1.103 +  public void init( ServletContext servletContext ) throws CewolfException {
   1.104 +  }
   1.105 +
   1.106 +  /**
   1.107 +   * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
   1.108 +   */
   1.109 +  public String removeChartImage(String cid, HttpServletRequest request) throws CewolfException {
   1.110 +	  HttpSession session = request.getSession();
   1.111 +	  // No session exit
   1.112 +	  if (session == null)
   1.113 +	  {
   1.114 +		  return cid;
   1.115 +	  }
   1.116 +	  SessionStorageGroup ssg = (SessionStorageGroup) session.getAttribute("CewolfCharts");
   1.117 +	  if ( ssg == null )
   1.118 +	  {
   1.119 +		  // No group exit
   1.120 +		  return cid;
   1.121 +	  }
   1.122 +	  ssg.remove(cid);
   1.123 +	  return cid;  
   1.124 +  }
   1.125 +}
   1.126 \ No newline at end of file