KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > jasperreports > AbstractJasperReportsSingleFormatView


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.jasperreports;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletOutputStream JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import net.sf.jasperreports.engine.JRExporter;
28 import net.sf.jasperreports.engine.JRExporterParameter;
29 import net.sf.jasperreports.engine.JasperPrint;
30
31 import org.springframework.ui.jasperreports.JasperReportsUtils;
32 import org.springframework.util.CollectionUtils;
33 import org.springframework.web.util.WebUtils;
34
35 /**
36  * Extends <code>AbstractJasperReportsView</code> to provide basic rendering logic
37  * for views that use a fixed format, e.g. always PDF or always HTML.
38  *
39  * <p>Subclasses need to implement two template methods: <code>createExporter</code>
40  * to create a JasperReports exporter for a specific output format, and
41  * <code>useWriter</code> to determine whether to write text or binary content.
42  *
43  * @author Rob Harrop
44  * @author Juergen Hoeller
45  * @since 1.1.5
46  * @see #createExporter()
47  * @see #useWriter()
48  */

49 public abstract class AbstractJasperReportsSingleFormatView extends AbstractJasperReportsView {
50
51     /**
52      * Initial size for the output array.
53      */

54     private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 4096;
55
56
57     /**
58      * Perform rendering for a single Jasper Reports exporter, that is,
59      * for a pre-defined output format.
60      */

61     protected void renderReport(JasperPrint populatedReport, Map JavaDoc model, HttpServletResponse JavaDoc response)
62             throws Exception JavaDoc {
63
64         // Prepare report for rendering.
65
JRExporter exporter = createExporter();
66
67         // Set exporter parameters - overriding with values from the Model.
68
Map JavaDoc mergedExporterParameters = mergeExporterParameters(model);
69         if (!CollectionUtils.isEmpty(mergedExporterParameters)) {
70             exporter.setParameters(mergedExporterParameters);
71         }
72
73         if (useWriter()) {
74             // We need to write text to the response Writer.
75

76             // Copy the encoding configured for the report into the response.
77
String JavaDoc contentType = getContentType();
78             String JavaDoc encoding = (String JavaDoc) exporter.getParameter(JRExporterParameter.CHARACTER_ENCODING);
79             if (encoding != null) {
80                 // Only apply encoding if content type is specified but does not contain charset clause already.
81
if (contentType != null && contentType.toLowerCase().indexOf(WebUtils.CONTENT_TYPE_CHARSET_PREFIX) == -1) {
82                     contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
83                 }
84             }
85             response.setContentType(contentType);
86
87             // Render report into HttpServletResponse's Writer.
88
JasperReportsUtils.render(exporter, populatedReport, response.getWriter());
89         }
90
91         else {
92             // We need to write binary output to the response OutputStream.
93

94             // Apply the content type as specified - we don't need an encoding here.
95
response.setContentType(getContentType());
96
97             // Render report into local OutputStream.
98
// IE workaround: write into byte array first.
99
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
100             JasperReportsUtils.render(exporter, populatedReport, baos);
101
102             // Write content length (determined via byte array).
103
response.setContentLength(baos.size());
104
105             // Flush byte array to servlet output stream.
106
ServletOutputStream JavaDoc out = response.getOutputStream();
107             baos.writeTo(out);
108             out.flush();
109         }
110     }
111
112     /**
113      * Merges the configured JRExporterParameters with any specified in the supplied model data.
114      * JRExporterParameters in the model override those specified in the configuration.
115      * @see #setExporterParameters(java.util.Map)
116      */

117     protected Map JavaDoc mergeExporterParameters(Map JavaDoc model) {
118         Map JavaDoc mergedParameters = new HashMap JavaDoc();
119         Map JavaDoc convertedExporterParameters = getConvertedExporterParameters();
120         if (!CollectionUtils.isEmpty(convertedExporterParameters)) {
121             mergedParameters.putAll(convertedExporterParameters);
122         }
123         for (Iterator JavaDoc it = model.keySet().iterator(); it.hasNext();) {
124             Object JavaDoc key = it.next();
125             if (key instanceof JRExporterParameter) {
126                 Object JavaDoc value = model.get(key);
127                 if (value instanceof String JavaDoc) {
128                     mergedParameters.put(key, value);
129                 }
130                 else {
131                     if (logger.isWarnEnabled()) {
132                         logger.warn("Ignoring exporter parameter [" + key + "]: value is not a String");
133                     }
134                 }
135             }
136         }
137         return mergedParameters;
138     }
139
140
141     /**
142      * Create a JasperReports exporter for a specific output format,
143      * which will be used to render the report to the HTTP response.
144      * <p>The <code>useWriter</code> method determines whether the
145      * output will be written as text or as binary content.
146      * @see #useWriter()
147      */

148     protected abstract JRExporter createExporter();
149
150     /**
151      * Return whether to use a <code>java.io.Writer</code> to write text content
152      * to the HTTP response. Else, a <code>java.io.OutputStream</code> will be used,
153      * to write binary content to the response.
154      * @see javax.servlet.ServletResponse#getWriter()
155      * @see javax.servlet.ServletResponse#getOutputStream()
156      */

157     protected abstract boolean useWriter();
158
159 }
160
Popular Tags