KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > OlapGetChart


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.war;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.URI JavaDoc;
31 import java.net.URISyntaxException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35
36 import javax.servlet.ServletConfig JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.http.HttpServlet JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * @author swood
47  *
48  */

49 public class OlapGetChart extends HttpServlet JavaDoc {
50     private static final Log logger = LogFactory.getLog(OlapGetChart.class);
51
52     String JavaDoc basePath;
53
54     /**
55      * TODO Should initialize fileNotFound image
56      */

57     public OlapGetChart() {
58         super();
59     }
60
61     /** Initializes the servlet.
62      */

63     final static String JavaDoc fileNotFound="/WEB-INF/jpivot/img_not_found.gif";
64     
65     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
66         super.init(config);
67     }
68     
69     /** Destroys the servlet.
70      */

71     public void destroy() {
72         
73     }
74     
75     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
76      * @param request servlet request
77      * @param response servlet response
78      */

79     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80             throws ServletException JavaDoc, IOException JavaDoc {
81
82         String JavaDoc filename = request.getParameter("filename");
83         logger.debug("GetChart called: filename="+filename);
84         if (filename == null) {
85             throw new ServletException JavaDoc("Parameter 'filename' must be supplied");
86         }
87
88         // Replace ".." with ""
89
// This is to prevent access to the rest of the file system
90
filename = searchReplace(filename, "..", "");
91
92         // Check the file exists
93
File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir"), filename);
94         if (!file.exists()) {
95             logger.error("File '" + file.getAbsolutePath() + "' does not exist");
96             URL JavaDoc url = this.getClass().getResource(fileNotFound);
97             
98             URI JavaDoc uri;
99             try {
100                 uri = new URI JavaDoc(url.toString());
101             } catch (URISyntaxException JavaDoc e) {
102                 throw new ServletException JavaDoc(e);
103             }
104             file = new File JavaDoc(uri.getPath());
105         } else {
106             // Serve it up
107
sendTempFile(file, response);
108         }
109        
110         return;
111
112     }
113     public static void sendTempFile(File JavaDoc file, HttpServletResponse JavaDoc response)
114             throws IOException JavaDoc, FileNotFoundException JavaDoc {
115
116         String JavaDoc mimeType = null;
117         String JavaDoc filename = file.getName();
118         if (filename.length() > 5) {
119             if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg") ||
120                 filename.substring(filename.length() - 5, filename.length()).equals(".jpg")) {
121                 mimeType = "image/jpeg";
122             }
123             else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) {
124                 mimeType = "image/png";
125             }
126             else if (filename.substring(filename.length() - 4, filename.length()).equals(".gif")) {
127                 mimeType = "image/gif";
128             }
129         }
130         sendTempFile(file, response, mimeType);
131     }
132
133     /**
134      * Binary streams the specified file to the HTTP response in 1KB chunks
135      *
136      * @param file The file to be streamed.
137      * @param response The HTTP response object.
138      * @param mimeType The mime type of the file, null allowed.
139      *
140      * @throws IOException if there is an I/O problem.
141      * @throws FileNotFoundException if the file is not found.
142      */

143     public static void sendTempFile(File JavaDoc file, HttpServletResponse JavaDoc response,
144                                     String JavaDoc mimeType)
145             throws IOException JavaDoc, FileNotFoundException JavaDoc {
146
147         if (file.exists()) {
148             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
149
150             // Set HTTP headers
151
if (mimeType != null) {
152                 response.setHeader("Content-Type", mimeType);
153             }
154             response.setHeader("Content-Length", String.valueOf(file.length()));
155             SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss z");
156             response.setHeader("Last-Modified", sdf.format(new Date JavaDoc(file.lastModified())));
157
158             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(response.getOutputStream());
159             byte[] input = new byte[1024];
160             boolean eof = false;
161             while (!eof) {
162                 int length = bis.read(input);
163                 if (length == -1) {
164                     eof = true;
165                 } else {
166                     bos.write(input, 0, length);
167                 }
168             }
169             bos.flush();
170             bis.close();
171             bos.close();
172         }
173         else {
174             throw new FileNotFoundException JavaDoc(file.getAbsolutePath());
175         }
176         return;
177     }
178
179       /**
180      * Perform a search/replace operation on a String
181      * There are String methods to do this since (JDK 1.4)
182      *
183      * @param inputString the String to have the search/replace operation.
184      * @param searchString the search String.
185      * @param replaceString the replace String.
186      *
187      * @return the String with the replacements made.
188      */

189     public static String JavaDoc searchReplace(String JavaDoc inputString,
190                                        String JavaDoc searchString,
191                                        String JavaDoc replaceString) {
192
193         int i = inputString.indexOf(searchString);
194         if (i == -1) {
195             return inputString;
196         }
197
198         String JavaDoc r = "";
199         r += inputString.substring(0, i) + replaceString;
200         if (i + searchString.length() < inputString.length()) {
201             r += searchReplace(inputString.substring(i + searchString.length()),
202                                searchString,
203                                replaceString);
204         }
205
206         return r;
207     }
208    
209     /** Handles the HTTP <code>GET</code> method.
210      * @param request servlet request
211      * @param response servlet response
212      */

213     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
214     throws ServletException JavaDoc, IOException JavaDoc {
215         processRequest(request, response);
216     }
217     
218     /** Handles the HTTP <code>POST</code> method.
219      * @param request servlet request
220      * @param response servlet response
221      */

222     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
223     throws ServletException JavaDoc, IOException JavaDoc {
224         processRequest(request, response);
225     }
226     
227     /** Returns a short description of the servlet.
228      */

229     public String JavaDoc getServletInfo() {
230         return "Serve up charts for OLAP";
231     }
232
233 }
234
Popular Tags