1 21 package com.jaspersoft.jasperserver.war.action; 22 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.servlet.ServletOutputStream ; 29 import javax.servlet.http.HttpServletResponse ; 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 51 public abstract class AbstractReportExporter extends MultiAction { 52 53 private static final Log log = LogFactory.getLog(AbstractReportExporter.class); 54 55 private String flowAttributeJasperPrintSessionName; 56 private String 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 , JRException { 62 ServletExternalContext servletContext = (ServletExternalContext) context.getExternalContext(); 63 HttpServletResponse response = servletContext.getResponse(); 64 65 AttributeMap flowAttrs = context.getFlowScope(); 66 String jasperPrintSessionName = flowAttrs.getRequiredString(getFlowAttributeJasperPrintSessionName()); 67 JasperPrint jasperPrint = (JasperPrint) servletContext.getSessionMap().getRequired(jasperPrintSessionName, JasperPrint.class); 68 String 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 response, JasperPrint jasperPrint, String reportUnitURI) throws IOException , JRException { 83 Map parameters = new HashMap (); 84 parameters.put(JRExporterParameter.JASPER_PRINT, jasperPrint); 85 86 OutputStream 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 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 response, JasperPrint jasperPrint, String reportUnitURI) throws IOException , JRException { 116 Map parameters = new HashMap (); 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 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 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 getFlowAttributeJasperPrintSessionName() { 153 return flowAttributeJasperPrintSessionName; 154 } 155 156 public void setFlowAttributeJasperPrintSessionName(String requestAttributeJasperPrint) { 157 this.flowAttributeJasperPrintSessionName = requestAttributeJasperPrint; 158 } 159 160 public String getFlowAttributeReportUnitURI() { 161 return flowAttributeReportUnitURI; 162 } 163 164 public void setFlowAttributeReportUnitURI( 165 String 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 getContentType(RequestContext context); 195 196 protected abstract void setAdditionalResponseHeaders(RequestContext context, HttpServletResponse response); 197 198 protected abstract void export(RequestContext context, ExecutionContext executionContext, String reportUnitURI, Map baseParameters) throws JRException; 199 200 } 201 | Popular Tags |