1 53 54 package org.jfree.chart.servlet; 55 56 import java.io.BufferedInputStream ; 57 import java.io.BufferedOutputStream ; 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.FileNotFoundException ; 61 import java.io.IOException ; 62 import java.text.SimpleDateFormat ; 63 import java.util.Date ; 64 import java.util.TimeZone ; 65 66 import javax.servlet.http.HttpServletResponse ; 67 import javax.servlet.http.HttpSession ; 68 69 import org.jfree.chart.ChartRenderingInfo; 70 import org.jfree.chart.ChartUtilities; 71 import org.jfree.chart.JFreeChart; 72 73 78 public class ServletUtilities { 79 80 81 private static String tempFilePrefix = "jfreechart-"; 82 83 84 private static String tempOneTimeFilePrefix = "jfreechart-onetime-"; 85 86 91 public static String getTempFilePrefix() { 92 return ServletUtilities.tempFilePrefix; 93 } 94 95 100 public static void setTempFilePrefix(String prefix) { 101 if (prefix == null) { 102 throw new IllegalArgumentException ("Null 'prefix' argument."); 103 } 104 ServletUtilities.tempFilePrefix = prefix; 105 } 106 107 113 public static String getTempOneTimeFilePrefix() { 114 return ServletUtilities.tempOneTimeFilePrefix; 115 } 116 117 123 public static void setTempOneTimeFilePrefix(String prefix) { 124 if (prefix == null) { 125 throw new IllegalArgumentException ("Null 'prefix' argument."); 126 } 127 ServletUtilities.tempOneTimeFilePrefix = prefix; 128 } 129 130 145 public static String saveChartAsPNG(JFreeChart chart, int width, int height, 146 HttpSession session) 147 throws IOException { 148 149 return ServletUtilities.saveChartAsPNG( 150 chart, width, height, null, session 151 ); 152 153 } 154 155 174 public static String saveChartAsPNG(JFreeChart chart, int width, int height, 175 ChartRenderingInfo info, 176 HttpSession session) 177 throws IOException { 178 179 if (chart == null) { 180 throw new IllegalArgumentException ("Null 'chart' argument."); 181 } 182 ServletUtilities.createTempDir(); 183 String prefix = ServletUtilities.tempFilePrefix; 184 if (session == null) { 185 prefix = ServletUtilities.tempOneTimeFilePrefix; 186 } 187 File tempFile = File.createTempFile( 188 prefix, ".png", new File (System.getProperty("java.io.tmpdir")) 189 ); 190 ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info); 191 if (session != null) { 192 ServletUtilities.registerChartForDeletion(tempFile, session); 193 } 194 return tempFile.getName(); 195 196 } 197 198 213 public static String saveChartAsJPEG(JFreeChart chart, int width, 214 int height, HttpSession session) 215 throws IOException { 216 217 return ServletUtilities.saveChartAsJPEG( 218 chart, width, height, null, session 219 ); 220 221 } 222 223 241 public static String saveChartAsJPEG(JFreeChart chart, int width, 242 int height, ChartRenderingInfo info, 243 HttpSession session) 244 throws IOException { 245 246 if (chart == null) { 247 throw new IllegalArgumentException ("Null 'chart' argument."); 248 } 249 250 ServletUtilities.createTempDir(); 251 String prefix = ServletUtilities.tempFilePrefix; 252 if (session == null) { 253 prefix = ServletUtilities.tempOneTimeFilePrefix; 254 } 255 File tempFile = File.createTempFile( 256 prefix, ".jpeg", new File (System.getProperty("java.io.tmpdir")) 257 ); 258 ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info); 259 if (session != null) { 260 ServletUtilities.registerChartForDeletion(tempFile, session); 261 } 262 return tempFile.getName(); 263 264 } 265 266 275 protected static void createTempDir() { 276 String tempDirName = System.getProperty("java.io.tmpdir"); 277 if (tempDirName == null) { 278 throw new RuntimeException ( 279 "Temporary directory system property (java.io.tmpdir) is null." 280 ); 281 } 282 283 File tempDir = new File (tempDirName); 285 if (!tempDir.exists()) { 286 tempDir.mkdirs(); 287 } 288 } 289 290 298 protected static void registerChartForDeletion(File tempFile, 299 HttpSession session) { 300 301 if (session != null) { 303 ChartDeleter chartDeleter 304 = (ChartDeleter) session.getAttribute("JFreeChart_Deleter"); 305 if (chartDeleter == null) { 306 chartDeleter = new ChartDeleter(); 307 session.setAttribute("JFreeChart_Deleter", chartDeleter); 308 } 309 chartDeleter.addChart(tempFile.getName()); 310 } 311 else { 312 System.out.println("Session is null - chart will not be deleted"); 313 } 314 } 315 316 325 public static void sendTempFile(String filename, 326 HttpServletResponse response) 327 throws IOException { 328 329 File file = new File (System.getProperty("java.io.tmpdir"), filename); 330 ServletUtilities.sendTempFile(file, response); 331 } 332 333 341 public static void sendTempFile(File file, HttpServletResponse response) 342 throws IOException { 343 344 String mimeType = null; 345 String filename = file.getName(); 346 if (filename.length() > 5) { 347 if (filename.substring(filename.length() - 5, 348 filename.length()).equals(".jpeg")) { 349 mimeType = "image/jpeg"; 350 } 351 else if (filename.substring(filename.length() - 4, 352 filename.length()).equals(".png")) { 353 mimeType = "image/png"; 354 } 355 } 356 ServletUtilities.sendTempFile(file, response, mimeType); 357 } 358 359 368 public static void sendTempFile(File file, HttpServletResponse response, 369 String mimeType) throws IOException { 370 371 if (file.exists()) { 372 BufferedInputStream bis = new BufferedInputStream ( 373 new FileInputStream (file) 374 ); 375 376 if (mimeType != null) { 378 response.setHeader("Content-Type", mimeType); 379 } 380 response.setHeader("Content-Length", String.valueOf(file.length())); 381 SimpleDateFormat sdf = new SimpleDateFormat ( 382 "EEE, dd MMM yyyy HH:mm:ss z" 383 ); 384 sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 385 response.setHeader( 386 "Last-Modified", sdf.format(new Date (file.lastModified())) 387 ); 388 389 BufferedOutputStream bos = new BufferedOutputStream ( 390 response.getOutputStream() 391 ); 392 byte[] input = new byte[1024]; 393 boolean eof = false; 394 while (!eof) { 395 int length = bis.read(input); 396 if (length == -1) { 397 eof = true; 398 } 399 else { 400 bos.write(input, 0, length); 401 } 402 } 403 bos.flush(); 404 bis.close(); 405 bos.close(); 406 } 407 else { 408 throw new FileNotFoundException (file.getAbsolutePath()); 409 } 410 return; 411 } 412 413 423 public static String searchReplace(String inputString, 424 String searchString, 425 String replaceString) { 426 427 int i = inputString.indexOf(searchString); 428 if (i == -1) { 429 return inputString; 430 } 431 432 String r = ""; 433 r += inputString.substring(0, i) + replaceString; 434 if (i + searchString.length() < inputString.length()) { 435 r += searchReplace( 436 inputString.substring(i + searchString.length()), 437 searchString, replaceString 438 ); 439 } 440 441 return r; 442 } 443 444 } 445 | Popular Tags |