KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > servlet > DisplayChart


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------
27  * DisplayChart.java
28  * -----------------
29  * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
30  *
31  * Original Author: Richard Atkinson;
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: DisplayChart.java,v 1.2 2005/03/09 14:07:16 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Aug-2002 : Version 1;
39  * 09-Mar-2005 : Added facility to serve up "one time" charts - see
40  * ServletUtilities.java (DG);
41  *
42  */

43
44 package org.jfree.chart.servlet;
45
46 import java.io.File JavaDoc;
47 import java.io.IOException JavaDoc;
48
49 import javax.servlet.ServletException JavaDoc;
50 import javax.servlet.http.HttpServlet JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53 import javax.servlet.http.HttpSession JavaDoc;
54
55 /**
56  * Servlet used for streaming charts to the client browser from the temporary
57  * directory. You need to add this servlet and mapping to your deployment
58  * descriptor (web.xml) in order to get it to work. The syntax is as follows:
59  * <xmp>
60  * <servlet>
61  * <servlet-name>DisplayChart</servlet-name>
62  * <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
63  * </servlet>
64  * <servlet-mapping>
65  * <servlet-name>DisplayChart</servlet-name>
66  * <url-pattern>/servlet/DisplayChart</url-pattern>
67  * </servlet-mapping>
68  * </xmp>
69  *
70  * @author Richard Atkinson
71  */

72 public class DisplayChart extends HttpServlet JavaDoc {
73
74     /**
75      * Default constructor.
76      */

77     public DisplayChart() {
78         super();
79     }
80
81     /**
82      * Init method.
83      *
84      * @throws ServletException never.
85      */

86     public void init() throws ServletException JavaDoc {
87         return;
88     }
89
90     /**
91      * Service method.
92      *
93      * @param request the request.
94      * @param response the response.
95      *
96      * @throws ServletException ??.
97      * @throws IOException ??.
98      */

99     public void service(HttpServletRequest JavaDoc request,
100                         HttpServletResponse JavaDoc response)
101             throws ServletException JavaDoc, IOException JavaDoc {
102
103         HttpSession JavaDoc session = request.getSession();
104         String JavaDoc filename = request.getParameter("filename");
105
106         if (filename == null) {
107             throw new ServletException JavaDoc("Parameter 'filename' must be supplied");
108         }
109
110         // Replace ".." with ""
111
// This is to prevent access to the rest of the file system
112
filename = ServletUtilities.searchReplace(filename, "..", "");
113
114         // Check the file exists
115
File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir"), filename);
116         if (!file.exists()) {
117             throw new ServletException JavaDoc(
118                 "File '" + file.getAbsolutePath() + "' does not exist"
119             );
120         }
121
122         // Check that the graph being served was created by the current user
123
// or that it begins with "public"
124
boolean isChartInUserList = false;
125         ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute(
126             "JFreeChart_Deleter"
127         );
128         if (chartDeleter != null) {
129             isChartInUserList = chartDeleter.isChartAvailable(filename);
130         }
131
132         boolean isChartPublic = false;
133         if (filename.length() >= 6) {
134             if (filename.substring(0, 6).equals("public")) {
135                 isChartPublic = true;
136             }
137         }
138         
139         boolean isOneTimeChart = false;
140         if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) {
141             isOneTimeChart = true;
142         }
143
144         if (isChartInUserList || isChartPublic || isOneTimeChart) {
145             // Serve it up
146
ServletUtilities.sendTempFile(file, response);
147             if (isOneTimeChart) {
148                 file.delete();
149             }
150         }
151         else {
152             throw new ServletException JavaDoc("Chart image not found");
153         }
154         return;
155     }
156
157 }
158
Popular Tags