001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view.jasperreports; 018 019import org.springframework.beans.BeanUtils; 020import org.springframework.util.Assert; 021 022/** 023 * Configurable JasperReports View, allowing to specify the JasperReports exporter 024 * to be specified through bean properties rather than through the view class name. 025 * 026 * <p><b>This class is compatible with classic JasperReports releases back until 2.x.</b> 027 * As a consequence, it keeps using the {@link net.sf.jasperreports.engine.JRExporter} 028 * API which got deprecated as of JasperReports 5.5.2 (early 2014). 029 * 030 * @author Rob Harrop 031 * @since 2.0 032 * @see JasperReportsCsvView 033 * @see JasperReportsHtmlView 034 * @see JasperReportsPdfView 035 * @see JasperReportsXlsView 036 */ 037@SuppressWarnings({"deprecation", "rawtypes"}) 038public class ConfigurableJasperReportsView extends AbstractJasperReportsSingleFormatView { 039 040 private Class<? extends net.sf.jasperreports.engine.JRExporter> exporterClass; 041 042 private boolean useWriter = true; 043 044 045 /** 046 * Set the {@code JRExporter} implementation {@code Class} to use. Throws 047 * {@link IllegalArgumentException} if the {@code Class} doesn't implement 048 * {@code JRExporter}. Required setting, as it does not have a default. 049 */ 050 public void setExporterClass(Class<? extends net.sf.jasperreports.engine.JRExporter> exporterClass) { 051 Assert.isAssignable(net.sf.jasperreports.engine.JRExporter.class, exporterClass); 052 this.exporterClass = exporterClass; 053 } 054 055 /** 056 * Specifies whether or not the {@code JRExporter} writes to the {@link java.io.PrintWriter} 057 * of the associated with the request ({@code true}) or whether it writes directly to the 058 * {@link java.io.InputStream} of the request ({@code false}). Default is {@code true}. 059 */ 060 public void setUseWriter(boolean useWriter) { 061 this.useWriter = useWriter; 062 } 063 064 /** 065 * Checks that the {@link #setExporterClass(Class) exporterClass} property is specified. 066 */ 067 @Override 068 protected void onInit() { 069 if (this.exporterClass == null) { 070 throw new IllegalArgumentException("exporterClass is required"); 071 } 072 } 073 074 075 /** 076 * Returns a new instance of the specified {@link net.sf.jasperreports.engine.JRExporter} class. 077 * @see #setExporterClass(Class) 078 * @see BeanUtils#instantiateClass(Class) 079 */ 080 @Override 081 protected net.sf.jasperreports.engine.JRExporter createExporter() { 082 return BeanUtils.instantiateClass(this.exporterClass); 083 } 084 085 /** 086 * Indicates how the {@code JRExporter} should render its data. 087 * @see #setUseWriter(boolean) 088 */ 089 @Override 090 protected boolean useWriter() { 091 return this.useWriter; 092 } 093 094}