sample-zuul-filters
sample-zuul-filters copied to clipboard
Example with GZipped response
Hello, thanks for examples! How to modify response data if I need to return it GZipped?
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));