Andy
Andy

Reputation: 9048

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?

Upvotes: 12

Views: 6083

Answers (2)

Alex Kamburov
Alex Kamburov

Reputation: 405

Have you tried:

((ServletAdapter) threadSelector.getAdapter()).addFilter()

Upvotes: 1

ra9r
ra9r

Reputation: 4728

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.

Upvotes: 8

Related Questions