Reputation: 1379
I'm writing a Play! application which exposes a REST API allowing users to generate PDF reports. I'm constrained by the requirements to use a old Java API to do the actual report generation. That library has a method generate(OutputStream out, ...)
, i.e. it takes a java.io.OutputStream
where it writes the resulting report.
My problem is integrating this with Play/Akka to serve the contents in Chunked Encoding. To that end, I need to create an Enumerator[Array[Byte]]
which somehow contains the OutputStream
from the Java library. I have come up with a working solution that uses a PipedOutputSteam
/PipedInputStream
duo to pipe the output from the library to Enumerator using Enumerator.fromStream
.
I'm wondering if there is a better way to achieve this, but I can't seem to find an explicit example in the Akka or Play! documentations that integrates Enumerators with OutputStreams. I'm aware that the Java library's blocking IO is a limiting factor on designing a better solution, but maybe there's a neater way to do this. Any thoughts?
Follow up
Assuming I use Enumerator.outputStream
, what would be a safe way to move the actual report generation to another actor (possibly on another machine)? For instance, I'm guessing sending the OutputStream isn't safe (and would only work locally).
Upvotes: 4
Views: 733
Reputation: 11244
Play 2.1 has the following method: Enumerator.outputStream(a: (OutputStream) ⇒ Unit): Enumerator[Array[Byte]]
which probably does exactly what you want.
It's implementation is specifically using other Play 2.1 classes, so if you are using Play 2.0 you would need to do some extra digging to achieve the same thing.
Upvotes: 1