Using OSGi HTTP Whiteboard in Liberty

You can use the httpWhiteboard-1.0 feature to enable the development and deployment of modular Web applications written using servlet technologies.

Before you begin

Stabilized feature: Support for OSGi applications is stabilized. Java™ Platform, Enterprise Edition (Java EE) 8 technologies are not enabled for OSGi applications and features that support OSGi development. As an alternative, develop applications by using the Java Platform, Enterprise Edition (Java EE) or Microprofile features. For more information, see Stabilized Liberty features and feature capabilities.
To use the httpWhiteboard-1.0 feature, you must have Liberty and the httpWhiteboard-1.0 feature installed.
  • Install the latest version of Liberty with the OSGi application programming model capabilities. You can install Liberty either by using one of the zip install packages (ZIP file) or by extracting the Java archive (JAR) package. For more information about installing Liberty, see Installing Liberty.
  • Install the httpWhiteboard-1.0 feature. If the feature is not available as part of the downloaded ZIP archive files, then install the feature from the Liberty repository by using the following command:
    bin\installUtility install httpWhiteboard-1.0
You will see the following messages as the installation progresses:

Step 1 of 3: Downloading httpWhiteboard-1.0...
Step 2 of 3: Installing httpWhiteboard-1.0...
Step 3 of 3: Cleaning up temporary files...

After the feature is installed successfully, you can use the feature.

Procedure

  • Configure the httpWhiteboard-1.0 feature in the server.xml file.
    
    <featureManager>
      <feature>httpWhiteboard-1.0</feature>
    </featureManager>
  • Optionally, specify the context path where you want your application to be located. For example:<httpWhiteboard context-path="/osgi/http" />

What to do next

  • Write a service.

    The Http Whiteboard implementation looks for a particular type of OSGi services that are registered, for example javax.servlet.Servlet. So to use the Http Whiteboard feature, you can use any supported OSGi component model, such as Blueprint, to register the service and the service is automatically picked up.

    The following Blueprint example shows a simple servlet implementation, com.my.MyWhiteboardServlet being registered in the OSGi service registry under the javax.servlet.Servlet interface. It is also registered under a standard Http Whiteboard service property, org.http.whiteboard.servlet.pattern, that provides a relative URL location for the servlet.
    
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
    	<bean id="MyWhiteboardServletBean"
    		class="com.my.MyWhiteboardServlet" />
    	<service id="MyWhiteboardServletBeanService"
    		ref="MyWhiteboardServletBean"
    		interface="javax.servlet.Servlet" />
    		<service-properties>
    			<entry key="osgi.http.whiteboard.servlet.pattern"
    				value="/mywhiteboardservlet" />
    		</service-properties>
    	</service>
    </blueprint>
  • Access the servlet.
    When you start the server or add the feature to a running server, you might see a few messages as given in the following example:
    
    [AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
    [AUDIT   ] CWWKZ0058I: Monitoring dropins for applications. 
    [AUDIT   ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http
    [AUDIT   ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http/shared
    [AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/osgi/http/
    [AUDIT   ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http/MyHttpWhiteboardApp
    [AUDIT   ] CWWKZ0001I: Application MyHttpWhiteboardApp started in 0.424 seconds.
    In this example, there are three HTTP Whiteboard context root added messages. The last message shows where the context root for the application, MyHttpWhiteboardApp, is added. The application also contains the servlet defined in the previous blueprint example. You can access a servlet by combining the Web application URL that ends in /osgi/http/ and the relative URL for the Whiteboard context root (merging the intersecting /osgi/http/), and then appending the location of the servlet that is specified in osgi.http.whiteboard.servlet.pattern. For example, merging the Web application URL (http://localhost:9080/osgi/http/) and whiteboard context root (/osgi/http/MyHttpWhiteboardApp) for the application gives:
    http://localhost:9080/osgi/http/MyHttpWhiteboardApp
    Adding the value of osgi.http.whiteboard.servlet.pattern will result in the following URL:
    http://localhost:9080/osgi/http/MyHttpWhiteboardApp/mywhiteboardservlet
  • Combine with other capabilities.

    The Http Whiteboard specification is based around the OSGi services. This means the feature integrates with other native OSGi services and component models. For example, Blueprint can be used to dynamically inject other services. Blueprint name space handlers and configuration admin can be used to dynamically inject configuration values into the servlet allowing the application configuration to be changed if required.