KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > AbstractReportExporter


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.action;
22
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.ServletOutputStream JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JRExporterParameter;
33 import net.sf.jasperreports.engine.JasperPrint;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.springframework.webflow.AttributeMap;
38 import org.springframework.webflow.Event;
39 import org.springframework.webflow.RequestContext;
40 import org.springframework.webflow.action.MultiAction;
41 import org.springframework.webflow.context.servlet.ServletExternalContext;
42
43 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
44 import com.jaspersoft.jasperserver.war.common.FileBufferedOutputStream;
45 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
46
47 /**
48  * @author Lucian Chirita (lucianc@users.sourceforge.net)
49  * @version $Id: AbstractReportExporter.java 4164 2006-08-08 15:58:48Z lucian $
50  */

51 public abstract class AbstractReportExporter extends MultiAction {
52     
53     private static final Log log = LogFactory.getLog(AbstractReportExporter.class);
54
55     private String JavaDoc flowAttributeJasperPrintSessionName;
56     private String JavaDoc flowAttributeReportUnitURI;
57     private boolean setResponseContentLength = true;
58     private int memoryThreshold = FileBufferedOutputStream.DEFAULT_MEMORY_THRESHOLD;
59     private int initialMemoryBufferSize = FileBufferedOutputStream.DEFAULT_INITIAL_MEMORY_BUFFER_SIZE;
60     
61     public Event export(RequestContext context) throws IOException JavaDoc, JRException {
62         ServletExternalContext servletContext = (ServletExternalContext) context.getExternalContext();
63         HttpServletResponse JavaDoc response = servletContext.getResponse();
64
65         AttributeMap flowAttrs = context.getFlowScope();
66         String JavaDoc jasperPrintSessionName = flowAttrs.getRequiredString(getFlowAttributeJasperPrintSessionName());
67         JasperPrint jasperPrint = (JasperPrint) servletContext.getSessionMap().getRequired(jasperPrintSessionName, JasperPrint.class);
68         String JavaDoc reportUnitURI = flowAttrs.getRequiredString(getFlowAttributeReportUnitURI());
69         
70         if (setResponseContentLength)
71         {
72             exportBuffered(context, response, jasperPrint, reportUnitURI);
73         }
74         else
75         {
76             exportToStream(context, response, jasperPrint, reportUnitURI);
77         }
78         
79         return success();
80     }
81
82     protected void exportToStream(RequestContext context, HttpServletResponse JavaDoc response, JasperPrint jasperPrint, String JavaDoc reportUnitURI) throws IOException JavaDoc, JRException {
83         Map JavaDoc parameters = new HashMap JavaDoc();
84         parameters.put(JRExporterParameter.JASPER_PRINT, jasperPrint);
85
86         OutputStream JavaDoc ouputStream = response.getOutputStream();
87         parameters.put(JRExporterParameter.OUTPUT_STREAM, ouputStream);
88         
89         try
90         {
91             response.setContentType(getContentType(context));
92             setAdditionalResponseHeaders(context, response);
93             export(context, getExecutionContext(context), reportUnitURI, parameters);
94         }
95         finally
96         {
97             if (ouputStream != null)
98             {
99                 try
100                 {
101                     ouputStream.close();
102                 }
103                 catch (IOException JavaDoc ex)
104                 {
105                     log.warn("Error closing output stream", ex);
106                 }
107             }
108         }
109     }
110
111     protected ExecutionContext getExecutionContext(RequestContext context) {
112         return JasperServerUtil.getExecutionContext(context);
113     }
114
115     protected void exportBuffered(RequestContext context, HttpServletResponse JavaDoc response, JasperPrint jasperPrint, String JavaDoc reportUnitURI) throws IOException JavaDoc, JRException {
116         Map JavaDoc parameters = new HashMap JavaDoc();
117         parameters.put(JRExporterParameter.JASPER_PRINT, jasperPrint);
118
119         FileBufferedOutputStream bufferedOutput = new FileBufferedOutputStream(getMemoryThreshold(), getInitialMemoryBufferSize());
120         parameters.put(JRExporterParameter.OUTPUT_STREAM, bufferedOutput);
121
122         try {
123             export(context, getExecutionContext(context), reportUnitURI, parameters);
124             bufferedOutput.close();
125             
126             response.setContentType(getContentType(context));
127             setAdditionalResponseHeaders(context, response);
128             response.setContentLength(bufferedOutput.size());
129             ServletOutputStream JavaDoc ouputStream = response.getOutputStream();
130
131             try {
132                 bufferedOutput.writeData(ouputStream);
133                 bufferedOutput.dispose();
134                 
135                 ouputStream.flush();
136             } finally {
137                 if (ouputStream != null) {
138                     try {
139                         ouputStream.close();
140                     }
141                     catch (IOException JavaDoc ex) {
142                         log.warn("Error closing output stream", ex);
143                     }
144                 }
145             }
146         } finally {
147             bufferedOutput.close();
148             bufferedOutput.dispose();
149         }
150     }
151
152     public String JavaDoc getFlowAttributeJasperPrintSessionName() {
153         return flowAttributeJasperPrintSessionName;
154     }
155
156     public void setFlowAttributeJasperPrintSessionName(String JavaDoc requestAttributeJasperPrint) {
157         this.flowAttributeJasperPrintSessionName = requestAttributeJasperPrint;
158     }
159
160     public String JavaDoc getFlowAttributeReportUnitURI() {
161         return flowAttributeReportUnitURI;
162     }
163
164     public void setFlowAttributeReportUnitURI(
165             String JavaDoc requestAttributeReportUnitURI) {
166         this.flowAttributeReportUnitURI = requestAttributeReportUnitURI;
167     }
168
169     public boolean isSetResponseContentLength() {
170         return setResponseContentLength;
171     }
172
173     public void setSetResponseContentLength(boolean setResponseContentLength) {
174         this.setResponseContentLength = setResponseContentLength;
175     }
176
177     public int getInitialMemoryBufferSize() {
178         return initialMemoryBufferSize;
179     }
180
181     public int getMemoryThreshold() {
182         return memoryThreshold;
183     }
184
185     public void setMemoryThreshold(int memoryThreshold) {
186         this.memoryThreshold = memoryThreshold;
187     }
188
189     public void setInitialMemoryBufferSize(int initialMemoryBufferSize) {
190         this.initialMemoryBufferSize = initialMemoryBufferSize;
191     }
192     
193     
194     protected abstract String JavaDoc getContentType(RequestContext context);
195     
196     protected abstract void setAdditionalResponseHeaders(RequestContext context, HttpServletResponse JavaDoc response);
197     
198     protected abstract void export(RequestContext context, ExecutionContext executionContext, String JavaDoc reportUnitURI, Map JavaDoc baseParameters) throws JRException;
199     
200 }
201
Popular Tags