0

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?

WillMcavoy
  • 1,795
  • 4
  • 22
  • 34

1 Answers1

1

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.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • Thanks! That worked! I have embedded osgi inside my tomcat 7.x using bridge.And the bundle with servlet registration runs over this bridge. Upon invocation,my servlet serves a .pdf file which can be downloaded through browser. I want to test if my custom httpcontext object is used by httpservice to get the MIME type of this file. However,no print statement in overridden getMimeType() is printed on console.I am not able to figure out when this overridden method is invoked. How can I ensure that MIME type mappings are handled by MyHttpContext and not by tomcat? – WillMcavoy Jun 27 '14 at 20:53
  • I guess you use felix http bridge. HandleSecurity is called by the bridge always before the service function of your servlet is called. getMimeType() and getResource(..) is called only if the same methods of the ServletContext of the servlet that you registered is called. I do not think it is called by the bridge unless your servlet calls it. There is a ResourceServlet in the bridge (well http.base that is in the bridge jar). Check the source of it. You might want to register that servlet with your HttpContext. – Balazs Zsoldos Jun 27 '14 at 21:46