1 5 package com.opensymphony.webwork.views.jasperreports; 6 7 import com.opensymphony.util.TextUtils; 8 import com.opensymphony.webwork.ServletActionContext; 9 import com.opensymphony.webwork.dispatcher.WebWorkResultSupport; 10 import com.opensymphony.xwork.ActionInvocation; 11 import com.opensymphony.xwork.util.OgnlValueStack; 12 import net.sf.jasperreports.engine.*; 13 import net.sf.jasperreports.engine.export.*; 14 import net.sf.jasperreports.engine.util.JRLoader; 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import javax.servlet.ServletContext ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.ServletOutputStream ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 52 public class JasperReportsResult extends WebWorkResultSupport implements JasperReportConstants { 53 private final static Log LOG = LogFactory.getLog(JasperReportsResult.class); 54 55 protected String IMAGES_DIR = "/images/"; 56 protected String dataSource; 57 protected String format; 58 protected String documentName; 59 protected String contentDisposition; 60 protected String delimiter; 61 62 public void setDataSource(String dataSource) { 63 this.dataSource = dataSource; 64 } 65 66 public void setFormat(String format) { 67 this.format = format; 68 } 69 70 public void setDocumentName(String documentName) { 71 this.documentName = documentName; 72 } 73 74 public void setContentDisposition(String contentDisposition) { 75 this.contentDisposition = contentDisposition; 76 } 77 78 public void setDelimiter(String delimiter) { 79 this.delimiter = delimiter; 80 } 81 82 protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { 83 if (this.format == null) { 84 this.format = FORMAT_PDF; 85 } 86 87 if (dataSource == null) { 88 String message = "No dataSource specified..."; 89 LOG.error(message); 90 throw new RuntimeException (message); 91 } 92 93 if (LOG.isDebugEnabled()) { 94 LOG.debug("Creating JasperReport for dataSource = " + dataSource + ", format = " + this.format); 95 } 96 97 HttpServletRequest request = (HttpServletRequest ) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST); 98 HttpServletResponse response = (HttpServletResponse ) invocation.getInvocationContext().get(ServletActionContext.HTTP_RESPONSE); 99 100 OgnlValueStack stack = invocation.getStack(); 102 OgnlValueStackDataSource stackDataSource = new OgnlValueStackDataSource(stack, dataSource); 103 104 format = conditionalParse(format, invocation); 105 dataSource = conditionalParse(dataSource, invocation); 106 107 if (contentDisposition != null) { 108 contentDisposition = conditionalParse(contentDisposition, invocation); 109 } 110 111 if (documentName != null) { 112 documentName = conditionalParse(documentName, invocation); 113 } 114 115 if (!TextUtils.stringSet(format)) { 117 format = FORMAT_PDF; 118 } 119 120 if (!"contype".equals(request.getHeader("User-Agent"))) { 121 ServletContext servletContext = (ServletContext ) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONTEXT); 123 String systemId = servletContext.getRealPath(finalLocation); 124 Map parameters = new OgnlValueStackShadowMap(stack); 125 File directory = new File (systemId.substring(0, systemId.lastIndexOf(File.separator))); 126 parameters.put("reportDirectory", directory); 127 128 byte[] output; 129 JasperPrint jasperPrint; 130 131 try { 133 JasperReport jasperReport = (JasperReport) JRLoader.loadObject(systemId); 134 135 jasperPrint = 136 JasperFillManager.fillReport(jasperReport, 137 parameters, 138 stackDataSource); 139 } catch (JRException e) { 140 LOG.error("Error building report for uri " + systemId, e); 141 throw new ServletException (e.getMessage(), e); 142 } 143 144 try { 146 if (contentDisposition != null || documentName != null) { 147 final StringBuffer tmp = new StringBuffer (); 148 tmp.append((contentDisposition == null) ? "inline" : contentDisposition); 149 150 if (documentName != null) { 151 tmp.append("; filename="); 152 tmp.append(documentName); 153 tmp.append("."); 154 tmp.append(format.toLowerCase()); 155 } 156 157 response.setHeader("Content-disposition", tmp.toString()); 158 } 159 160 if (format.equals(FORMAT_PDF)) { 161 response.setContentType("application/pdf"); 162 163 output = JasperExportManager.exportReportToPdf(jasperPrint); 165 } else { 166 JRExporter exporter; 167 168 if (format.equals(FORMAT_CSV)) { 169 response.setContentType("text/plain"); 170 exporter = new JRCsvExporter(); 171 } else if (format.equals(FORMAT_HTML)) { 172 response.setContentType("text/html"); 173 174 Map imagesMap = new HashMap (); 175 176 request.getSession(true).setAttribute("IMAGES_MAP", imagesMap); 177 exporter = new JRHtmlExporter(); 178 exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); 179 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + IMAGES_DIR); 180 } else if (format.equals(FORMAT_XLS)) { 181 response.setContentType("application/vnd.ms-excel"); 182 exporter = new JRXlsExporter(); 183 } else if (format.equals(FORMAT_XML)) { 184 response.setContentType("text/xml"); 185 exporter = new JRXmlExporter(); 186 } else { 187 throw new ServletException ("Unknown report format: " + format); 188 } 189 190 output = exportReportToBytes(jasperPrint, exporter); 191 } 192 } catch (JRException e) { 193 String message = "Error producing " + format + " report for uri " + systemId; 194 LOG.error(message, e); 195 throw new ServletException (e.getMessage(), e); 196 } 197 198 response.setContentLength(output.length); 199 200 ServletOutputStream ouputStream; 201 202 try { 203 ouputStream = response.getOutputStream(); 204 ouputStream.write(output); 205 ouputStream.flush(); 206 ouputStream.close(); 207 } catch (IOException e) { 208 LOG.error("Error writing report output", e); 209 throw new ServletException (e.getMessage(), e); 210 } 211 } else { 212 try { 214 ServletOutputStream outputStream; 215 response.setContentType("application/pdf"); 216 response.setContentLength(0); 217 outputStream = response.getOutputStream(); 218 outputStream.close(); 219 } catch (IOException e) { 220 LOG.error("Error writing report output", e); 221 throw new ServletException (e.getMessage(), e); 222 } 223 } 224 } 225 226 235 private byte[] exportReportToBytes(JasperPrint jasperPrint, JRExporter exporter) throws JRException { 236 byte[] output; 237 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 238 239 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 240 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 241 if (delimiter != null) { 242 exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, delimiter); 243 } 244 245 exporter.exportReport(); 246 247 output = baos.toByteArray(); 248 249 return output; 250 } 251 } 252 | Popular Tags |