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.JRRtfExporter;
042: import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
043:
044: /**
045: * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
046: * @version $Id: RtfServlet.java 1757 2007-06-15 12:12:02Z shertage $
047: */
048: public class RtfServlet 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: JRRtfExporter exporter = new JRRtfExporter();
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/rtf");
082: response.setHeader("Content-Disposition",
083: "inline; filename=\"file.rtf\"");
084: response.setContentLength(fbos.size());
085: ServletOutputStream ouputStream = response
086: .getOutputStream();
087:
088: try {
089: fbos.writeData(ouputStream);
090: fbos.dispose();
091: ouputStream.flush();
092: } finally {
093: if (ouputStream != null) {
094: try {
095: ouputStream.close();
096: } catch (IOException ex) {
097: }
098: }
099: }
100: }
101: } catch (JRException e) {
102: throw new ServletException(e);
103: } finally {
104: fbos.close();
105: fbos.dispose();
106: }
107: // else
108: // {
109: // response.setContentType("text/html");
110: // PrintWriter out = response.getWriter();
111: // out.println("<html>");
112: // out.println("<body bgcolor=\"white\">");
113: // out.println("<span class=\"bold\">Empty response.</span>");
114: // out.println("</body>");
115: // out.println("</html>");
116: // }
117: } else {
118: response.setContentType("application/rtf");
119: response.setHeader("Content-Disposition",
120: "inline; filename=\"file.rtf\"");
121:
122: JRRtfExporter exporter = new JRRtfExporter();
123: exporter.setParameter(
124: JRExporterParameter.JASPER_PRINT_LIST,
125: jasperPrintList);
126:
127: OutputStream ouputStream = response.getOutputStream();
128: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
129: ouputStream);
130:
131: try {
132: exporter.exportReport();
133: } catch (JRException e) {
134: throw new ServletException(e);
135: } finally {
136: if (ouputStream != null) {
137: try {
138: ouputStream.close();
139: } catch (IOException ex) {
140: }
141: }
142: }
143: }
144: }
145:
146: }
|