001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.j2ee.servlets;
029:
030: import java.awt.Dimension;
031: import java.io.IOException;
032: import java.util.List;
033:
034: import javax.servlet.ServletException;
035: import javax.servlet.ServletOutputStream;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import net.sf.jasperreports.engine.JRElement;
040: import net.sf.jasperreports.engine.JRException;
041: import net.sf.jasperreports.engine.JRImageRenderer;
042: import net.sf.jasperreports.engine.JRPrintImage;
043: import net.sf.jasperreports.engine.JRRenderable;
044: import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
045: import net.sf.jasperreports.engine.export.JRHtmlExporter;
046: import net.sf.jasperreports.engine.util.JRTypeSniffer;
047:
048: /**
049: * @author Teodor Danciu (teodord@users.sourceforge.net)
050: * @version $Id: ImageServlet.java 1811 2007-08-13 19:52:07Z teodord $
051: */
052: public class ImageServlet extends BaseHttpServlet {
053:
054: /**
055: *
056: */
057: public static final String IMAGE_NAME_REQUEST_PARAMETER = "image";
058:
059: /**
060: *
061: */
062: public void service(HttpServletRequest request,
063: HttpServletResponse response) throws IOException,
064: ServletException {
065: byte[] imageData = null;
066: String imageMimeType = null;
067:
068: String imageName = request
069: .getParameter(IMAGE_NAME_REQUEST_PARAMETER);
070: if ("px".equals(imageName)) {
071: try {
072: JRRenderable pxRenderer = JRImageRenderer
073: .getInstance("net/sf/jasperreports/engine/images/pixel.GIF");
074: imageData = pxRenderer.getImageData();
075: } catch (JRException e) {
076: throw new ServletException(e);
077: }
078: } else {
079: List jasperPrintList = BaseHttpServlet
080: .getJasperPrintList(request);
081:
082: if (jasperPrintList == null) {
083: throw new ServletException(
084: "No JasperPrint documents found on the HTTP session.");
085: }
086:
087: JRPrintImage image = JRHtmlExporter.getImage(
088: jasperPrintList, imageName);
089:
090: JRRenderable renderer = image.getRenderer();
091: if (renderer.getType() == JRRenderable.TYPE_SVG) {
092: renderer = new JRWrappingSvgRenderer(
093: renderer,
094: new Dimension(image.getWidth(), image
095: .getHeight()),
096: JRElement.MODE_OPAQUE == image.getMode() ? image
097: .getBackcolor()
098: : null);
099: }
100:
101: imageMimeType = JRTypeSniffer.getImageMimeType(renderer
102: .getImageType());
103:
104: try {
105: imageData = renderer.getImageData();
106: } catch (JRException e) {
107: throw new ServletException(e);
108: }
109: }
110:
111: if (imageData != null && imageData.length > 0) {
112: if (imageMimeType != null) {
113: response.setHeader("Content-Type", imageMimeType);
114: }
115: response.setContentLength(imageData.length);
116: ServletOutputStream ouputStream = response
117: .getOutputStream();
118: ouputStream.write(imageData, 0, imageData.length);
119: ouputStream.flush();
120: ouputStream.close();
121: }
122: }
123:
124: }
|