1 21 22 package com.jaspersoft.jasperserver.war; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.net.URI ; 31 import java.net.URISyntaxException ; 32 import java.net.URL ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Date ; 35 36 import javax.servlet.ServletConfig ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 49 public class OlapGetChart extends HttpServlet { 50 private static final Log logger = LogFactory.getLog(OlapGetChart.class); 51 52 String basePath; 53 54 57 public OlapGetChart() { 58 super(); 59 } 60 61 63 final static String fileNotFound="/WEB-INF/jpivot/img_not_found.gif"; 64 65 public void init(ServletConfig config) throws ServletException { 66 super.init(config); 67 } 68 69 71 public void destroy() { 72 73 } 74 75 79 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 80 throws ServletException , IOException { 81 82 String filename = request.getParameter("filename"); 83 logger.debug("GetChart called: filename="+filename); 84 if (filename == null) { 85 throw new ServletException ("Parameter 'filename' must be supplied"); 86 } 87 88 filename = searchReplace(filename, "..", ""); 91 92 File file = new File (System.getProperty("java.io.tmpdir"), filename); 94 if (!file.exists()) { 95 logger.error("File '" + file.getAbsolutePath() + "' does not exist"); 96 URL url = this.getClass().getResource(fileNotFound); 97 98 URI uri; 99 try { 100 uri = new URI (url.toString()); 101 } catch (URISyntaxException e) { 102 throw new ServletException (e); 103 } 104 file = new File (uri.getPath()); 105 } else { 106 sendTempFile(file, response); 108 } 109 110 return; 111 112 } 113 public static void sendTempFile(File file, HttpServletResponse response) 114 throws IOException , FileNotFoundException { 115 116 String mimeType = null; 117 String 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 143 public static void sendTempFile(File file, HttpServletResponse response, 144 String mimeType) 145 throws IOException , FileNotFoundException { 146 147 if (file.exists()) { 148 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); 149 150 if (mimeType != null) { 152 response.setHeader("Content-Type", mimeType); 153 } 154 response.setHeader("Content-Length", String.valueOf(file.length())); 155 SimpleDateFormat sdf = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss z"); 156 response.setHeader("Last-Modified", sdf.format(new Date (file.lastModified()))); 157 158 BufferedOutputStream bos = new BufferedOutputStream (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 (file.getAbsolutePath()); 175 } 176 return; 177 } 178 179 189 public static String searchReplace(String inputString, 190 String searchString, 191 String replaceString) { 192 193 int i = inputString.indexOf(searchString); 194 if (i == -1) { 195 return inputString; 196 } 197 198 String 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 213 protected void doGet(HttpServletRequest request, HttpServletResponse response) 214 throws ServletException , IOException { 215 processRequest(request, response); 216 } 217 218 222 protected void doPost(HttpServletRequest request, HttpServletResponse response) 223 throws ServletException , IOException { 224 processRequest(request, response); 225 } 226 227 229 public String getServletInfo() { 230 return "Serve up charts for OLAP"; 231 } 232 233 } 234 | Popular Tags |