KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > j2ee > servlets > ImageServlet


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.j2ee.servlets;
29
30 import java.awt.Dimension JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import net.sf.jasperreports.engine.JRElement;
40 import net.sf.jasperreports.engine.JRException;
41 import net.sf.jasperreports.engine.JRImage;
42 import net.sf.jasperreports.engine.JRImageRenderer;
43 import net.sf.jasperreports.engine.JRPrintImage;
44 import net.sf.jasperreports.engine.JRRenderable;
45 import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
46 import net.sf.jasperreports.engine.export.JRHtmlExporter;
47 import net.sf.jasperreports.engine.util.JRTypeSniffer;
48
49
50 /**
51  * @author Teodor Danciu (teodord@users.sourceforge.net)
52  * @version $Id: ImageServlet.java 1508 2006-11-28 19:09:17 +0200 (Tue, 28 Nov 2006) teodord $
53  */

54 public class ImageServlet extends BaseHttpServlet
55 {
56
57
58     /**
59      *
60      */

61     public static final String JavaDoc IMAGE_NAME_REQUEST_PARAMETER = "image";
62
63             
64     /**
65      *
66      */

67     public void service(
68         HttpServletRequest JavaDoc request,
69         HttpServletResponse JavaDoc response
70         ) throws IOException JavaDoc, ServletException JavaDoc
71     {
72         byte[] imageData = null;
73         String JavaDoc imageMimeType = null;
74
75         String JavaDoc imageName = request.getParameter(IMAGE_NAME_REQUEST_PARAMETER);
76         if ("px".equals(imageName))
77         {
78             try
79             {
80                 JRRenderable pxRenderer =
81                     JRImageRenderer.getInstance(
82                         "net/sf/jasperreports/engine/images/pixel.GIF",
83                         JRImage.ON_ERROR_TYPE_ERROR
84                         );
85                 imageData = pxRenderer.getImageData();
86             }
87             catch (JRException e)
88             {
89                 throw new ServletException JavaDoc(e);
90             }
91         }
92         else
93         {
94             List JavaDoc jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
95             
96             if (jasperPrintList == null)
97             {
98                 throw new ServletException JavaDoc("No JasperPrint documents found on the HTTP session.");
99             }
100             
101             JRPrintImage image = JRHtmlExporter.getImage(jasperPrintList, imageName);
102             
103             JRRenderable renderer = image.getRenderer();
104             if (renderer.getType() == JRRenderable.TYPE_SVG)
105             {
106                 renderer =
107                     new JRWrappingSvgRenderer(
108                         renderer,
109                         new Dimension JavaDoc(image.getWidth(), image.getHeight()),
110                         JRElement.MODE_OPAQUE == image.getMode() ? image.getBackcolor() : null
111                         );
112             }
113
114             imageMimeType = JRTypeSniffer.getImageMimeType(renderer.getImageType());
115             
116             try
117             {
118                 imageData = renderer.getImageData();
119             }
120             catch (JRException e)
121             {
122                 throw new ServletException JavaDoc(e);
123             }
124         }
125
126         if (imageData != null && imageData.length > 0)
127         {
128             if (imageMimeType != null)
129             {
130                 response.setHeader("Content-Type", imageMimeType);
131             }
132             response.setContentLength(imageData.length);
133             ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
134             ouputStream.write(imageData, 0, imageData.length);
135             ouputStream.flush();
136             ouputStream.close();
137         }
138     }
139
140
141 }
142
Popular Tags