Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Documentation for Manipulate Content by HttpResponseInterceptor #68

Open
d0x opened this issue Aug 23, 2012 · 1 comment
Open

Comments

@d0x
Copy link

d0x commented Aug 23, 2012

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
Copy link

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);
            }
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants