KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > actions > image > ImageLoaderAction


1 /*
2  * Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.actions.image;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.ServletOutputStream JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import com.opensymphony.webwork.ServletActionContext;
29 import com.opensymphony.xwork.ActionContext;
30 import com.opensymphony.xwork.ActionSupport;
31
32 import org.apache.log4j.Logger;
33
34 /**
35  * This action is used to return images for HTML pages generated
36  * by the JasperReports HTML export process.
37  *
38  * Each image generated by the export process is placed in the
39  * session scoped IMAGES_MAP. The generated HTML page calls this
40  * action to retreive the image from the map.
41  *
42  * Another possible solution is to output images generated by the
43  * export to a directory and have the HTML page refer to the directory.
44  * This could cause a problem with multiple users because the image names
45  * generated are not unique.
46  *
47  * Both solutions are not ideal and are the result of the JasperReports
48  * HTML export process. Rewriting the export to only generate dynamic
49  * images and not images already available as files would probably speed
50  * up the process but is not possible with the current api.
51  *
52  */

53
54 public class ImageLoaderAction extends ActionSupport
55 {
56     protected static Logger log = Logger.getLogger(ImageLoaderAction.class);
57
58     private String JavaDoc imageName;
59
60     public String JavaDoc execute()
61     {
62         Map JavaDoc imagesMap = (Map JavaDoc) ActionContext.getContext().getSession().get("IMAGES_MAP");
63
64         if (imagesMap != null)
65         {
66             if (imageName != null)
67             {
68                 byte[] imageData = (byte[]) imagesMap.get(imageName);
69
70                 HttpServletResponse JavaDoc response =
71                     ServletActionContext.getResponse();
72
73                 try
74                 {
75                     response.setContentLength(imageData.length);
76                     ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
77                     ouputStream.write(imageData, 0, imageData.length);
78                     ouputStream.flush();
79                     ouputStream.close();
80                 }
81                 catch(IOException JavaDoc ioe)
82                 {
83                     log.warn(ioe.toString());
84                 }
85             }
86         }
87
88         return NONE;
89     }
90
91     public String JavaDoc getImageName()
92     {
93         return imageName;
94     }
95
96     public void setImageName(String JavaDoc imageName)
97     {
98         this.imageName = imageName;
99     }
100
101 }
Popular Tags