1 17 18 package org.pentaho.ui.servlet; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.pentaho.core.system.PentahoSystem; 30 import org.pentaho.messages.Messages; 31 32 public class ShowTestResult extends ServletBase { 33 34 37 private static final long serialVersionUID = -360244121499172556L; 38 39 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException , IOException { 40 doPost(arg0, arg1); 41 } 42 43 private static final Log logger = LogFactory.getLog(ShowTestResult.class); 44 45 public Log getLogger() { 46 return logger; 47 } 48 49 protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 50 51 getPentahoSession(req); 52 String fileName = req.getParameter("file"); String extension = req.getParameter("ext"); 55 if (fileName == null) { 56 error(Messages.getErrorString("TESTRESULT.ERROR_0001_FILE_PARAMETER_EMPTY")); return; 58 } 59 fileName += extension; 60 String filePath; 61 if (fileName.charAt(0) != '/' && fileName.charAt(0) != '\\') { 62 filePath = PentahoSystem.getApplicationContext().getFileOutputPath("test/tmp/") + fileName; } else { 64 filePath = PentahoSystem.getApplicationContext().getFileOutputPath("test/tmp") + fileName; } 66 67 File file = new File (filePath); 68 if ((!file.exists()) || (!file.isFile())) { 69 error(Messages.getErrorString("IMAGE.ERROR_0002_FILE_NOT_FOUND", fileName)); return; 71 } 72 73 res.setContentLength((int) file.length()); 74 75 String mimeType = getServletContext().getMimeType(fileName); 76 if ((null != mimeType) && (mimeType.length() > 0)) { 77 res.setContentType(mimeType); 78 } 79 80 FileInputStream in = new FileInputStream (file); 82 OutputStream out = res.getOutputStream(); 83 84 try { 85 byte[] buf = new byte[1024]; 87 int count = 0; 88 while ((count = in.read(buf)) >= 0) { 89 out.write(buf, 0, count); 90 } 91 } finally { 92 in.close(); 93 out.close(); 94 } 95 96 } 97 98 } 99 | Popular Tags |