1 16 17 package org.springframework.web.servlet.view.jasperreports; 18 19 import java.util.Enumeration ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 24 import javax.servlet.http.HttpServletResponse ; 25 26 import net.sf.jasperreports.engine.JasperPrint; 27 28 import org.springframework.beans.BeanUtils; 29 import org.springframework.context.ApplicationContextException; 30 import org.springframework.util.ClassUtils; 31 32 71 public class JasperReportsMultiFormatView extends AbstractJasperReportsView { 72 73 76 public static final String DEFAULT_FORMAT_KEY = "format"; 77 78 79 82 private String formatKey = DEFAULT_FORMAT_KEY; 83 84 88 private Map formatMappings; 89 90 93 private Properties contentDispositionMappings; 94 95 96 100 public JasperReportsMultiFormatView() { 101 this.formatMappings = new HashMap (4); 102 this.formatMappings.put("csv", JasperReportsCsvView.class); 103 this.formatMappings.put("html", JasperReportsHtmlView.class); 104 this.formatMappings.put("pdf", JasperReportsPdfView.class); 105 this.formatMappings.put("xls", JasperReportsXlsView.class); 106 } 107 108 112 public void setFormatKey(String formatKey) { 113 this.formatKey = formatKey; 114 } 115 116 126 public void setFormatMappings(Properties mappingsWithClassNames) { 127 if (mappingsWithClassNames == null || mappingsWithClassNames.isEmpty()) { 128 throw new IllegalArgumentException ("formatMappings must not be empty"); 129 } 130 131 this.formatMappings = new HashMap (mappingsWithClassNames.size()); 132 for (Enumeration discriminators = mappingsWithClassNames.propertyNames(); discriminators.hasMoreElements();) { 133 String discriminator = (String ) discriminators.nextElement(); 134 String className = mappingsWithClassNames.getProperty(discriminator); 135 try { 136 if (logger.isDebugEnabled()) { 137 logger.debug("Mapped view class [" + className + "] to mapping key [" + discriminator + "]"); 138 } 139 this.formatMappings.put(discriminator, ClassUtils.forName(className)); 140 } 141 catch (ClassNotFoundException ex) { 142 throw new ApplicationContextException( 143 "Class [" + className + "] mapped to format [" + discriminator + "] cannot be found", ex); 144 } 145 } 146 } 147 148 154 public void setContentDispositionMappings(Properties mappings) { 155 this.contentDispositionMappings = mappings; 156 } 157 158 163 public Properties getContentDispositionMappings() { 164 if (this.contentDispositionMappings == null) { 165 this.contentDispositionMappings = new Properties (); 166 } 167 return this.contentDispositionMappings; 168 } 169 170 171 176 protected void renderReport(JasperPrint populatedReport, Map model, HttpServletResponse response) 177 throws Exception { 178 179 String format = (String ) model.get(this.formatKey); 180 if (format == null) { 181 throw new IllegalArgumentException ("No format format found in model"); 182 } 183 184 if (logger.isDebugEnabled()) { 185 logger.debug("Rendering report using format mapping key [" + format + "]"); 186 } 187 188 Class viewClass = (Class ) this.formatMappings.get(format); 189 if (viewClass == null) { 190 throw new IllegalArgumentException ("Format discriminator [" + format + "] is not a configured mapping"); 191 } 192 193 if (logger.isDebugEnabled()) { 194 logger.debug("Rendering report using view class [" + viewClass.getName() + "]"); 195 } 196 197 AbstractJasperReportsView view = (AbstractJasperReportsView) BeanUtils.instantiateClass(viewClass); 198 199 view.setExporterParameters(getExporterParameters()); 201 202 view.convertExporterParameters(); 205 206 populateContentDispositionIfNecessary(response, format); 208 view.renderReport(populatedReport, model, response); 209 } 210 211 218 private void populateContentDispositionIfNecessary(HttpServletResponse response, String format) { 219 if (this.contentDispositionMappings != null) { 220 String header = this.contentDispositionMappings.getProperty(format); 221 if (header != null) { 222 if (logger.isDebugEnabled()) { 223 logger.debug("Setting Content-Disposition header to: [" + header + "]"); 224 } 225 response.setHeader(HEADER_CONTENT_DISPOSITION, header); 226 } 227 } 228 } 229 230 } 231 | Popular Tags |