1 11 package com.tonbeller.jpivot.jboss.portlet; 12 13 import java.io.BufferedInputStream ; 14 import java.io.BufferedOutputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 21 import javax.portlet.ActionResponse; 22 import javax.portlet.PortletException; 23 import javax.portlet.PortletResponse; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.log4j.Logger; 28 import org.jboss.portal.portlet.impl.ActionResponseImpl; 29 import org.jboss.portal.server.WindowContext; 30 import org.jboss.portlet.JBossActionResponse; 31 32 import com.tonbeller.jpivot.chart.GetChart; 33 34 public class GetChartImage { 35 private static Logger logger = Logger.getLogger(GetChartImage.class); 36 37 42 public static void processRequest( 43 HttpServletRequest request, 44 HttpServletResponse response, 45 WindowContext windowCtx, 46 ActionResponse portletResponse) throws PortletException { 47 48 String filename = request.getParameter("filename"); 49 logger.info("GetChart called: filename="+filename); 50 if (filename == null) { 51 throw new PortletException("Parameter 'filename' must be supplied"); 52 } 53 54 filename = GetChart.searchReplace(filename, "..", ""); 57 58 File file = new File (System.getProperty("java.io.tmpdir"), filename); 60 if (!file.exists()) { 61 throw new PortletException("File '" + file.getAbsolutePath() + "' does not exist"); 62 } 63 try { 64 sendTempFile(file, portletResponse, windowCtx, getMimeType(file)); 66 67 } catch (FileNotFoundException e) { 68 throw new PortletException(e); 69 } catch (IOException e) { 70 throw new PortletException(e); 71 } 72 } 73 74 public static String getMimeType(File file) { 75 String mimeType = null; 76 String filename = file.getName(); 77 if (filename.length() > 5) { 78 if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg") || 79 filename.substring(filename.length() - 5, filename.length()).equals(".jpg")) { 80 return "image/jpeg"; 81 } 82 else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) { 83 return "image/png"; 84 } 85 else if (filename.substring(filename.length() - 4, filename.length()).equals(".gif")) { 86 return "image/gif"; 87 } 88 } 89 return null; 90 } 91 92 103 public static void sendTempFile( 104 File file, 105 PortletResponse portletResponse, 106 WindowContext windowCtx, 107 String contentType) 108 throws IOException , FileNotFoundException { 109 110 if (!file.exists()) { 111 throw new FileNotFoundException (file.getAbsolutePath()); 112 } 113 114 BufferedInputStream bis = null; 115 ByteArrayOutputStream baos = null; 116 BufferedOutputStream bos = null; 117 try { 118 bis = new BufferedInputStream (new FileInputStream (file)); 119 baos = new ByteArrayOutputStream (16384); 120 bos = new BufferedOutputStream (baos); 121 byte[] input = new byte[1024]; 122 boolean eof = false; 123 while (!eof) { 124 int length = bis.read(input); 125 if (length == -1) { 126 eof = true; 127 } else { 128 bos.write(input, 0, length); 129 } 130 } 131 } finally { 132 if (bos != null) 133 bos.flush(); 134 if (bis != null) 135 bis.close(); 136 if (bos != null) 137 bos.close(); 138 } 139 140 JBossActionResponse responseImpl = (JBossActionResponse) portletResponse; 146 responseImpl.sendBytes(contentType, baos.toByteArray()); 147 } 148 149 } 150 | Popular Tags |