1 19 20 package org.efs.openreports.services.servlet; 21 22 import java.beans.PropertyDescriptor ; 23 import java.io.IOException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletOutputStream ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.commons.beanutils.PropertyUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 import org.efs.openreports.engine.ReportEngine; 38 import org.efs.openreports.engine.output.QueryEngineOutput; 39 import org.efs.openreports.engine.output.ReportEngineOutput; 40 import org.efs.openreports.services.ReportService; 41 import org.efs.openreports.services.input.ServletReportServiceInput; 42 import org.springframework.context.ApplicationContext; 43 import org.springframework.web.context.support.WebApplicationContextUtils; 44 45 52 53 public class ReportServiceServlet extends HttpServlet 54 { 55 private static Logger log = Logger.getLogger(ReportServiceServlet.class.getName()); 56 57 private ReportService reportService; 58 private PropertyDescriptor [] descriptors; 59 60 public void init(ServletConfig servletConfig) throws ServletException 61 { 62 ApplicationContext appContext = WebApplicationContextUtils 63 .getRequiredWebApplicationContext(servletConfig.getServletContext()); 64 65 reportService = (ReportService) appContext.getBean("reportService", ReportService.class); 66 67 descriptors = PropertyUtils.getPropertyDescriptors(ServletReportServiceInput.class); 69 70 super.init(servletConfig); 71 72 log.info("Started..."); 73 } 74 75 protected void service(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException , IOException 77 { 78 try 79 { 80 ServletReportServiceInput reportInput = buildReportServiceInput(request); 81 reportInput.setParameterMap(request.getParameterMap()); 82 83 ReportEngineOutput reportOutput = reportService.generateReport(reportInput); 84 85 if (reportOutput instanceof QueryEngineOutput) 86 { 87 QueryEngineOutput queryReport = (QueryEngineOutput) reportOutput; 88 89 request.getSession().setAttribute("properties", queryReport.getProperties()); 90 request.getSession().setAttribute("results", queryReport.getResults()); 91 request.getSession().setAttribute("reportName", StringUtils.deleteWhitespace(reportInput.getReportName())); 92 93 response.sendRedirect("report-viewer/query-report.jsp"); 94 95 97 return; 98 } 99 100 ServletOutputStream out = response.getOutputStream(); 101 102 if (reportOutput.getContent() != null) 103 { 104 if (reportInput.getExportType() != ReportEngine.EXPORT_HTML) 105 { 106 response.setContentType(reportOutput.getContentType()); 107 response.setContentLength(reportOutput.getContent().length); 108 response.setHeader("Content-disposition", "inline; filename=" 109 + StringUtils.deleteWhitespace(reportInput.getReportName()) 110 + reportOutput.getContentExtension()); 111 } 112 113 out.write(reportOutput.getContent(), 0, reportOutput.getContent().length); 114 } 115 else 116 { 117 out.write(reportOutput.getContentMessage().getBytes()); 118 } 119 120 out.flush(); 121 out.close(); 122 } 123 catch (Exception e) 124 { 125 response.getOutputStream().write(e.toString().getBytes()); 126 } 127 } 128 129 135 protected ServletReportServiceInput buildReportServiceInput(HttpServletRequest request) 136 throws IllegalAccessException , InvocationTargetException 137 { 138 ServletReportServiceInput reportInput = new ServletReportServiceInput(); 139 140 for (int i = 0; i < descriptors.length; i++) 141 { 142 String value = request.getParameter(descriptors[i].getName()); 143 if (value != null) 144 { 145 Method method = descriptors[i].getWriteMethod(); 146 147 log.debug("Building ServletReportRequest: " + descriptors[i].getName() 148 + " | " + method.getName() + " | " + value); 149 150 if (descriptors[i].getPropertyType() == int.class) 151 { 152 method.invoke(reportInput, new Integer []{Integer.valueOf(value)}); 153 } 154 else if (descriptors[i].getPropertyType() == String .class) 155 { 156 method.invoke(reportInput, new String []{value}); 157 } 158 } 159 } 160 161 return reportInput; 162 } 163 } 164 | Popular Tags |