KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > chart > GetChart


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  */

11 package com.tonbeller.jpivot.chart;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileNotFoundException JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 /**
30  *
31  * @author arosselet
32  * @version
33  */

34 public class GetChart extends HttpServlet JavaDoc {
35     String JavaDoc basePath;
36     /** Initializes the servlet.
37      */

38     final static String JavaDoc fileNotFound="/WEB-INF/jpivot/img_not_found.gif";
39
40     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
41         super.init(config);
42     }
43     /** Destroys the servlet.
44      */

45     public void destroy() {
46
47     }
48
49     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
50      * @param request servlet request
51      * @param response servlet response
52      */

53     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
54     throws ServletException JavaDoc, IOException JavaDoc {
55
56         HttpSession JavaDoc session = request.getSession();
57         String JavaDoc filename = request.getParameter("filename");
58         System.out.println("GetChart called: filename="+filename);
59         if (filename == null) {
60             throw new ServletException JavaDoc("Parameter 'filename' must be supplied");
61         }
62
63         // Replace ".." with ""
64
// This is to prevent access to the rest of the file system
65
filename = searchReplace(filename, "..", "");
66
67         // Check the file exists
68
File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir"), filename);
69         if (!file.exists()) {
70             throw new ServletException JavaDoc("File '" + file.getAbsolutePath() + "' does not exist");
71         }
72          // Serve it up
73
sendTempFile(file, response);
74
75         return;
76
77     }
78     public static void sendTempFile(File JavaDoc file, HttpServletResponse JavaDoc response)
79             throws IOException JavaDoc, FileNotFoundException JavaDoc {
80
81         String JavaDoc mimeType = null;
82         String JavaDoc filename = file.getName();
83         if (filename.length() > 5) {
84             if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg") ||
85                 filename.substring(filename.length() - 5, filename.length()).equals(".jpg")) {
86                 mimeType = "image/jpeg";
87             }
88             else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) {
89                 mimeType = "image/png";
90             }
91             else if (filename.substring(filename.length() - 4, filename.length()).equals(".gif")) {
92                 mimeType = "image/gif";
93             }
94         }
95         sendTempFile(file, response, mimeType);
96     }
97
98     /**
99      * Binary streams the specified file to the HTTP response in 1KB chunks
100      *
101      * @param file The file to be streamed.
102      * @param response The HTTP response object.
103      * @param mimeType The mime type of the file, null allowed.
104      *
105      * @throws IOException if there is an I/O problem.
106      * @throws FileNotFoundException if the file is not found.
107      */

108     public static void sendTempFile(File JavaDoc file, HttpServletResponse JavaDoc response,
109                                     String JavaDoc mimeType)
110             throws IOException JavaDoc, FileNotFoundException JavaDoc {
111
112         if (file.exists()) {
113             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
114
115             // Set HTTP headers
116
if (mimeType != null) {
117                 response.setHeader("Content-Type", mimeType);
118             }
119             response.setHeader("Content-Length", String.valueOf(file.length()));
120             response.setDateHeader("Last-Modified", file.lastModified());
121             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(response.getOutputStream());
122             byte[] input = new byte[1024];
123             boolean eof = false;
124             while (!eof) {
125                 int length = bis.read(input);
126                 if (length == -1) {
127                     eof = true;
128                 } else {
129                     bos.write(input, 0, length);
130                 }
131             }
132             bos.flush();
133             bis.close();
134             bos.close();
135         }
136         else {
137             throw new FileNotFoundException JavaDoc(file.getAbsolutePath());
138         }
139         return;
140     }
141
142       /**
143      * Perform a search/replace operation on a String
144      * There are String methods to do this since (JDK 1.4)
145      *
146      * @param inputString the String to have the search/replace operation.
147      * @param searchString the search String.
148      * @param replaceString the replace String.
149      *
150      * @return the String with the replacements made.
151      */

152     public static String JavaDoc searchReplace(String JavaDoc inputString,
153                                        String JavaDoc searchString,
154                                        String JavaDoc replaceString) {
155
156         int i = inputString.indexOf(searchString);
157         if (i == -1) {
158             return inputString;
159         }
160
161         String JavaDoc r = "";
162         r += inputString.substring(0, i) + replaceString;
163         if (i + searchString.length() < inputString.length()) {
164             r += searchReplace(inputString.substring(i + searchString.length()),
165                                searchString,
166                                replaceString);
167         }
168
169         return r;
170     }
171
172     /** Handles the HTTP <code>GET</code> method.
173      * @param request servlet request
174      * @param response servlet response
175      */

176     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
177     throws ServletException JavaDoc, IOException JavaDoc {
178         processRequest(request, response);
179     }
180
181     /** Handles the HTTP <code>POST</code> method.
182      * @param request servlet request
183      * @param response servlet response
184      */

185     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
186     throws ServletException JavaDoc, IOException JavaDoc {
187         processRequest(request, response);
188     }
189
190     /** Returns a short description of the servlet.
191      */

192     public String JavaDoc getServletInfo() {
193         return "Short description";
194     }
195
196 }
197
Popular Tags