Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using filters with Grizzly server running a Jersey REST service

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?

like image 516
Andy Avatar asked Sep 17 '09 15:09

Andy


2 Answers

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.

like image 186
ra9r Avatar answered Sep 28 '22 21:09

ra9r


Have you tried:

((ServletAdapter) threadSelector.getAdapter()).addFilter()
like image 39
Alex Kamburov Avatar answered Sep 28 '22 20:09

Alex Kamburov