Java Reference
In-Depth Information
Configuring the HTTP Service
The HTTP Service is registered by an implementation bundle. The client has no con-
trol over the port or URL on which the service is running. That's the job of the admin-
istrator of the OSGi framework. The HTTP Service specification defines framework
properties to configure the service ports:
■
org.osgi.service.http.port
—Specifies the port used for servlets and
resources accessible via HTTP. The default value is 80.
■
org.osgi.service.http.port.secure
—Specifies the port used for servlets
and resources accessible via HTTPS. The default value is 443.
You can set framework properties using the launching API covered in chapter 13. In
this case, the launcher passes system properties through to the framework proper-
ties. It's also generally possible to configure the HTTP Service implementation using
the Configuration Admin Service, but that is implementation-dependent.
SETTING THE HTTPCONTEXT
You may have noticed the
HttpContext
parameter in the
HttpService.register-
Resources()
method. In the previous example, you passed in
null
, but what does this
parameter do?
HttpContext
provides a way to inject the
HTTP
Service with resource-
lookup and -access policies. Let's first look at the
API
, followed by an example, to show
what this allows you to do. The
HttpContext
interface is defined as follows:
public interface HttpContext {
boolean handleSecurity(HttpServletRequest req, HttpServletResponse res)
throws IOException;
URL getResource(String name);
String getMimeType(String path);
}
The
handleSecurity()
method provides a callback to allow the
HTTP
Service to verify
whether a request should be allowed for a given resource. The
getResource()
method provides a mechanism to define how a particular resource is mapped to a
URL
, which makes it possible to host contents from any scheme accessible from
URL
s.
Finally, the
getMimeType()
provides a mechanism to control the
MIME
type headers
returned with the stream of a particular resource.
If you use
null
for the
HttpContext
, as in the previous example, the
HTTP
Service
uses a default context implementation, which can also be accessed using the
Http-
Service.createDefaultHttpContext()
method. Table 15.1 describes the behavior of
the default
HttpContext
as defined by the
OSG
i specification.
To demonstrate how to use the
HttpContext
, let's create a
ResourceTracker
to
track bundles and automatically register their resources with the
HTTP
Service. To
accomplish this, you'll use the
org.osgi.util.tracker.BundleTracker
introduced
in the
OSG
i R4.2 Compendium specification. Listing 15.2 shows the body of the
add-
Bundle()
method of the
BundleTracker
subclass.