After having the persistence layer, I was thinking how to serve static resources in an OSGi based web environment. I definitely do not want to create WABs with web.xml files. This is I have come up so far:
- path: Path in the bundle where from we would like to export resources
- filePattern: Pattern of the files that can be served. E.g.: *.gif
- recurse: Export files from sub-directories or not. Default is true.
- scope: Only export files from the bundle or from the imported packages as well. Possible values: local / wiring. Default is local
- mimeConfig: Location of an optional properties file inside the bundle where file extension – metatype values are paired. E.g.: gif=image/gif
- someAttributeKey: Optional attributes that will be listed in the OSGi service properties (see below)
An extender picks up every bundle that contains the “static.resources” capability and registers a StaticResourceProvider OSGi service for each capability. The service properties come from the capability attributes. The functions of the StaticResourceProvider interface:
- listFiles(String folder): Listing files and sub-directories inside the specified folder
- getResourceAsStream(String path)
- getMimeType(String path)
- staticResourceProviders.target: OSGi filter that addresses one or more static resource provider service
- acceptRanges: Whether range queries are accepted or not (HTTP 1.1)
- dirAllowed: If directory listing is allowed or not
- styleSheet: Location of a CSS static resource that could decorate directory listing
- welcomeFileNames: Comma separeated list of welcome file names. If directory url is provided, it will be redirected to the welcome file if that exists.
- gzFileRegex: If a file name with path matches with this optional regular expression, it will be sent unpacked, via a compressed stream if available (HTTP 1.1)
- etags: Whether etags are allowed or not (HTTP 1.1)
- maxCacheSize: The maximum total size of the cache or 0 for no cache.
- maxCachedFileSize: The maximum size of a file to cache
- maxCachedFiles: The maximum number of files to cache
- cacheControl: If set, all static content will have this value set as the cache-controlheader
- otherServiceProps: String array of key-value pairs that should be put into the properties of the registered OSGi service. E.g.: Putting whiteboard pattern specific properties.
The configuration properties of StaticResourceServlet component can be familiar from DefaultServlet of Jetty.
I implemented a similar solution and it is now available at github.
The specification was changed a bit. It is more simple. As soon as I used it in a project, I will upload it to maven central.