Java Reference
In-Depth Information
In this example, you define an
HTTP-Resources
manifest header that bundles can
use to specify resources they wish to register with the
HTTP
Service. You check
whether a bundle specifies any resources in the
HTTP-Resources
header
B
. The for-
mat of this header is a comma-delimited list of directories that may optionally be
aliased (you'll see this working in a second). If any resources are found, you create a
ProxyHttpContext
(shown in the following listing) and register the resources with
the
HttpService
.
Listing 15.3
ProxyHttpContext
for reading resources from a bundle
public class ProxyHttpContext implements HttpContext {
private final Bundle bundle;
public ProxyHttpContext(Bundle bundle) {
this.bundle = bundle;
}
B
Passes getResource()
to tracked bundle
public URL getResource(String name) {
return bundle.getResource(name);
}
...
}
If you used the default
HttpContext
, the
HTTP
Service would try to find the requested
resources in your
ResourceTracker
bundle, which clearly isn't correct.
ProxyHttp-
Context
attempts to find the resources in the bundle you're tracking. You create a
unique
ProxyHttpContext
for each tracked bundle. The key line of code in this class
passes the
getResource()
call through to the tracked bundle
B
.
To demonstrate how to use the resource tracker, the
org.foo.http.resource
bun-
dle in the chapter15/httpservice/ directory of the companion code contains the fol-
lowing header:
HTTP-Resources: /resource=html,/resource/images=images
If you deploy this bundle into an
OSG
i framework along with an
HTTP
Service and
your
ResourceTracker
, then its resources are registered; you can browse them at
http://localhost:8080/resource/index.html. This is just one trivial usage of the
Http-
Context
object. Other possible scenarios might include the following:
Managing authenticated access to web content
■
Mapping local file system resources into the
HTTP
Service
■
Now that you're familiar with registering static resources with the
HTTP
Service, let's
look at how it allows you to use servlets in an
OSG
i environment.
REGISTERING SERVLETS
Java servlets are the building block on which a vast number of web applications have
been built. Some of the key advantages of the servlet specification are the relative
simplicity of the
API
and the huge number of tools and frameworks available to help
you develop web applications with it. Similar to static content, the
HTTP
Service