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.io.IOException;
031: import java.io.OutputStream;
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.JRException;
040: import net.sf.jasperreports.engine.JRExporterParameter;
041: import net.sf.jasperreports.engine.export.JRPdfExporter;
042: import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: PdfServlet.java 1757 2007-06-15 12:12:02Z shertage $
047: */
048: public class PdfServlet extends BaseHttpServlet {
049:
050: /**
051: *
052: */
053: public void service(HttpServletRequest request,
054: HttpServletResponse response) throws IOException,
055: ServletException {
056: List jasperPrintList = BaseHttpServlet
057: .getJasperPrintList(request);
058:
059: if (jasperPrintList == null) {
060: throw new ServletException(
061: "No JasperPrint documents found on the HTTP session.");
062: }
063:
064: Boolean isBuffered = Boolean
065: .valueOf(request
066: .getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER));
067: if (isBuffered.booleanValue()) {
068: FileBufferedOutputStream fbos = new FileBufferedOutputStream();
069: JRPdfExporter exporter = new JRPdfExporter();
070: exporter.setParameter(
071: JRExporterParameter.JASPER_PRINT_LIST,
072: jasperPrintList);
073: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
074: fbos);
075:
076: try {
077: exporter.exportReport();
078: fbos.close();
079:
080: if (fbos.size() > 0) {
081: response.setContentType("application/pdf");
082: response.setContentLength(fbos.size());
083: ServletOutputStream ouputStream = response
084: .getOutputStream();
085:
086: try {
087: fbos.writeData(ouputStream);
088: fbos.dispose();
089: ouputStream.flush();
090: } finally {
091: if (ouputStream != null) {
092: try {
093: ouputStream.close();
094: } catch (IOException ex) {
095: }
096: }
097: }
098: }
099: } catch (JRException e) {
100: throw new ServletException(e);
101: } finally {
102: fbos.close();
103: fbos.dispose();
104: }
105:
106: // else
107: // {
108: // response.setContentType("text/html");
109: // PrintWriter out = response.getWriter();
110: // out.println("<html>");
111: // out.println("<body bgcolor=\"white\">");
112: // out.println("<span class=\"bold\">Empty response.</span>");
113: // out.println("</body>");
114: // out.println("</html>");
115: // }
116: } else {
117: response.setContentType("application/pdf");
118:
119: JRPdfExporter exporter = new JRPdfExporter();
120: exporter.setParameter(
121: JRExporterParameter.JASPER_PRINT_LIST,
122: jasperPrintList);
123:
124: OutputStream ouputStream = response.getOutputStream();
125: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
126: ouputStream);
127:
128: try {
129: exporter.exportReport();
130: } catch (JRException e) {
131: throw new ServletException(e);
132: } finally {
133: if (ouputStream != null) {
134: try {
135: ouputStream.close();
136: } catch (IOException ex) {
137: }
138: }
139: }
140: }
141: }
142:
143: }
|