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