Source for org.jfree.chart.servlet.DisplayChart

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -----------------
  28:  * DisplayChart.java
  29:  * -----------------
  30:  * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Aug-2002 : Version 1;
  38:  * 09-Mar-2005 : Added facility to serve up "one time" charts - see 
  39:  *               ServletUtilities.java (DG);
  40:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  41:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.chart.servlet;
  46: 
  47: import java.io.File;
  48: import java.io.IOException;
  49: 
  50: import javax.servlet.ServletException;
  51: import javax.servlet.http.HttpServlet;
  52: import javax.servlet.http.HttpServletRequest;
  53: import javax.servlet.http.HttpServletResponse;
  54: import javax.servlet.http.HttpSession;
  55: 
  56: /**
  57:  * Servlet used for streaming charts to the client browser from the temporary
  58:  * directory.  You need to add this servlet and mapping to your deployment 
  59:  * descriptor (web.xml) in order to get it to work.  The syntax is as follows:
  60:  * <xmp>
  61:  * <servlet>
  62:  *    <servlet-name>DisplayChart</servlet-name>
  63:  *    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  64:  * </servlet>
  65:  * <servlet-mapping>
  66:  *     <servlet-name>DisplayChart</servlet-name>
  67:  *     <url-pattern>/servlet/DisplayChart</url-pattern>
  68:  * </servlet-mapping>
  69:  * </xmp>
  70:  */
  71: public class DisplayChart extends HttpServlet {
  72: 
  73:     /**
  74:      * Default constructor.
  75:      */
  76:     public DisplayChart() {
  77:         super();
  78:     }
  79: 
  80:     /**
  81:      * Init method.
  82:      *
  83:      * @throws ServletException never.
  84:      */
  85:     public void init() throws ServletException {
  86:         return;
  87:     }
  88: 
  89:     /**
  90:      * Service method.
  91:      *
  92:      * @param request  the request.
  93:      * @param response  the response.
  94:      *
  95:      * @throws ServletException ??.
  96:      * @throws IOException ??.
  97:      */
  98:     public void service(HttpServletRequest request, 
  99:                         HttpServletResponse response)
 100:             throws ServletException, IOException {
 101: 
 102:         HttpSession session = request.getSession();
 103:         String filename = request.getParameter("filename");
 104: 
 105:         if (filename == null) {
 106:             throw new ServletException("Parameter 'filename' must be supplied");
 107:         }
 108: 
 109:         //  Replace ".." with ""
 110:         //  This is to prevent access to the rest of the file system
 111:         filename = ServletUtilities.searchReplace(filename, "..", "");
 112: 
 113:         //  Check the file exists
 114:         File file = new File(System.getProperty("java.io.tmpdir"), filename);
 115:         if (!file.exists()) {
 116:             throw new ServletException("File '" + file.getAbsolutePath() 
 117:                     + "' does not exist");
 118:         }
 119: 
 120:         //  Check that the graph being served was created by the current user
 121:         //  or that it begins with "public"
 122:         boolean isChartInUserList = false;
 123:         ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute(
 124:                 "JFreeChart_Deleter");
 125:         if (chartDeleter != null) {
 126:             isChartInUserList = chartDeleter.isChartAvailable(filename);
 127:         }
 128: 
 129:         boolean isChartPublic = false;
 130:         if (filename.length() >= 6) {
 131:             if (filename.substring(0, 6).equals("public")) {
 132:                 isChartPublic = true;
 133:             }
 134:         }
 135:         
 136:         boolean isOneTimeChart = false;
 137:         if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) {
 138:             isOneTimeChart = true;   
 139:         }
 140: 
 141:         if (isChartInUserList || isChartPublic || isOneTimeChart) {
 142:             //  Serve it up
 143:             ServletUtilities.sendTempFile(file, response);
 144:             if (isOneTimeChart) {
 145:                 file.delete();   
 146:             }
 147:         }
 148:         else {
 149:             throw new ServletException("Chart image not found");
 150:         }
 151:         return;
 152:     }
 153: 
 154: }