question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

when i add a filter to jersey , then Jersey error

See original GitHub issue

I’m using dropwizard 1.0.0 and the lastest dropwizard-guice runing success. But when I add a filter , errors comes here. this issue not happened in dropwizard 0.9.3 and dropwizard 0.8.4 ConnectorProtectFilter is an implment for ContainerRequestFilter

environment.jersey().register(ConnectorProtectFilter.class);

this is the ConnectorProtectFilter code

public class ConnectorProtectFilter implements ContainerRequestFilter {
    private Logger logger = LoggerFactory.getLogger(ConnectorProtectFilter.class);

    private static final int   RPC_PORT = 9999;

    @Context
    private HttpServletRequest request;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        String requestUri = request.getRequestURI();
        if (requestUri.startsWith("/rpc/") || requestUri.equalsIgnoreCase("/rpc")) {
            if (RPC_PORT != request.getServerPort()) {
                logger.error("Try visit protect path[remoteIp={},port={},path={}]", request.getRemoteHost(),
                        request.getServerPort(), request.getRequestURI());
                throw new WebApplicationException(Response.Status.NOT_FOUND);
            }
        }
    }
}
WARN  [2016-09-30 19:13:33,214] com.squarespace.jersey2.guice.JerseyGuiceUtils: It appears jersey2-guice-spi is either not present or in conflict with some other Jar: ServiceLocatorGeneratorImpl(hk2-locator, 984567559)
INFO  [2016-09-30 11:13:34,634] org.eclipse.jetty.util.log: Logging initialized @4046ms
INFO  [2016-09-30 11:13:34,875] io.dropwizard.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO  [2016-09-30 11:13:34,875] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO  [2016-09-30 11:13:34,889] com.hubspot.dropwizard.guice.AutoConfig: Added resource class: class com.wantu.dropwizard.test.HelloResource
INFO  [2016-09-30 11:13:34,890] io.dropwizard.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO  [2016-09-30 11:13:34,890] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO  [2016-09-30 11:13:34,892] io.dropwizard.server.ServerFactory: Starting test
INFO  [2016-09-30 11:13:35,107] org.eclipse.jetty.setuid.SetUIDListener: Opened application@101a461c{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
INFO  [2016-09-30 11:13:35,108] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@360e9c06{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
INFO  [2016-09-30 11:13:35,112] org.eclipse.jetty.server.Server: jetty-9.3.9.v20160517
WARN  [2016-09-30 11:13:36,078] 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 org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    ...
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:378)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
    at io.dropwizard.cli.Cli.run(Cli.java:75)
    at io.dropwizard.Application.run(Application.java:79)
    at com.wantu.dropwizard.test.App.main(App.java:14)
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)
    ... 77 more

WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
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.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:252)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.server.Server.start(Server.java:411)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:378)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
    at io.dropwizard.cli.Cli.run(Cli.java:75)
    at io.dropwizard.Application.run(Application.java:79)
    at com.wantu.dropwizard.test.App.main(App.java:14)
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)
    ... 77 more
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.wantu.dropwizard.test.ConnectorProtectFilter errors were found
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:246)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
    at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
    at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
    at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:122)
    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.Providers.getAllRankedProviders(Providers.java:247)
    at org.glassfish.jersey.server.ApplicationHandler.getProcessingProviders(ApplicationHandler.java:772)
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:537)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    ...
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:378)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
    at io.dropwizard.cli.Cli.run(Cli.java:75)
    at io.dropwizard.Application.run(Application.java:79)
    at com.wantu.dropwizard.test.App.main(App.java:14)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on com.wantu.dropwizard.test.ConnectorProtectFilter
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:386)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
    at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
    at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
    at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:122)
    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.Providers.getAllRankedProviders(Providers.java:247)
    at org.glassfish.jersey.server.ApplicationHandler.getProcessingProviders(ApplicationHandler.java:772)
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:537)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    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.processWithException(Errors.java:255)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:640)
    at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:419)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
    ...
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:378)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
    at io.dropwizard.cli.Cli.run(Cli.java:75)
    at io.dropwizard.Application.run(Application.java:79)
    at com.wantu.dropwizard.test.App.main(App.java:14)


WARN  [2016-09-30 11:13:36,091] /: unavailable
! 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)
! ... 77 common frames omitted
! Causing: 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.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
! at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
! ... 68 common frames omitted
! Causing: org.glassfish.hk2.api.MultiException: A MultiException has 3 exceptions.  They are:
! 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
! 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.wantu.dropwizard.test.ConnectorProtectFilter errors were found
! 3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.wantu.dropwizard.test.ConnectorProtectFilter
! 
! at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89)
! at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:249)
! at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
! at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
! at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
! at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
! at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
! at java.util.concurrent.FutureTask.run(FutureTask.java:266)
! at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
! at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
! at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:122)
! 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.Providers.getAllRankedProviders(Providers.java:247)
! at org.glassfish.jersey.server.ApplicationHandler.getProcessingProviders(ApplicationHandler.java:772)
! at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:537)
! at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
! at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
...
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.Server.doStart(Server.java:378)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)
! at io.dropwizard.Application.run(Application.java:79)
! at com.wantu.dropwizard.test.App.main(App.java:14)
INFO  [2016-09-30 11:13:36,125] org.eclipse.jetty.server.AbstractConnector: Started application@101a461c{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
INFO  [2016-09-30 11:13:36,130] org.eclipse.jetty.server.AbstractConnector: Started admin@360e9c06{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
ERROR [2016-09-30 11:13:36,136] io.dropwizard.cli.ServerCommand: Unable to start server, shutting down
! 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)
! ... 77 common frames omitted
! Causing: 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.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
! at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
! ... 68 common frames omitted
! Causing: org.glassfish.hk2.api.MultiException: A MultiException has 3 exceptions.  They are:
! 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
! 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.wantu.dropwizard.test.ConnectorProtectFilter errors were found
! 3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.wantu.dropwizard.test.ConnectorProtectFilter
! 
! at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89)
! at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:249)
! at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
! at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
! at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
! at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
! at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
! at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
! at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
! at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
! at javax.servlet.GenericServlet.init(GenericServlet.java:244)
! at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:640)
! ... 41 common frames omitted
! Causing: javax.servlet.ServletException: io.dropwizard.jersey.setup.JerseyServletContainer-157ec23b@8caccb8e==io.dropwizard.jersey.setup.JerseyServletContainer,1,false
! at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:661)
! at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:419)
! at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
! at org.eclipse.jetty.server.Server.doStart(Server.java:378)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
! at io.dropwizard.cli.Cli.run(Cli.java:75)
! at io.dropwizard.Application.run(Application.java:79)
! at com.wantu.dropwizard.test.App.main(App.java:14)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
heldeencommented, Oct 5, 2016

It has to do w/ how Guice, Guice Servlet, and Jersey2-Guice works vs just plain Jersey/HK2. When the JerseyGuiceUtils.install code is in place the org.glassfish.hk2.api.ServiceLocator that Jersey uses has a preference to satisfy injections from the Guice object graph before the HK2 locator is consulted. The Guice com.google.inject.servlet.ServletModule sets up a provider for javax.servlet.http.HttpServletRequest that is com.google.inject.servlet.RequestScoped and that is why it fails to provide the request object at Jersey startup time; there is no request happening then.

On the other hand if just Jersey/HK2 are involved than the javax.ws.rs.core.@Context annotated javax.servlet.http.HttpServletRequest that HK2 provides for injection is a proxy for a thread local pointer to the current request. The proxy is setup and injected at Jersey startup. As long as that proxy object is only accessed during a request there is no problem.

There are lots of ways to work around it. My earlier suggestion of just using the javax.ws.rs.container.ContainerRequestContext that is provided in the javax.ws.rs.container.ContainerRequestFilter#filter(ContainerRequestContext) is probably the best. The Guice way to get the same functionality as Jersey/HK would be to inject a javax.inject.Provider<javax.servlet.http.HttpServletRequest> instead of the barejavax.servlet.http.HttpServletRequest. Guice can inject a Provider at Jersey startup and as long as the the provider is only asked for the current request during a request there would be no errors.

I would also suggest not mixing JAX-RS and Servlet specs in your filter. Only use the JAX-RS core inside Jersey and only use Servlet inside Jetty.

Hope this helps!

1reaction
heldeencommented, Oct 4, 2016

Deleting that code breaks the binding for HK2 to Guice. You can get the info you need from the javax.ws.rs.container.ContainerRequestContext that is provided in the javax.ws.rs.container.ContainerRequestFilter#filter(ContainerRequestContext) like so:

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.getUriInfo().getBaseUri().getPort();
        requestContext.getUriInfo().getBaseUri().getPath();
    }

The reason it is failing is Jersey is initializing the filter at start time and there is no HTTP Request at that time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create filter to handle exception JERSEY - Stack Overflow
I need to create a filter that handles exception. In details when and exception occurres in my program with jersey filter I must...
Read more >
Jersey Filters and Interceptors - Baeldung
In this tutorial, we focused on understanding how filters and interceptors work in Jersey 2 and how we can use them in a...
Read more >
Jersey 2.37 User Guide - GitHub Pages
This is user guide for Jersey 2.37. We are trying to keep it up to date as we add new features. When reading...
Read more >
Jersey Notes - Java Hot Chocolate
You'll create a filter that implements ContainerRequestFilter plus ResourceFilter. If you wish to intercept a response, which will go back out as the...
Read more >
Implementing Jersey Client Filters (REST Binding Component ...
Do the following to add the required Jersey JAR files to the project: In the Project window, right-click the Java application, and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found