开发者

osgi HttpService register resource from outside of the bundle ( filesystem )

I am using OSGI's HttpService to register my servlets and resources. To register resource I am using HttpService.registerResources(java.lang.String alias, java.lang.String name, HttpContext context)

I have tested that "name" can take any relative path inside the bundle. example /resource where resource is a folder inside the bundle jar. But I want to give "name" a value outside of the bundle example /home/user/webapps/resource. i.e. name links to the filesystem's path开发者_开发知识库. I want to know if that's possible and how ?

I saw a similar post but it didn't conclude. how to get the image which is outside the OSGi bundle?


An easy way to fetch objects from outside the bundle is to implement a servlet as Robert described it.

A better way, were you have much better control on what objects to return, correct MIME type handling etc. is to implement an HttpContext object and to register it with the HTTP Service. The HTTP Service then always calls method HttpContext.getResource(String name) whenever a resource is requested. And here you can serve your objects from the file system. From the specification:

public URL getResource( String name )

Called by the Http Service to map a resource name to a URL. For servlet registrations, Http Service will call this method to support the ServletContext methods getResource and getResourceAsStream. For resource registrations, Http Service will call this method to locate the named resource. The context can control from where resources come. For example, the resource can be mapped to a file in the bundle’s persistent storage area via bundleContext.getDataFile(name).toURL() or to a resource in the context’s bundle via getClass().getResource(name).

Please have a look at section 102.3, Registering Resources and following of the OSGi Service Compendium. There, the whole concept is explained in detail and with examples.


I am not familiar with the deep internals of HttpService.registerResources(..) but my work-around solution would be to implement a Servlet that delivers the resources. As it is your own implementation you can access the filesystem as well. The only thing you have to keep in mind when implementing that Servlet is to set the correct Content-Type and may be other required response header fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜