Skip to content

Instantly share code, notes, and snippets.

@Bradleykingz
Created April 2, 2018 07:48
Show Gist options
  • Save Bradleykingz/318932725121dc479258e38e24044531 to your computer and use it in GitHub Desktop.
Save Bradleykingz/318932725121dc479258e38e24044531 to your computer and use it in GitHub Desktop.
Edit ServletResponse body inside ZuulFilter
package com.dummy.dummydata.filter;
import com.dummy.dummydata.pojo.*;
import com.google.gson.Gson;
import com.netflix.servo.jsr166e.ThreadLocalRandom;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;
@Component
//Can be any filter
public class DummyFilter extends ZuulFilter {
Logger logger = Logger.getLogger(getClass().getName());
@Override
public String filterType() {
return POST_TYPE;
}
@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest servletRequest = context.getRequest();
HttpServletResponse servletResponse = context.getResponse();
//Check if response is gzipped
boolean stream = context.getResponseGZipped();
if (stream) {
InputStream inputStream = context.getResponseDataStream();
try {
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
String body = StreamUtils.copyToString(gzipInputStream, Charset.defaultCharset());
//Convert from json to POJO
RandomPojo randomPojo = new Gson().fromJson(body, RandomPojo.class);
//Do some editing here.
// eg. randomPojo.setName("randomName");
//Convert back to json
String gsonResponse = new Gson().toJson(randomPojo);
//Recompress json
InputStream responseInputStream = new ByteArrayInputStream(gsonResponse.getBytes());
InputStream gzippedResponse = compress(responseInputStream);
context.setResponseDataStream(gzippedResponse);
context.setResponseGZipped(true);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public InputStream compress(@Nonnull final InputStream inputStream) {
InputStream zipInputStream = null;
try {
ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(bytesOutput);
try {
byte[] buffer = new byte[10240];
for (int length = 0; (length = inputStream.read(buffer)) != -1; ) {
gzipOutput.write(buffer, 0, length);
}
} finally {
try {
inputStream.close();
} catch (IOException ignore) {
}
try {
gzipOutput.close();
} catch (IOException ignore) {
}
}
zipInputStream = new ByteArrayInputStream(bytesOutput.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return zipInputStream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment