KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > jboss > portlet > GetChartImage


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) 2005 SHERMAN WOOD.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  */

11 package com.tonbeller.jpivot.jboss.portlet;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.portlet.ActionResponse;
22 import javax.portlet.PortletException;
23 import javax.portlet.PortletResponse;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
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     /** Processes requests for displaying generated chart images.
38      * @param request servlet request
39      * @param response servlet response
40      * @throws PortletException
41      */

42     public static void processRequest(
43             HttpServletRequest JavaDoc request,
44             HttpServletResponse JavaDoc response,
45             WindowContext windowCtx,
46             ActionResponse portletResponse) throws PortletException {
47
48         String JavaDoc 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         // Replace ".." with ""
55
// This is to prevent access to the rest of the file system
56
filename = GetChart.searchReplace(filename, "..", "");
57
58         // Check the file exists
59
File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir"), filename);
60         if (!file.exists()) {
61             throw new PortletException("File '" + file.getAbsolutePath() + "' does not exist");
62         }
63         try {
64             // Serve it up
65
sendTempFile(file, portletResponse, windowCtx, getMimeType(file));
66             
67         } catch (FileNotFoundException JavaDoc e) {
68             throw new PortletException(e);
69         } catch (IOException JavaDoc e) {
70             throw new PortletException(e);
71         }
72     }
73
74     public static String JavaDoc getMimeType(File JavaDoc file) {
75         String JavaDoc mimeType = null;
76         String JavaDoc 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     /**
93     * Binary streams the specified file to the portlet response
94     *
95     * @param file The file to be streamed.
96     * @param response The HTTP response object.
97     * @param windowCtx the portlet window content
98     * @param contentType The mime type of the file, null allowed.
99     *
100     * @throws IOException if there is an I/O problem.
101     * @throws FileNotFoundException if the file is not found.
102     */

103     public static void sendTempFile(
104             File JavaDoc file,
105             PortletResponse portletResponse,
106             WindowContext windowCtx,
107             String JavaDoc contentType)
108         throws IOException JavaDoc, FileNotFoundException JavaDoc {
109     
110         if (!file.exists()) {
111             throw new FileNotFoundException JavaDoc(file.getAbsolutePath());
112         }
113
114         BufferedInputStream JavaDoc bis = null;
115         ByteArrayOutputStream JavaDoc baos = null;
116         BufferedOutputStream JavaDoc bos = null;
117         try {
118             bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
119             baos = new ByteArrayOutputStream JavaDoc(16384);
120             bos = new BufferedOutputStream JavaDoc(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         // This will only work in JBoss
141
//ActionResponseImpl responseImpl = (ActionResponseImpl) portletResponse;
142
//org.jboss.portal.server.output.Result portletResult = new org.jboss.portal.server.output.StreamResult(windowCtx, contentType, baos.toByteArray());
143
//responseImpl.setResult(portletResult);
144
// This will only work in JBoss
145
JBossActionResponse responseImpl = (JBossActionResponse) portletResponse;
146         responseImpl.sendBytes(contentType, baos.toByteArray());
147    }
148
149 }
150
Popular Tags