...
Code Block |
---|
public class HelloWorld extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("Hello World"); } } |
To register a Servlet and map it to a URI, you need to retrieve the HttpService
and call its registerServlet
method:
Code Block |
---|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference sRef = context.getServiceReference(HttpService.class.getName()); if (sRef != null) { { HttpService service = (HttpService) context.getService(sRef); service.registerServlet("/hello", new HelloWorld(), null, null); } } } |
In the same way, you can unregister a Servlet (for instance, in the stop
method of the Bundle Activator) calling the HttpService.unregister
method.
...
Code Block |
---|
public interface HttpContext { String getMimeType(java.lang.String name); //Returns the mime type of the specified resource URL getResource(java.lang.String name); //Returns the URL to retrieve the specified resource boolean handleSecurity(HttpServletRequest request, HttpServletResponse response); //Manages security for the specified request } |
...
Code Block |
---|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference sRef = context.getServiceReference(HttpService.class.getName()); if (sRef != null) { { HttpService service = (HttpService) context.getService(sRef); service.registerResources("/static", "/etc/www", null); } } } |
As a result of the service.registerResources("/static", "/etc/www", null)
code, all the files available under /etc/www
will be exposed under /static
(f.i. http://localhost:8080/static/001.jpg will render the /etc/www/001.jpg). However, the example above can be simplistic in practice; the HttpContext object is the solution to customize the resource handling.
For instance, you can set the define more complex URI to file mappings overriding the HttpContext.getResource
method, or the correct mime type implementing the method HttpContext.getMimeType
like in the example:
Code Block |
---|
//.... public String getMimeType(String file) { if (file.endsWith(".jpg") { return "image/jpeg"; } else if (file.endsWith(".png")) { return "image/png"; } else { return "text/html"; } } //.... |
If you implement a customized HttpContext object, don't forget to specify it as third parameter of the registerResources
method invocation:
Code Block |
---|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference sRef = context.getServiceReference(HttpService.class.getName()); if (sRef != null) { { HttpService service = (HttpService) context.getService(sRef); HttpContext myHttpContext = new MyHttpContext()); service.registerResources("/static", "/etc/www", myHttpContext); } } } |
Using the ExtHttpService
...
Code Block |
---|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference sRef = context.getServiceReference(ExtHttpService.class.getName()); if (sRef != null) { { ExtHttpService service = (ExtHttpService) context.getService(sRef); service.registerFilter(new HelloWorldFilter(), "/hello/.*", null, 0, null); } } } |
Notice the pattern for filters is using regular expressions. So .*
is the same as a simple
using standard servlet patterns.
*
...