views:

860

answers:

1

I'm trying to use Grizzly to create a server to run a REST service that I've developed using Jersey. I'm creating the Grizzly server using:

final String baseUri = "http://localhost:9998/";
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages", "me.my.rest.package");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);

as all the examples I've found seem to suggest. This is fine, the server starts and is able to forward incoming requests to my resource classes.

However, the service implementation requires it to use a servlet filter. It appears that Grizzly supports the definition of filters and other similar servlet related configuration options, via the ServletAdapter class. My problem is that I can't work out how to define a filter when using a com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory which provides Jersey integration.

Any ideas?

+5  A: 

I think I might have something for you ...

GrizzlyWebServer ws = new GrizzlyWebServer(9999);
ServletAdapter jerseyServletAdapter = new ServletAdapter();
jerseyServletAdapter.setServletInstance(new ServletContainer());
jerseyServletAdapter.addInitParameter(
    "com.sun.jersey.config.property.packages", "me.my.rest.package");
jerseyServletAdapter.setServletPath("/api");

// HERE IS HOW YOU ADD A FILTER 
jerseyServletAdapter.addFilter(new MyFilter(), "HibernateSessionFilter", null);

ws.addGrizzlyAdapter(jerseyServletAdapter, null);

... its working for me, I hope it helps you as well.

raiglstorfer
Thank you! This is exactly what I need.
Andy