org.primefaces.model.StreamedContent Java Examples

The following examples show how to use org.primefaces.model.StreamedContent. 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: WorkbookService.java    From Html5_Spreadsheet_Editor_by_Aspose.Cells_for_Java with MIT License 6 votes vote down vote up
public StreamedContent getOutputFile(int saveFormat) {
    if (!isLoaded()) {
        return null;
    }

    byte[] buf;
    String ext = getExtensionForSaveFormat(saveFormat);

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        getAsposeWorkbook().save(out, saveFormat);
        buf = out.toByteArray();
    } catch (Exception x) {
        LOGGER.throwing(null, null, x);
        msg.sendMessageDialog("Could not export", x.getMessage());
        return null;
    }

    return new DefaultStreamedContent(new ByteArrayInputStream(buf), "application/octet-stream", "Spreadsheet." + ext);
}
 
Example #2
Source File: NetDumperBean.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public StreamedContent downloadFile(File file) {

		if (!file.exists()) {
			logger.info("File {} does not exist", file.getPath());
			BeanUtil.addErrorMessage("File does not exist", "");
			return null;
		}
		
		try {

            InputStream stream = new BufferedInputStream(new FileInputStream(file));

			return new DefaultStreamedContent(stream, "*/*", file.getName());

        } catch (IOException e) {
			logger.error(e.getMessage(), e);
			BeanUtil.addErrorMessage("Download failed", e.getMessage());
		}
		
		return null;
	}
 
Example #3
Source File: ToolsBean.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public StreamedContent getConverted() {

        try {
            File outputFile = Files.createTempFile("merged", ".csv").toFile();
            MergeMatrix.mergeMatrix(outputFile, uploadedFiles);
            for (File matrixFile: uploadedFiles) {
                matrixFile.delete();
            }
            uploadedFiles.clear();

            return new DefaultStreamedContent(new FileInputStream(outputFile), ContentType.DEFAULT_BINARY.toString(), outputFile.getName());
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            BeanUtil.showMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ExceptionUtils.getStackTrace(e));
            return null;
        }
    }
 
Example #4
Source File: SessionScopeBean.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public synchronized StreamedContent putImage(Object key, byte[] image, String mimeType) {
	if (image != null && image.length > 0 && mimeType != null && mimeType.length() > 0) {
		return imageStore.put(key, new DefaultStreamedContent(new ByteArrayInputStream(image), mimeType));
	} else {
		return imageStore.remove(key);
	}
}
 
Example #5
Source File: DownloadBean.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getStrContent() {
logger.info("getStrContent invoked {}", getUser());
      if(selectedFiles.length != 1) {
          return null; // Message?
      }
      if(selectedFiles[0].isDirectory()) {
          return null; // Message?
      }

      StreamedContent content = selectedFiles[0].getStrContent();
      return content == null ? null : content;
  }
 
Example #6
Source File: SessionScopeBean.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public synchronized StreamedContent getImage() {
	FacesContext context = FacesContext.getCurrentInstance();
	if (PhaseId.RENDER_RESPONSE.equals(context.getCurrentPhaseId())) {
		// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
		return new DefaultStreamedContent();
	} else {
		// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
		String uuid = WebUtil.getParamValue(GetParamNames.UUID);
		if (imageStore.containsKey(uuid)) {
			return imageStore.get(uuid);
		}
		return new DefaultStreamedContent();
	}
}
 
Example #7
Source File: FileAdapter.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getStrContent()
{
       if(!file.exists()) {
           return null;
       }

	try {
		InputStream stream = new BufferedInputStream(new FileInputStream(file));
		return new DefaultStreamedContent(stream, "*/*", file.getName());
	}
	catch(IOException e) {
		logger.error(e.getMessage(), e);
	}
	return null;
}
 
Example #8
Source File: MessagesBean.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getResultsInCSV() {
logger.info("getResultsInCSV invoked {}", getUser());

      if(selectedOptions.isEmpty() && !includeRawMessage) {
          BeanUtil.addWarningMessage("No columns selected", "Select at least one column");
          return null;
      }

      if(messageLazyModel.getRowCount() != 0) {
	try {
              String fileNameCsv = "query_result_" + UUID.randomUUID();
		File temp = File.createTempFile(fileNameCsv, ".csv");
		CsvMessageWriter writer = new CsvMessageWriter(selectedOptions, includeRawMessage);
              writer.writeAndClose(temp, messageLazyModel.load(0, messageLazyModel.getRowCount()));

		File zipFile = new File(temp.getParent(), "messages.zip");
		AppZip appZip = new AppZip();
		appZip.generateFileList(temp);
		appZip.zipIt(zipFile.getAbsoluteFile().toString());

		InputStream stream = new FileInputStream(zipFile);
		return new DefaultStreamedContent(stream, "application/zip", "Messages.zip");
	} catch (IOException e) {
		logger.error("Could not export messages", e);
		BeanUtil.addErrorMessage("Could not export messages", e.getMessage());
		return null;
	}
  	} else {
	BeanUtil.addWarningMessage("No data to export", "Table messages is empty");
}
  	return null;
  }
 
Example #9
Source File: EnvironmentBean.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getServicesInZip() {
	logger.info("getServicesInZip invoked {}", getUser());

	try {
		InputStream stream = new FileInputStream(BeanUtil.getSfContext().getServiceMarshalManager().packInZip(exportServices()));
		return new DefaultStreamedContent(stream, "application/zip", "Services.zip");
	} catch (FileNotFoundException e) {
		logger.error(e.getMessage(), e);
	}
	return null;
}
 
Example #10
Source File: DictionaryEditorModel.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getDownloadFiles() {

        try {
            return getNewFile();
        } catch (IOException | RuntimeException e) {
            logger.error("Can not pack current dictionary.", e);
            BeanUtil.showMessage(FacesMessage.SEVERITY_ERROR, "Error",
                                 "can not pack current dictionary." + e.getMessage());
            return null;
        }

    }
 
Example #11
Source File: SessionScopeBean.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public SessionScopeBean() {
	auth = new AuthenticationVO();
	imageStore = new MaxSizeHashMap<Object, StreamedContent>(WebUtil.IMAGE_STORE_MAX_SIZE);
	columnManager = new ColumnManagementBean();
	logon = null;
	failedAttempts = 0;
	authenticationFailed = false;
	localPasswordRequired = false;
	authenticationFailedMessage = null;
}
 
Example #12
Source File: ResourceHandlerWrapper.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void handleResourceRequest(FacesContext context) throws IOException {
	Map<String, String> params = context.getExternalContext().getRequestParameterMap();
	String library = params.get("ln");
	String dynamicContentId = params.get(Constants.DYNAMIC_CONTENT_PARAM);
	if (dynamicContentId != null && library != null && library.equals("primefaces")) {
		Map<String, Object> session = context.getExternalContext().getSessionMap();
		try {
			String dynamicContentEL = (String) session.get(dynamicContentId);
			if (!ALLOWED_EXPRESSIONS.contains(dynamicContentEL)) {
				throw new Exception("prevented EL " + dynamicContentEL);
			}
			System.out.println(dynamicContentEL);
			ELContext elContext = context.getELContext();
			ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), dynamicContentEL, StreamedContent.class);
			StreamedContent content = (StreamedContent) ve.getValue(elContext);
			HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
			response.setContentType(content.getContentType());
			byte[] buffer = new byte[2048];
			int length;
			InputStream inputStream = content.getStream();
			while ((length = (inputStream.read(buffer))) >= 0) {
				response.getOutputStream().write(buffer, 0, length);
			}
			response.setStatus(200);
			response.getOutputStream().flush();
			context.responseComplete();
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Error in streaming dynamic resource" + e.getMessage());
		} finally {
			session.remove(dynamicContentId);
		}
	} else {
		super.handleResourceRequest(context);
	}
}
 
Example #13
Source File: TestScriptsBean.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public StreamedContent getMatrixInZip() {
	logger.debug("getMatrixInZip invoked {}", BeanUtil.getUser());
	try {
		InputStream stream = new FileInputStream(packZip());
		return new DefaultStreamedContent(stream, "application/zip", "matrix.zip");
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		BeanUtil.showMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
	}

	return null;

}
 
Example #14
Source File: FileDownloadManagedBean.java    From journaldev with MIT License 4 votes vote down vote up
public void setContent(StreamedContent content) {
	this.content = content;
}
 
Example #15
Source File: FileDownloadManagedBean.java    From journaldev with MIT License 4 votes vote down vote up
public StreamedContent getContent() {
	return content;
}
 
Example #16
Source File: InvestmentSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public void setFile(StreamedContent file) {
	this.file = file;
}
 
Example #17
Source File: InvestmentSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public StreamedContent getFile() {
	return file;
}
 
Example #18
Source File: InputFieldBean.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
public StreamedContent getFileStreamedContent() throws Exception {
	if (isHasImage()) {
		return new DefaultStreamedContent(new ByteArrayInputStream(in.getDatas()), in.getMimeType(), in.getFileName());
	}
	return null;
}
 
Example #19
Source File: FileDownloadController.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
public void setFile(StreamedContent file) {
    this.file = file;
}
 
Example #20
Source File: FileDownloadController.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
public StreamedContent getFile() {
    System.out.println("return file " + file.getName());
    return file;
}
 
Example #21
Source File: ModelTemplateContainerController.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
public void setFileData(StreamedContent fileData) {
    this.fileData = fileData;
}
 
Example #22
Source File: ModelTemplateContainerController.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
public StreamedContent getFileData() {
    return fileData;
}
 
Example #23
Source File: MoneyTransferBean.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
public StreamedContent getReimbursementsPdfNoTrialStreamedContent() throws Exception {
	return getReimbursementsPdfStreamedContent(null);
}
 
Example #24
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public StreamedContent getFile1() {
	return file1;
}
 
Example #25
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public void setFile1(StreamedContent file1) {
	this.file1 = file1;
}
 
Example #26
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public StreamedContent getFile2() {
	return file2;
}
 
Example #27
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public void setFile2(StreamedContent file2) {
	this.file2 = file2;
}
 
Example #28
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public StreamedContent getFile3() {
	return file3;
}
 
Example #29
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public void setFile3(StreamedContent file3) {
	this.file3 = file3;
}
 
Example #30
Source File: TransactionSummaryController.java    From primefaces-blueprints with The Unlicense 4 votes vote down vote up
public StreamedContent getFile() {
	return file;
}