Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2005, 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-2005, by Richard Atkinson and Contributors. 31: * 32: * Original Author: Richard Atkinson; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * $Id: DisplayChart.java,v 1.2.2.1 2005/10/25 20:58:06 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 19-Aug-2002 : Version 1; 40: * 09-Mar-2005 : Added facility to serve up "one time" charts - see 41: * ServletUtilities.java (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: * @author Richard Atkinson 72: */ 73: public class DisplayChart extends HttpServlet { 74: 75: /** 76: * Default constructor. 77: */ 78: public DisplayChart() { 79: super(); 80: } 81: 82: /** 83: * Init method. 84: * 85: * @throws ServletException never. 86: */ 87: public void init() throws ServletException { 88: return; 89: } 90: 91: /** 92: * Service method. 93: * 94: * @param request the request. 95: * @param response the response. 96: * 97: * @throws ServletException ??. 98: * @throws IOException ??. 99: */ 100: public void service(HttpServletRequest request, 101: HttpServletResponse response) 102: throws ServletException, IOException { 103: 104: HttpSession session = request.getSession(); 105: String filename = request.getParameter("filename"); 106: 107: if (filename == null) { 108: throw new ServletException("Parameter 'filename' must be supplied"); 109: } 110: 111: // Replace ".." with "" 112: // This is to prevent access to the rest of the file system 113: filename = ServletUtilities.searchReplace(filename, "..", ""); 114: 115: // Check the file exists 116: File file = new File(System.getProperty("java.io.tmpdir"), filename); 117: if (!file.exists()) { 118: throw new ServletException( 119: "File '" + file.getAbsolutePath() + "' does not exist" 120: ); 121: } 122: 123: // Check that the graph being served was created by the current user 124: // or that it begins with "public" 125: boolean isChartInUserList = false; 126: ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute( 127: "JFreeChart_Deleter" 128: ); 129: if (chartDeleter != null) { 130: isChartInUserList = chartDeleter.isChartAvailable(filename); 131: } 132: 133: boolean isChartPublic = false; 134: if (filename.length() >= 6) { 135: if (filename.substring(0, 6).equals("public")) { 136: isChartPublic = true; 137: } 138: } 139: 140: boolean isOneTimeChart = false; 141: if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) { 142: isOneTimeChart = true; 143: } 144: 145: if (isChartInUserList || isChartPublic || isOneTimeChart) { 146: // Serve it up 147: ServletUtilities.sendTempFile(file, response); 148: if (isOneTimeChart) { 149: file.delete(); 150: } 151: } 152: else { 153: throw new ServletException("Chart image not found"); 154: } 155: return; 156: } 157: 158: }