webmetrics / browsermob-proxy

NOTICE: this project has been forked and is being maintained at https://github.com/lightbody/browsermob-proxy
https://github.com/lightbody/browsermob-proxy
Apache License 2.0
234 stars 773 forks source link

Enhance Documentation for Manipulate Content by HttpResponseInterceptor #68

Open d0x opened 12 years ago

d0x commented 12 years ago

Could you place put an example how to manipulate the content with the HttpResponseInterceptor?

It's not clear how to replace the HttpEntity.

In this Example I like to convert all Text to uppercase:

@Test
public void shoudConvertEverythingToUpperCase() throws ClientProtocolException, IOException
{
    final DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

    defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException
        {
            final HttpEntity entity = response.getEntity();

            final HttpEntity upperCaseEntity = makeAllUppercase(entity);

            response.setEntity(upperCaseEntity);
        }

        private HttpEntity makeAllUppercase(final HttpEntity entity)
        {
            // how to uppercase everything and return the cloned HttpEntity
            return null;
        }
    });

    final HttpResponse httpResponse = defaultHttpClient.execute(new HttpGet("http://opensource.webmetrics.com/browsermob-proxy/"));

    assertTrue(StringUtils.isAllUpperCase(EntityUtils.toString(httpResponse.getEntity())));
}
vasilievip commented 11 years ago

Here is how you can get access to content:


        server.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                EntityEnclosingRequestWrapper wrapper = (EntityEnclosingRequestWrapper) request;
                requests.add((ClonedInputStream) wrapper.getEntity().getContent());
                try {
                    Field consumed = EntityEnclosingRequestWrapper.class.getDeclaredField("consumed");
                    consumed.setAccessible(true);
                    consumed.set(wrapper, Boolean.FALSE);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

        server.addResponseInterceptor(new HttpResponseInterceptor() {
            @Override
            public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
                ClonedInputStream cloned = new ClonedInputStream(httpResponse.getEntity().getContent());
                try {
                    Field content = BasicHttpEntity.class.getDeclaredField("content");
                    content.setAccessible(true);
                    content.set(httpResponse.getEntity(), cloned);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                responses.add(cloned);
            }
        });