Overriding HttpServletResponse

119 views
Skip to first unread message

Alexander Urmuzov

unread,
Nov 27, 2011, 9:21:38 PM11/27/11
to google...@googlegroups.com
I've got a filter that wraps HttpServletResponse like this:

        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        ...
        OutputStream out = httpResponse.getOutputStream();
        GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);

        filterChain.doFilter(servletRequest, wrapper);
        
        String content = new String(wrapper.getData(), httpResponse.getCharacterEncoding());
        String designedContent = design.process(content, features);
        out.write(designedContent.getBytes());

        out.close();

The problem comes when I try to inject Provider<HttpServletResponse>.
Injected HttpServletResponse is not an instance of GenericResponseWrapper, it is a original HttpServletResponse. So it breaks my application.
Maybe it is a correct behavior, but i need to override HttpServletResponse binding. How can I achieve it?

Fred Faber

unread,
Nov 27, 2011, 10:35:16 PM11/27/11
to google...@googlegroups.com
Modules.override is the best way to override bindings, but you probably don't want to do that here given that other internals will depend on an injection of httprequest (and also I'm not sure if it's possible to use this technique to override httprequest in the first place).

As potential alternatives here, you could:

1) bind your wrapper to a provider that itself has an injection of the original HttpRequest, and inject the wrapper to the class that need it
2) decompose the benefits of the wrapper to see if you can substitute its use by means of injecting derived values from the original request

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/rmiLj9x_ws0J.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Sam Berlin

unread,
Nov 27, 2011, 10:44:58 PM11/27/11
to google...@googlegroups.com

The latest Git head has some fixes from Isaac that should allow subsequent filters in the chain to be injected with the supplied request/response.

sam

--

Alexander Urmuzov

unread,
Nov 28, 2011, 9:34:44 AM11/28/11
to google...@googlegroups.com
Thank you!

I tried, but have some problems:
1) Compilation of multibindings and grapher module fails
2) Some tests fails. In guice-servlet extension too. I turned tests off.
3) When I attach compiled atrifacts i've got this on startup:
java.lang.NoClassDefFoundError: com/google/inject/internal/util/$Maps
        at com.google.inject.assistedinject.BindingCollector.<init>(BindingCollector.java:34)
        at com.google.inject.assistedinject.FactoryModuleBuilder.<init>(FactoryModuleBuilder.java:206)

Is there any snapshot repository where I can get most recent artifacts?

--
С уважением, Урмузов Александр Алесандрович



2011/11/28 Sam Berlin <sbe...@gmail.com>:

Sam Berlin

unread,
Nov 28, 2011, 9:42:46 AM11/28/11
to google...@googlegroups.com
Are you compiling through maven or ant?  We have a continuous build system that checks the ant build, but not maven... If it's Maven, I'll look into why that's failing.

sam

Alexander Urmuzov

unread,
Nov 28, 2011, 9:43:45 AM11/28/11
to google...@googlegroups.com
Yes, it's maven.

Stuart McCulloch

unread,
Nov 28, 2011, 2:43:30 PM11/28/11
to google...@googlegroups.com
On 28 Nov 2011, at 14:34, Alexander Urmuzov wrote:

Thank you!

I tried, but have some problems:
1) Compilation of multibindings and grapher module fails

There's a failing test (MultibinderTest.testSetAndMapValueConflict) for a bug reported on the mailing list:


I couldn't see an issue for this so I created one just now: http://code.google.com/p/google-guice/issues/detail?id=670

The AbstractInjectorGrapherTest class has an annotation (@com.google.testing.testsize.MediumTest) that's not available from any dependency - this annotation should probably be removed.

2) Some tests fails. In guice-servlet extension too. I turned tests off.

These failures appear to be because the servlet tests now use the Throwables class - Guice core doesn't use Throwables, so it gets removed by jarjar when it embeds Guava inside Guice

Either we need to include Throwables in core (by altering the jarjar instructions) or we need to fix the tests so they don't depend on Throwables

( note that in the experimental sisu-guice branch we don't embed Guava anymore, which is another solution http://groups.google.com/group/google-guice/browse_thread/thread/d146d38116d00333 )

3) When I attach compiled atrifacts i've got this on startup:
java.lang.NoClassDefFoundError: com/google/inject/internal/util/$Maps
        at com.google.inject.assistedinject.BindingCollector.<init>(BindingCollector.java:34)
        at com.google.inject.assistedinject.FactoryModuleBuilder.<init>(FactoryModuleBuilder.java:206)


It sounds like you might be mixing up core + extensions built from trunk with older extensions - due to a change to the jarjar instructions in trunk, moving embedded guava classes from internal/... to internal/guava/..., you need to make sure all extensions are from trunk (ie. all at the same version).

FYI, you can use "mvn clean install -Dmaven.test.skip" to build the complete tree without hitting the test failures

Is there any snapshot repository where I can get most recent artifacts?

We host a CI build at Sonatype that deploys snapshots to:


but it's a bit stale because of the failing tests - I'll do a re-spin with them disabled to refresh the snapshots

Sam Berlin

unread,
Nov 28, 2011, 2:46:56 PM11/28/11
to google...@googlegroups.com
Thanks for listing these, Stuart!  I'll fix up the mistakes, and hopefully we'll soon have the mvn running continuously and this won't happen again.. (there's some various infra work we have to take care of first, but someday soon it will happen!)

sam

Stuart McCulloch

unread,
Nov 28, 2011, 2:54:58 PM11/28/11
to google...@googlegroups.com
On 28 Nov 2011, at 19:46, Sam Berlin wrote:

Thanks for listing these, Stuart!  I'll fix up the mistakes, and hopefully we'll soon have the mvn running continuously and this won't happen again.. (there's some various infra work we have to take care of first, but someday soon it will happen!)

No problem - btw, latest snapshots (guice + extensions) have just been deployed to https://repository.sonatype.org/content/groups/forge/com/google/inject/

Alexander Urmuzov

unread,
Nov 29, 2011, 7:41:19 AM11/29/11
to google...@googlegroups.com
Thank you very much, Sam, Stuart!


Stuart is absolutely right about mixing-up dependencies. 3.0 dependencies comes from jersey-guice that I use for rest web-services and from another library. <exclusions> for all artifacts that depend on guice solved the problem.

So, what strategy should I use for building my project now?
  1. Use guice-snapshot from sonatype snapshot repository
  2. Compile my own guice-snapshot before compiling main project
How long sould we wait for next release (4, 3.1, 3.0.1) that fix problem with HttpServletRequest?

--
С уважением, Урмузов Александр Алесандрович



2011/11/29 Stuart McCulloch <mcc...@gmail.com>
Reply all
Reply to author
Forward
0 new messages