dropwizard-guice icon indicating copy to clipboard operation
dropwizard-guice copied to clipboard

com.google.inject.OutOfScopeException on inject javax.servlet.http.HttpServletRequest

Open svenwb opened this issue 10 years ago • 11 comments

I updated to dropwizard-guice 1.0.0 Since then I do get an com.google.inject.OutOfScopeException when I try to inject the HttpServletRequest:

This is the method signature:

public Response uploadCsvFile(@PathParam("subject_id") String subjectId, @Context HttpServletRequest request)

This is my stacktrace:

WARN  [2016-08-30 12:57:27,713] [X-PEX-CORRELATION-ID:] org.glassfish.jersey.internal.Errors: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 1
com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped [javax.servlet.http.HttpServletRequest]. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
  at com.google.inject.servlet.InternalServletModule.provideHttpServletRequest(InternalServletModule.java:106) (via modules: com.google.inject.servlet.ServletModule -> com.google.inject.servlet.InternalServletModule)
  while locating javax.servlet.http.HttpServletRequest

1 error
    at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025)
    at com.squarespace.jersey2.guice.GuiceBindingDescriptor.create(GuiceBindingDescriptor.java:66)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
    at com.squarespace.jersey2.guice.GuiceScopeContext.findOrCreate(GuiceScopeContext.java:36)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
    at org.glassfish.jersey.internal.inject.ContextInjectionResolver.resolve(ContextInjectionResolver.java:126)
    at org.glassfish.jersey.server.internal.inject.DelegatedInjectionValueFactoryProvider$1.provide(DelegatedInjectionValueFactoryProvider.java:107)
    at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
    at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:90)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1689)
    at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:34)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
    at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:50)
    at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:44)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
    at com.signavio.pex.common.util.PexCorrelationIdFilter.doFilter(PexCorrelationIdFilter.java:32)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1174)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1106)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240)
    at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51)
    at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:396)
    at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68)
    at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:56)
    at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:524)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.google.inject.OutOfScopeException: Cannot access scoped [javax.servlet.http.HttpServletRequest]. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
    at com.google.inject.servlet.GuiceFilter.getContext(GuiceFilter.java:165)
    at com.google.inject.servlet.GuiceFilter.getOriginalRequest(GuiceFilter.java:147)
    at com.google.inject.servlet.ServletScopes$1$1.get(ServletScopes.java:107)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
    at com.google.inject.internal.InjectorImpl$2$1.call(InjectorImpl.java:1016)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
    at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1012)
    ... 64 more

Any idea what might be wrong here? Thanks

svenwb avatar Aug 30 '16 14:08 svenwb

Hmm I'll take a look. Out of curiosity, does it work if you inject a Provider<HttpServletRequest> into your constructor and then call requestProvider.get() inside of your uploadCsvFile method?

jhaber avatar Aug 30 '16 14:08 jhaber

Thanks for the advice, but unluckily I do still get the same exception.

svenwb avatar Aug 30 '16 15:08 svenwb

I ran into this as well.

It looks like the issue is caused by JerseyUtil.registerGuiceFilter, specifically:

environment.getApplicationContext().getContextPath() + "*"

Servlet and filter mappings don't need to take the server context path into account, "/*" should be sufficient.

The bug goes away when I unset my application context path, and works if I manually add the filter to match all.

Will push a patch momentarily

carterkozak avatar Sep 07 '16 17:09 carterkozak

I came here to suggest exactly what @cakofony did. The io.dropwizard.jersey.setup.JerseyServletContainer is being mapped to /* so it makes sense to map the GuiceFilter to the same.

heldeen avatar Sep 08 '16 19:09 heldeen

In the meantime this can be worked around by adding:

environment.servlets()
    .addFilter("Guice Filter", GuiceFilter.class)
    .addMappingForUrlPatterns(null, false, "/*");

carterkozak avatar Sep 09 '16 04:09 carterkozak

I had this problem even with 1.0.0.1. The culprit was the Session injection:

public Settings getSettings(@Session HttpSession session) { ... }

I could solve it by changing it to:

public Settings getSettings(@Context HttpServletRequest req) { HttpSession session = req.getSession(); ... }

jcolaco-nmz avatar Sep 27 '16 15:09 jcolaco-nmz

I am also getting the same error. Adding the below didn't help.

environment.servlets() .addFilter("Guice Filter", GuiceFilter.class) .addMappingForUrlPatterns(null, false, "/*");

Using DW version 1.0.6 and dw-guice version 1.0.0.3

priyadarsh avatar Mar 07 '17 14:03 priyadarsh

I was injecting http request into method parameter which is failing. When i moved the same to constructor it worked fine.

Not working - public void someMethod(@Context HttpServletRequest request ...

Working - public SomeConstructor(@Context HttpServletRequest servletRequest){...

priyadarsh avatar Mar 21 '17 15:03 priyadarsh

Injecting a provider for HttpServletRequest worked for me in DW 1.1.0 and dw-guice 1.0.6.0

ra2085 avatar Jul 20 '17 04:07 ra2085

Replacing HttpServletRequest with Provider<HttpServletRequest> worked for me with dw 1.2.1 and dw-guice 1.0.6.0.

gauravic08 avatar Dec 01 '17 11:12 gauravic08

We occasionally get this exception in our service when injecting a com.google.inject.Provider<HttpServletRequest> requestProvider into an exception mapper for our service. We have not been able to reproduce this issue locally by replaying the same requests but sometimes see the above exception in our service logs.

mattcoley avatar Apr 20 '18 19:04 mattcoley