sample-zuul-filters icon indicating copy to clipboard operation
sample-zuul-filters copied to clipboard

Example with GZipped response

Open ppolushkin opened this issue 7 years ago • 1 comments

Hello, thanks for examples! How to modify response data if I need to return it GZipped?

ppolushkin avatar Nov 29 '17 11:11 ppolushkin

had to do the same, went for some modifications on the existing ModifyResponseDataStreamFilter

RequestContext context = getCurrentContext();
InputStream stream = context.getResponseDataStream();
// uncompress
String body = StreamUtils.copyToString(new GZIPInputStream(stream),
    Charset.forName("UTF-8"));

// modify
body = "Modified gzipped response via setResponseBody(): " + body;

// compress again
ByteArrayOutputStream bos = new ByteArrayOutputStream(body.length());
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(body.getBytes("UTF-8"));
gzip.close();
byte[] compressed = bos.toByteArray();
bos.close();
context.setResponseDataStream(new ByteArrayInputStream(compressed));

axelhodler avatar Feb 10 '18 17:02 axelhodler