Jetty AsyncMiddleManServlet with Dropwizard to decorate JSON payload response from an existing service

141 views
Skip to first unread message

Matt Duggan

unread,
Sep 21, 2017, 6:25:00 AM9/21/17
to dropwizard-user
Hi Everyone, 

I'm using Dropwizard v1.0.3 to provide a service that decorates JSON responses from another service based on some calculations. I've added jetty-proxy 9.3.11.v20160721 as a dependency to my project.

The intended flow is:-
1. [existing client]------request----->[new service]-----request--->[existing service]
2. [existing client]<----decorated response----[new service]<----response---[existing service]

The new service should pass requests from the existing client to the existing service at step 1 and update the responses at step 2.

I'm hoping to use Jetty's AsyncMiddleManServlet with a ContentTransformer to do this based on Jetty's own tests at the following link, see the testAfterContentTransformer method at line 886.

I've tried adding the proxy as follows but I get a 502 Bad Gateway error come back in curl when pushing requests through it. I was hoping to see just the data returned from the existing service via this.  I've tried the AsyncMiddleManServlet.Transparent servlet (without the transformer etc) and that works fine. 

Any advice would be most appreciated.

thx
Matt

final AsyncMiddleManServlet proxy = new  AsyncMiddleManServlet()

{

    @Override

    protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)

    {

        return new AfterContentTransformer()

        {

            @Override

            public boolean transform(Source source, Sink sink) throws IOException

            {

                InputStream input = source.getInputStream();

                Map<String,Object> json = mapper.readValue(new InputStreamReader(input, "UTF-8"), new TypeReference<Map<String,Object>>() {});

                // transform here...

                try (OutputStream output = sink.getOutputStream())

                {

                    output.write(json.toString().getBytes(StandardCharsets.UTF_8));

                    return true;

                }

            }

         };

    }

};

final ServletRegistration.Dynamic proxy = environment.servlets().addServlet("proxy", proxy);

proxy.setInitParameter("proxyTo", configuration.getExistingService());

proxy.setInitParameter("prefix", "/");

proxy.addMapping("/*");


Reply all
Reply to author
Forward
0 new messages