Serving static web resources in an OSGi environment

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:

In case we would like to offer static resources from a bundle we add the following capability to it one or more times:
Provide-Capability: static.resources
Possible attributes of the capability:
  • 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)
A full example:
Provide-Capability: static.resources;path=/pathinbundle/;filePattern=*;recurse=true;scope=wiring;someAttributeKey=someValue

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)
The StaticResourceProvider OSGi services can be used by the component called StaticResourceServlet. The configuration options of the component:
  • 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.


Tags: , ,

About Balázs Zsoldos

Balazs Zsoldos is the co-founder of Everit. He is the leader of the development of Everit OpenSource Components. Developing Java based solutions is not only his job but also his passion. He believes in simplicity. That is why he decided to design and build as many simple, but useful goal-oriented modules as he can. As the base of the stack, he chose OSGi. Balazs does not believe in monoholitic frameworks, therefore all of the solutions that was designed by him can be used separately. In the beginning of his career, Balazs was a big fan of JEE and Spring. After a while, he changed his mind and started to try replacing everything with non-magical solutions that do not contain interceptors, weaving, etc.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: