如何在ZuulFilter中处理gzip编码

时间:2019-04-17 17:42:34

标签: java spring netflix-zuul

我有一个ZuulFilter,它在代理后会稍微改变响应。这在标准情况下可以正常工作

@Component
public class ResponseEnhancerFilter extends ZuulFilter {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public String filterType() {
        return "post";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        try (final InputStream is = ctx.getResponseDataStream()) {
            String response = StreamUtils.copyToString(is, getEncoding(ctx));
            ctx.setResponseBody(response.replaceAll("regex", "replacement"));
        } catch (IOException e) {
        }
        return null;
    }

    private Charset getEncoding(RequestContext ctx) {
        return Optional.ofNullable(ctx.getResponse().getCharacterEncoding())
                .map(Charset::forName)
                .orElse(Charsets.UTF_8);
    }
}

但是当服务器的响应路由到getResponseDataStream中返回gzip响应时,我得到的是gzip消息,当然替换是行不通的。

我看到上下文中涉及gzip的方法有两种:getResponseGZipped和isGzipRequested

在zuul中是否有处理这种过滤器的标准方法?

0 个答案:

没有答案