Search code examples
jakarta-eeservletsosgibundleapache-felix

Servlet registration in OSGi fails with custom org.osgi.service.http.HttpContext object


I am working on servlet registration in osgi bundle.To support MIME mapping I want to write custom implementation of HttpContext and want HttpService to call it instead of default HttpContext.

public final class Activator implements BundleActivator{
   ...
   public void start( BundleContext bc ){

   private ServiceReference httpServiceRef;
   httpServiceRef = bc.getServiceReference( HttpService.class.getName());
   final HttpService httpService = (HttpService) bc.getService( httpServiceRef );

   httpService.registerServlet("/hi",new MyServlet(),new MyHttpContext());}

MyHttpContext looks like this:

public class MyHttpContext implements HttpContext {

@Override
public URL getResource(String name) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public String getMimeType(String name) {
    // TODO Auto-generated method stub
    System.out.println("Name: "+name);
    if (name.endsWith(".jpg"))
        return "image/jpeg";
    else if (name.endsWith(".pdf"))
        return "application/pdf";
    else if (name.endsWith(".txt"))
        return "text/plain";
    else
        return "text/html";

}

@Override
public boolean handleSecurity(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    // TODO Auto-generated method stub
    return false;
}

The servlet is not getting called when I try to hit the correct url. However,it works if I pass null as a third parameter in registerServlet() in which case httpservice internally uses default HttpContext.

What can probably be wrong with my custom implementation? Am I missing something in getResource() method?


Solution

  • See the javadoc of handleSecurity function:

    returns true if the request should be serviced, false if the request should not be serviced and Http Service will send the response back to the client.