[2.3.8 Scala] Trying to send an InputStream through Result... failing

75 visualitzacions
Ves al primer missatge no llegit

JoeT

no llegida,
16 d’abr. 2015, 15:22:5716/4/15
a play-fr...@googlegroups.com
For various esoteric reasons, I'm not using the Play WS library to do an HTTP call in my controller.  I am using the "Google" HTTP client and have implemented it such that I can get the response body back in a ByteArrayInputStream which as I understand it should be able to translate to 

play.api.libs.iteratee.Enumerator[scala.Array[scala.Byte]]

using 

Enumerator.fromStream

So, in what seems to be a fairly standard pattern, I'm trying to send that byte array back as the body of the response from Play.  I dug up somewhere that I could use a controller with Action.async and return my own Result from a Future.  So I ended up originally with something like this:

  def getFromBackend = Action.async {

   
GoogleHttpClient.getStream(esUrl).map {
     
case (status, body, headers, responseOpt) =>

       
Result(ResponseHeader(status, headers.map( kv => { (kv._1, kv._2.toString) })), Enumerator.fromStream( body ) )
   
}
 
}

A few things of note:

"body" is a ByteArrayInputStream which was created from the http client input stream which is closed at the point that the Result is created.  This should work fine as ByteArrayInputStream is an object that lives in memory and can be used after the stream is closed.

So here's the problem:  the response is never sent to the client (Postman) UNTIL I close the Play application.  It appears to "hang" (just sits there, Postman says "Loading..." until I kill the app THEN the response appears in Postman as I would expect.

Over the course of debugging this issue, I wanted to eliminate the stream as being the issue so I eliminated the stream from the Result and replaced the above Result with this:

Result(ResponseHeader(status, headers.map( kv => { (kv._1, kv._2.toString) })),Enumerator.enumerate( Seq( Array( 0.toByte, 1.toByte ))))


And saw the same issue - the response was not received by the client until I killed the app at which point it appeared immediately.

I've spent several hours on this at this point (Googling stuff, reading docs, etc) and am at a loss as to how to continue.  I feel like I must be missing something but could really use some help in figuring out what it is I'm missing.  Thanks for any help you can provide.



JoeT

no llegida,
16 d’abr. 2015, 17:54:3416/4/15
a play-fr...@googlegroups.com
The issues turned out to be the headers map...

My mistake was doing this:

 headers.map( kv => { (kv._1, kv._2.toString) })

 which caused content-length to be interpreted by Postman incorrectly and appear to hang even though Play had sent the response.  Lesson learned...
Respon a tots
Respon a l'autor
Reenvia
0 missatges nous