hstaudacher / osgi-jax-rs-connector

An OSGi - JAX-RS 2.0 Connector, software repository available on the link below
http://hstaudacher.github.io/osgi-jax-rs-connector
Other
191 stars 98 forks source link

Filter/Interceptor to handle request and response processing for my Rest resources #150

Open madhugarimilla opened 8 years ago

madhugarimilla commented 8 years ago

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;
    }
BryanHunt commented 8 years ago

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 {
    ...
  }
}
madhugarimilla commented 8 years ago

Thanks Bryan. That worked. Can i provide multiple types to service attribute in the same class. something like

@Component(service = ContainerRequestFilter.class,ContainerResponseFilter.class)

BryanHunt commented 8 years ago

OSGi allows you to register a component as multiple services. I don't know if the publisher will do anything with that.