Previous Topic

Next Topic

Book Contents

Book Index

Registering Resources

The resources (files and directories) that correspond to aliases must not end with a "/". You register HTTP resources by using the following syntax:

where <resource_path> is the path of the resource in the bundle JAR file. The "/" before the resource path is optional, and it means that the resource path begins from the main directory in the bundle JAR file.

To register resources on a different that the default server, use the getServiceReferences(String clazz, String filter) method of the BundleContext interface passing the name and the port of the desired HTTP Service through the filter argument.

Listing 1.

package demo.bundle;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;

                        . . .

  BundleContext bc = null;
  ServiceReference httpRef = null;
  HttpService httpServ = null;

                        . . .

    // Obtaining the HTTP Service from the server framework
    httpRef = bc.getServiceReference("org.osgi.service.http.HttpService");
    httpServ = (HttpService)bc.getService(httpRef);

    // Registering resources using different resource paths
    httpServ.registerResources("/aliasOne", "/resources/resource.html",httpContext);
    httpServ.registerResources("/aliasTwo","resources/resource.html",httpContext);

                        . . .

Considering the first registration of resources in the code example above, the resource file is found in:

<Bundle JAR File>/

 

 

demo/bundle

 

resources/resource.html

If there is no slash, the resource is situated in the folder of the current class file. The second registration implies that the resource is in:

When the HTTP server constructs its response to a resource request, it should fill in the Content-Type header. The content type of a resource can be set in the implementation of HttpContext.getMimeType method.

You can add a resource extension in the mbs.http.pid OSGi-compliant configuration dictionary. For more details refer to HTTP Common Configuration.