0

In my Java EE web application I want to provide a download functionality of a file, which is created on the fly.

  • The user clicks a p:commandButton which triggers some action in the underlying CDI ViewScoped bean. (non-AJAX call)
  • From the CDI Bean my business logic is called, to do some security checks, prepare data etc. The service returns a DTO to the CDI Bean, containing the data which is required to create the requested file.
  • The next step is to send a POST request to my PHP and submit the DTO as JSON in the request body.
  • The PHP now creates the file based on the received information. I am already able to create the HTTP response with the correct header information and the file in the response body.

I am missing the last step how to ship this file to the user. Am I able to chain this response and directly send it to the user client? Or do I have to read the content, and create a new HTTP response in the CDI Bean manually? Maybe it would be even better to encapsualte the communication with PHP in the business logic (and hide it completely from the View) and only return the File to the CDI Bean. After this I would be able to create the response and ship the file to the user.

My thoughts, so far... Any ideas, recommendations how I should and could ship the file to the user?

stg
  • 2,757
  • 2
  • 28
  • 55
  • *the file in the response body* -- The actual file contents or the file name? – apokryfos Jun 29 '16 at 10:12
  • @apokryfos the file itself, not jsut the name. The file is not saved on the server, it's just in memory and thrown away afterwards – stg Jun 29 '16 at 10:18
  • Set the content type of the response to the correct MIME type (e.g. `text/json`), set `Content-Disposition` to `"attachment;filename=name_of_download.json"` fill the response output stream with the string you want to send and you're off. Check [this page](http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/) for a basic idea on what to do. – apokryfos Jun 29 '16 at 10:48
  • In JSF just do the same as you apparently successfully did in PHP: write file contents to response body along with necessary set of headers. – BalusC Jun 29 '16 at 10:51
  • @BalusC MY problem is not how to create a HTTP response in JSF, I already know how to do this. My problem is that the response is created by php and the client, which receives the response is the CDI Bean in this case. But the response of the php is exactly the response which I want to send to the user. It feels kind of wrong to handle the response in the CDI bean and create the very same Response again and send it back to the user. So I am wondering if it is possible to "redirect" the response directly to the user (or maybe this is a bad idea..?) – stg Jun 29 '16 at 10:56
  • Just either let HTML form submit to PHP directly or reimplement PHP logic in Java. Try at programmers.se if you still can't decide on this architectural question. – BalusC Jun 29 '16 at 11:01

0 Answers0