osgi-jax-rs-connector
osgi-jax-rs-connector copied to clipboard
Filter/Interceptor to handle request and response processing for my Rest resources
I am trying to add some processing for all my requests and response of the Rest resources exposed inside my OSGI budle using the below classes
javax.ws.rs.container.ContainerRequestFilter, javax.ws.rs.container.ContainerResponseFilter
How do i hookup these filters iniside my Activator class? I was using the below method before i started using osgi-jax-rs-connector
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
this.tracker = new ServiceTracker(Activator.context, HttpService.class.getName(), null){
@Override
public Object addingService(ServiceReference reference) {
httpService = (HttpService) super.addingService(reference);
httpService.registerServlet("/rest/v1", new ServletContainer(), getJerseyServletParams(), null);
return httpService;
}
@Override
public void removedService(ServiceReference reference,
Object service) {
if (httpService == service) {
unregisterServlets();
httpService = null;
}
super.removedService(reference, service);
}
};
this.tracker.open();
}
private void unregisterServlets() {
if (this.httpService != null) {
httpService.unregister("/rest/v1");
}
}
private Dictionary<String, String> getJerseyServletParams() {
Dictionary<String, String> jerseyServletParams = new Hashtable();
jerseyServletParams.put("javax.ws.rs.Application", JerseyApplication.class.getName());
jerseyServletParams.put("com.sun.jersey.spi.container.ContainerRequestFilters", MyFilter.class.getName());
jerseyServletParams.put("com.sun.jersey.spi.container.ContainerResponseFilters", MyFilter.class.getName());
return jerseyServletParams;
}
I have created a request filter by simply using the @Provider annotation and registering the component using DS.
@Provider
@Component(service = ContainerRequestFilter.class)
public class StorageResourceFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
...
}
}
Thanks Bryan. That worked. Can i provide multiple types to service attribute in the same class. something like
@Component(service = ContainerRequestFilter.class,ContainerResponseFilter.class)
OSGi allows you to register a component as multiple services. I don't know if the publisher will do anything with that.