Search code examples
spring-bootnetflix-zuul

Create HttpServletResponse object in Zuul custom filter


I have a Zuul custom filter of type PRE_TYPE. When I receive the request I want to prevent its routing and instead send a response, in this case a SOAP message, since I am simulating a web service response. My custom filter:

  @Component
public class CustomFilter extends ZuulFilter {
    private ThreadLocal<byte[]> buffers;

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

    @Override
    public Object run() {
        RequestContext ctx = getCurrentContext();
        ctx.unset();
        String s= "<soap:Envelope xmlns:......</soap:Envelope>";


 }

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

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

I need to create a HttpServletResponse and fill it with my response and write it to the output stream, so the client receives that response. How can I create the servletresponse object?


Solution

  • Try something like this:

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.setSendZuulResponse(false);
        ctx.setResponseBody("<soap:Envelope xmlns:......</soap:Envelope>");
        ctx.setResponseStatusCode(...);
        return null;
    }