Kotlin/Java Sending Gzip streaming response

642 Views Asked by At

I am trying to send a Response containing compressed (gzip) streamingOutput. My current code is :

@Path("/")
@Get
@Produces(MediaType.APPLICATION_JSON)
fun testRessource() : Response {
   val streamingOutput = TestOutputStream()
   val gzipStreamingOutput = CompressedHttpOutputStream(streamingOutput)
   val response = Response.ok(gzipStreamingOutput)
   response.setHeader("Content-Encoding", "gzip")

   return response
}

class TestOutputStream() : StreamingOutput {
   override fun write(outputStream: OutputStream) {
       val writer = BufferedWriter(OutputStreamWriter(outputStream))
       writer.write("{ "id" : 5 }")
       writer.flush()
   }
}


class CompressedHttpOutputStream(private val streamingOutput: StreamingOutput) : StreamingOutput {
    override fun write(outputStream: OutputStream) {
        val os = GZIPOutputStream(outputStream)
        streamingOutput.write(os)
        os.finish()
    }
}

When I do request this service, I get gibberish data in my browser. It seems like I am missing something even though my response have the following headers correctly set : Content-Encoding : gzip and Transfer-encoding : chunked.

In my unit tests with rest-assured, if I extract the body and read it through a GzipInputStream(), I am able to retrieve the json body.

When I replace :

val os = GZIPOutputStream(outputStream)

with

val os = DeflaterOutputStream(outputStream) and Content-Encoding : deflate

The output is correctly decompressed into json.

I am using Quarkus 2.6.0.Final.

Thank you for your help and insights !

1

There are 1 best solutions below

0
On

Nevermind, There is no need of CompressedHttpOutputStream. Quarkus implements gzip support for rest endpoints.

I just need to add @GZIP annotation to the endpoint and quarkus.resteasy.gzip.enabled=true in the application.properties.