Reputation: 3659
I am trying to write a simple wrapper in grails that will read a binary file from a non-public back-end web-server and makes it available as a stream to a public server. The first question is what is the best method to handle such a solution considering that the Grails will attempt to render views by default.
I am attempting a solution which leads to an "Attempted read from closed stream." which apparently looks to avoid the view rendering by invoking response but I am not sure if this is a good solution anyway.
I am reading the back-end file through the following code:
def http = new HTTPBuilder(baseUrl)
http.request(method, ContentType.BINARY) {
uri.path = path
uri.query = query
response.success = { resp, inputstream ->
log.info "response status: ${resp.statusLine}"
resp.headers.each { h -> log.info " ${h.name} : ${h.value}" }
return inputstream
}
and later attempt to push it to the client through the following method in a controller:
def binary(){
response.outputStream << getBinaryContent("http://aaa" );
response.flush()
}
Error: "Attempted read from closed stream."
Upvotes: 0
Views: 604
Reputation:
If I'm not mistaken <<
will close the response so response.flush()
is invalid.
Here's the snippet that I use for CSV, just replace with your content type:
byte[] bytes = fileGenerated.getBytes() //fileGenerated is a File
response.setContentType("text/csv")
response.setContentLength(bytes.length)
response.setHeader("Content-Disposition", "attachment; filename=" + fileGenerated.getName())
response.outputStream << bytes
Upvotes: 1