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.oasis.JROdtExporter;
042: import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: OdtServlet.java 1757 2007-06-15 12:12:02Z shertage $
047: */
048: public class OdtServlet 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: JROdtExporter exporter = new JROdtExporter();
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
082: .setContentType("application/vnd.oasis.opendocument.text");
083: // response.setHeader("Content-Disposition", "inline; filename=\"file.odt\"");
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:
108: // else
109: // {
110: // response.setContentType("text/html");
111: // PrintWriter out = response.getWriter();
112: // out.println("<html>");
113: // out.println("<body bgcolor=\"white\">");
114: // out.println("<span class=\"bold\">Empty response.</span>");
115: // out.println("</body>");
116: // out.println("</html>");
117: // }
118: } else {
119: response
120: .setContentType("application/vnd.oasis.opendocument.text");
121: //response.setHeader("Content-Disposition", "inline; filename=\"file.odt\"");
122:
123: JROdtExporter exporter = new JROdtExporter();
124: exporter.setParameter(
125: JRExporterParameter.JASPER_PRINT_LIST,
126: jasperPrintList);
127:
128: OutputStream ouputStream = response.getOutputStream();
129: exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
130: ouputStream);
131:
132: try {
133: exporter.exportReport();
134: } catch (JRException e) {
135: throw new ServletException(e);
136: } finally {
137: if (ouputStream != null) {
138: try {
139: ouputStream.close();
140: } catch (IOException ex) {
141: }
142: }
143: }
144: }
145: }
146:
147: }
|