users@grizzly.java.net

Re: Grizzly 2 + Guice + Jersey

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Thu, 7 Jun 2012 16:37:08 -0400

Oh, one other thing. I followed your advice and looked at the unit
tests for jersey:

http://java.net/projects/jersey/sources/svn/content/trunk/jersey/contribs/jersey-guice/src/test/java/com/sun/jersey/guice/AbstractGuiceGrizzlyTest.java

In his .startServer() it does I think pretty much same as me except
that I'm making an HttpServer with .createSimpleServer() and they're
using a factory that's in a package I don't feel I should particularly
need (am I wrong?)



On Thu, Jun 7, 2012 at 4:16 PM, Christopher Piggott <cpiggott_at_gmail.com> wrote:
> Actually, I have a perhaps-useful piece of information that might
> help.  It seems this works to map the filter:
>
>                reg.addMappingForUrlPatterns(null, "/*");
>
> so that's good.  Now my context listener gets invoked, guice gets set
> up, jersey finds all my resource classes and providers.  That's all
> good.  But when I go to actually find a request I get an index out of
> bounds exception.
>
> This is getting into an area i'm not familiar with, and I'm not sure
> if the problem is jersey, grizzly, or I'm still missing something that
> I have to configure.  The actual exception seems to occur inside
> FilterChainImpl.invokeFilterChain() when it tries to do
> doFilter(request, response).
>
>
> I'm lost.
>
>
> 16:09:59.505 ERROR c.s.j.s.i.a.WebApplicationImpl - The
> RuntimeException could not be mapped to a response, re-throwing to the
> HTTP container
> java.lang.StringIndexOutOfBoundsException: String index out of range: -1
>        at java.lang.String.substring(String.java:1937) ~[na:1.6.0_26]
>        at java.lang.String.substring(String.java:1904) ~[na:1.6.0_26]
>        at com.sun.jersey.spi.container.ContainerRequest.getEncodedPath(ContainerRequest.java:377)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.ContainerRequest.getPath(ContainerRequest.java:370)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1477)
> [jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
> [jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
> [jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
> [jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
> [jersey-servlet-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
> [jersey-servlet-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
> [jersey-servlet-1.12.jar:1.12]
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
> [guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
> [guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
> [guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
> [guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
> [guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
> [guice-servlet-3.0.jar:na]
>        at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:137)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:249)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:185)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:196)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:169)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at java.lang.Thread.run(Thread.java:662) [na:1.6.0_26]
> 16:09:59.506 ERROR o.g.grizzly.servlet.ServletHandler - service exception:
> javax.servlet.ServletException:
> java.lang.StringIndexOutOfBoundsException: String index out of range:
> -1
>        at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:139)
> ~[grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
> ~[grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:249)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:185)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:196)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:164)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:169)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547)
> [grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at java.lang.Thread.run(Thread.java:662) [na:1.6.0_26]
> Caused by: java.lang.StringIndexOutOfBoundsException: String index out
> of range: -1
>        at java.lang.String.substring(String.java:1937) ~[na:1.6.0_26]
>        at java.lang.String.substring(String.java:1904) ~[na:1.6.0_26]
>        at com.sun.jersey.spi.container.ContainerRequest.getEncodedPath(ContainerRequest.java:377)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.ContainerRequest.getPath(ContainerRequest.java:370)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1477)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
> ~[jersey-server-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
> ~[jersey-servlet-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
> ~[jersey-servlet-1.12.jar:1.12]
>        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
> ~[jersey-servlet-1.12.jar:1.12]
>        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
> ~[grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
> ~[guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
> ~[guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
> ~[guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
> ~[guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
> ~[guice-servlet-3.0.jar:na]
>        at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
> ~[guice-servlet-3.0.jar:na]
>        at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:137)
> ~[grizzly-http-servlet-server-2.2.9.jar:2.2.9]
>        ... 21 common frames omitted
>
>
>
> On Thu, Jun 7, 2012 at 4:05 PM, Christopher Piggott <cpiggott_at_gmail.com> wrote:
>> I still don't have this working.  I would like to ask some questions
>> that maybe will lead me to the right solution.
>>
>> With Jersey + Guice, what I normally do is set up a filter
>> (GuiceFilter) and map it to "/*" ... and add a
>> GuiceServletContextListener.,   The context listener creates the main
>> injector using whatever guice modules you tell it; the modules usually
>> end with something like:
>>
>>                serve("*").with(GuiceContainer.class);
>>
>> To create an entire server, the trick in 1.9.4 is to do this:
>>
>>                ServletAdapter adapter = new ServletAdapter();
>>                adapter.addServletListener( MyContextListener.class.getName() );
>>
>>                GuiceFilterfilter = new GuiceFilter();
>>                adapter.addFilter( filter, "guiceFilter", null );
>>
>>                GrizzlyWebServerws = new GrizzlyWebServer( 8888, ".", use_ssl );
>>                GrizzlyWebServerProvider.reset( ws );
>>
>>                ws.addGrizzlyAdapter( adapter, new String[] { "/" } );
>>
>>
>> So the more refined (perhaps) question is how to approximate this with
>> grizzly 2.  What I tried so far, that doesn't work, is this:
>>
>>                HttpServer ws = HttpServer.createSimpleServer(".", 8890);
>>
>>                WebappContext ctx = new WebappContext("hub", "/");
>>                ctx.addListener(CustHubContextListener.class);
>>
>>                FilterRegistration reg = ctx.addFilter("gf", GuiceFilter.class);
>>                /* Add filter mapping - how? */
>>
>>                ctx.deploy(ws);
>>
>> When I do this, I indeed get a server up and running, but it doesn't
>> actually answer amy queries.  It says:
>>
>>
>> 15:50:22.013 INFO  o.g.grizzly.servlet.WebappContext - [hub] Filter
>> [com.google.inject.servlet.GuiceFilter] registered for url pattern(s)
>> [[]] and servlet name(s) [[]]
>> 15:50:22.013 INFO  o.g.grizzly.servlet.WebappContext - Application
>> [hub] is ready to service requests.  Root: [/].
>> 15:50:22.064 INFO  o.g.g.http.server.NetworkListener - Started
>> listener bound to [0.0.0.0:8890]
>> 15:50:22.067 INFO  o.g.grizzly.http.server.HttpServer - [HttpServer] Started.
>>
>>
>> but two things go wrong.  First, I dno't know how to map the filter to
>> "/*" ... a FilterRegistration doesn't have an .addMapping().  it has:
>>
>>                addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes,
>> String[] servletNames)
>>
>> which I don't understand (it takes an EnumSet?) ...   Second, even
>> without regard to this, it never actually invoked my context listener,
>> and I'm not sure why.  (maybe a result of the filter never getting
>> mapped?)