String type = request.getParameter("type");
String reportData = request.getParameter("reportDataSession");
String downloadFileName = null;
if (component == null || template == null || type == null || reportData == null) {
throw new ReportingException("required one or more parameters missing (component ,template , reportType, reportData)");
}
if (type.equals("pdf")) {
response.setContentType("application/pdf");
downloadFileName = template + ".pdf";
} else if (type.equals("excel")) {
response.setContentType("application/vnd.ms-excel");
downloadFileName = template + ".xls";
} else if (type.equals("html")) {
response.setContentType("text/html");
} else {
throw new ReportingException("requested report type can not be support");
}
if (downloadFileName != null) {
response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFileName + "\"");
}
Object reportDataObject = request.getSession().getAttribute(reportData);
if (reportDataObject == null) {
throw new ReportingException("can't generate report , data unavailable in session ");
}
try {
String serverURL = CarbonUIUtil.getServerURL(request.getSession().getServletContext(), request.getSession());
ConfigurationContext configurationContext = (ConfigurationContext) request.getSession().getServletContext().
getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);
String cookie = (String) request.getSession().getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
ReportResourceSupplierClient resourceSupplierClient = new ReportResourceSupplierClient(cookie,
serverURL, configurationContext);
List<ReportDataManager> reportDataList = (List<ReportDataManager>) reportDataObject;
String reportResource = resourceSupplierClient.getReportResources(component, template);
JRDataSource jrDataSource = new JRBeanCollectionDataSource(reportDataList);
JasperPrintProvider jasperPrintProvider = new JasperPrintProvider();
JasperPrint jasperPrint = jasperPrintProvider.createJasperPrint(jrDataSource ,reportResource, new ReportParamMap[0]);
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
ReportStream reportStream = new ReportStream();
ByteArrayOutputStream outputStream = reportStream.getReportStream(jasperPrint,type);
ServletOutputStream servletOutputStream = response.getOutputStream();
try{
outputStream.writeTo(servletOutputStream);
outputStream.flush();
}finally {
outputStream.close();
servletOutputStream.close();
}
} catch (Exception e) {
String msg = "Error occurred handling " + template + "report request from " + component;
log(msg);
throw new ReportingException(msg, e);
}
}