Java Code Examples for javax.faces.context.ExternalContext#getResponseOutputStream()

The following examples show how to use javax.faces.context.ExternalContext#getResponseOutputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatisticsReportingBean.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public void getResultsInCSV() {
    logger.info("getResultsInCSV invoked {}", System.getProperty("user.name"));
    try {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.responseReset();
        String reportName = getExportFileName();
        externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\"");
        OutputStream output = externalContext.getResponseOutputStream();
        statisticsReportHandler.writeReport(output);
        facesContext.responseComplete();
    } catch (Exception e) {
        logger.error("Could not export report", e);
        BeanUtil.addErrorMessage("Could not export report", e.getMessage());
    }
}
 
Example 2
Source File: TagGroupDimensionalReportBean.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public void getExported() {

		try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();
            externalContext.responseReset();
            externalContext.setResponseContentType("text/csv");
            String reportName = StatisticsUtils.createStatsPerTagsName();
            externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\"");

            OutputStream output = externalContext.getResponseOutputStream();
            StatisticsUtils.writeTagGroupReportToCsv(output, results);
            facesContext.responseComplete();
		} catch (IOException e) {

			logger.error("Could not export data to csv file", e);
			
			BeanUtil.addErrorMessage("Could not export statistics to csv file", "");
		}
		
	}
 
Example 3
Source File: TestScriptsHistoryBean.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public void getExported() {

		try {

            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();
            externalContext.responseReset();
            externalContext.setResponseContentType("text/csv");
            String reportName = StatisticsUtils.createScriptRunsHistoryName();
            externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\"");
            OutputStream output = externalContext.getResponseOutputStream();
            StatisticsUtils.writeScriptRunsHistory(BeanUtil.getSfContext(), output,
                                                   new ArrayList<>(Arrays.asList(selectedColumns)), lastResult,
                                                   exportWithTCs, exportWithActions, matrixInfo);
            facesContext.responseComplete();

		} catch (IOException e) {

			logger.error("Could not export data to csv file", e);

			BeanUtil.addErrorMessage("Could not export statistics to csv file", "");

		}
	}
 
Example 4
Source File: GeneratorDataBean.java    From microprofile-starter with Apache License 2.0 6 votes vote down vote up
private void download(byte[] archive) {
    String fileName = engineData.getMavenData().getArtifactId() + ".zip";
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    ec.responseReset();
    ec.setResponseContentType("application/zip");
    ec.setResponseContentLength(archive.length);
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    try {
        OutputStream outputStream = ec.getResponseOutputStream();

        outputStream.write(archive);
        outputStream.close();
    } catch (IOException e) {
        throw new JessieUnexpectedException("IO Error during download of ZIP");
    }

    // Important! Otherwise JSF will attempt to render the response which obviously will fail
    // since it's already written with a file and closed.
    fc.responseComplete();
}