KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > j2ee > servlets > RtfServlet


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.j2ee.servlets;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletOutputStream JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 import net.sf.jasperreports.engine.JRException;
41 import net.sf.jasperreports.engine.JRExporterParameter;
42 import net.sf.jasperreports.engine.export.JRRtfExporter;
43
44 /**
45  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
46  * @version $Id: RtfServlet.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
47  */

48 public class RtfServlet extends BaseHttpServlet
49 {
50
51
52     /**
53      *
54      */

55     public void service(
56         HttpServletRequest JavaDoc request,
57         HttpServletResponse JavaDoc response
58         ) throws IOException JavaDoc, ServletException JavaDoc
59     {
60         List JavaDoc jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
61
62         if (jasperPrintList == null)
63         {
64             throw new ServletException JavaDoc("No JasperPrint documents found on the HTTP session.");
65         }
66         
67         Boolean JavaDoc isBuffered = Boolean.valueOf(request.getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER));
68         if (isBuffered.booleanValue())
69         {
70             JRRtfExporter exporter = new JRRtfExporter();
71             exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
72
73             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
75
76             try
77             {
78                 exporter.exportReport();
79             }
80             catch (JRException e)
81             {
82                 throw new ServletException JavaDoc(e);
83             }
84
85             byte[] bytes = baos.toByteArray();
86             
87             if (bytes != null && bytes.length > 0)
88             {
89                 response.setContentType("application/rtf");
90                 response.setHeader("Content-Disposition", "inline; filename=\"file.rtf\"");
91                 response.setContentLength(bytes.length);
92                 ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
93
94                 try
95                 {
96                     ouputStream.write(bytes, 0, bytes.length);
97                     ouputStream.flush();
98                 }
99                 finally
100                 {
101                     if (ouputStream != null)
102                     {
103                         try
104                         {
105                             ouputStream.close();
106                         }
107                         catch (IOException JavaDoc ex)
108                         {
109                         }
110                     }
111                 }
112             }
113 // else
114
// {
115
// response.setContentType("text/html");
116
// PrintWriter out = response.getWriter();
117
// out.println("<html>");
118
// out.println("<body bgcolor=\"white\">");
119
// out.println("<span class=\"bold\">Empty response.</span>");
120
// out.println("</body>");
121
// out.println("</html>");
122
// }
123
}
124         else
125         {
126             response.setContentType("application/rtf");
127             response.setHeader("Content-Disposition", "inline; filename=\"file.rtf\"");
128
129             JRRtfExporter exporter = new JRRtfExporter();
130             exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
131             
132             OutputStream JavaDoc ouputStream = response.getOutputStream();
133             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
134
135             try
136             {
137                 exporter.exportReport();
138             }
139             catch (JRException e)
140             {
141                 throw new ServletException JavaDoc(e);
142             }
143             finally
144             {
145                 if (ouputStream != null)
146                 {
147                     try
148                     {
149                         ouputStream.close();
150                     }
151                     catch (IOException JavaDoc ex)
152                     {
153                     }
154                 }
155             }
156         }
157     }
158
159     
160 }
161
162
Popular Tags