List of usage examples for org.apache.wicket.authroles.authentication AuthenticatedWebApplication getSharedResources
public SharedResources getSharedResources()
From source file:org.devgateway.toolkit.forms.wicket.page.reports.AbstractReportPage.java
License:Open Source License
/** * Generates the report in the specified <code>outputType</code> and writes * it into the specified <code>outputStream</code>. * * It is the responsibility of the caller to close the * <code>outputStream</code> after this method is executed. * * @param outputType/*from www. j av a 2s .co m*/ * the output type of the report (HTML, PDF, HTML) * @param outputStream * the stream into which the report will be written * @throws IllegalArgumentException * indicates the required parameters were not provided * @throws ReportProcessingException * indicates an error generating the report */ public void generateReport(final OutputType outputType, final OutputStream outputStream) throws IllegalArgumentException, ReportProcessingException { if (outputStream == null) { throw new IllegalArgumentException("The output stream was not specified"); } // Get the report and data factory final MasterReport report = getReportDefinition(); // Add any parameters to the report final Map<String, Object> reportParameters = getReportParameters(); if (reportParameters == null) { return; } for (String key : reportParameters.keySet()) { report.getParameterValues().put(key, reportParameters.get(key)); } // Prepare to generate the report AbstractReportProcessor reportProcessor = null; try { // Greate the report processor for the specified output type switch (outputType) { case PDF: final PdfOutputProcessor targetPdf = new PdfOutputProcessor(report.getConfiguration(), outputStream, report.getResourceManager()); reportProcessor = new PageableReportProcessor(report, targetPdf); reportProcessor.processReport(); break; case EXCEL: final FlowExcelOutputProcessor targetExcel = new FlowExcelOutputProcessor(report.getConfiguration(), outputStream, report.getResourceManager()); reportProcessor = new FlowReportProcessor(report, targetExcel); reportProcessor.processReport(); break; case RTF: final FlowRTFOutputProcessor targetRtf = new FlowRTFOutputProcessor(report.getConfiguration(), outputStream, report.getResourceManager()); reportProcessor = new FlowReportProcessor(report, targetRtf); reportProcessor.processReport(); break; case HTML: ContentLocation targetRoot = null; File tempDir = null; try { //we manually make the folder to drop all exported html files into tempDir = ReportUtil.createTemporaryDirectory("tmpreport"); targetRoot = new FileRepository(tempDir).getRoot(); } catch (ContentIOException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //we create a folder content resource for the entire tmpdir. // This dir will only hold the fields for this export FolderContentResource fcr = new FolderContentResource(tempDir); //we always have an authenticated web app AuthenticatedWebApplication authApp = (AuthenticatedWebApplication) getApplication(); //we add the folder resource as a shared resource authApp.getSharedResources().add(tempDir.getName(), fcr); SharedResourceReference folderResourceReference = new SharedResourceReference(tempDir.getName()); authApp.mountResource(tempDir.getName(), folderResourceReference); final HtmlOutputProcessor outputProcessor = new StreamHtmlOutputProcessor( report.getConfiguration()); final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager()); printer.setContentWriter(targetRoot, new DefaultNameGenerator(targetRoot, "index", "html")); printer.setDataWriter(targetRoot, new DefaultNameGenerator(targetRoot, "content")); //$NON-NLS-1$ //we use a special URL Rewriter that knows how to speak Wicket :-) printer.setUrlRewriter(new WicketResourceURLRewriter(folderResourceReference)); outputProcessor.setPrinter(printer); reportProcessor = new StreamReportProcessor(report, outputProcessor); reportProcessor.processReport(); // we plug the html file stream into the output stream FileInputStream indexFileStream = new FileInputStream( tempDir.getAbsolutePath() + File.separator + "index.html"); IOUtils.copy(indexFileStream, outputStream); indexFileStream.close(); break; default: throw new RuntimeException("Unknown output type provided!"); } } catch (IOException e) { e.printStackTrace(); } finally { if (reportProcessor != null) { reportProcessor.close(); } } }