Add web services global handlers
Components that need to register web services handlers to all the web services end points must implement the Handler interface and register that implementation in the service registry.
The global handler service is provided either by the jaxws-2.2 , or jaxrs-1.1, so we must specify the following feature or feature combinations in server.xml:
- jaxws-2.2
- jaxrs-1.1
- jaxws-2.2 and jaxrs-1.1
The Handler SPI provides different properties to specify the ENGINE_TYPE, the FLOW_TYPE, and the client side (IS_CLIENT_SIDE) or the server side (IS_SERVER_SIDE) where handlers take effect.
We must implement the Handler interface and register the implementation class into the service registry.
The Java API documentation for each Liberty profile SPI is available in a separate compressed file in one of the Javadoc subdirectories of the ${wlp.install.dir}/dev directory.
Parent topic: Liberty SPI utilities
Deploy the handler bundle
We can deploy the handler bundle using the WebSphere Application Server Developer Tools for Eclipse .
- Click File > New > Other and then expand OSGi.
- Click OSGi Bundle Project and click Next. The New OSGi Bundle Project window opens.
- Enter MyHandler as the Project name. In the Target runtime list, select WebSphere Application Server Liberty Profile. If no runtime exists, click New Runtime to create a WebSphere(r) Application Server Liberty Profile runtime.
- Clear the Add bundle to application ratio.
- Click Next twice and go to the OSGi Bundle page.
- On the OSGi Bundle page, check Generate an activator, a Java class that controls the life cycle of the bundle. Leave the Activator name as myhandler.Activator and click Finish.
- Click Window > Preferences > Plug-in Development > Target Platform and select WebSphere Application Server Liberty Profile with SPI .
Ensure that we have added WebSphere Application Server Liberty Profile runtime in 3.
- Click Apply and click OK.
- Expand MyHandler > BundleContent > META-INF and open the MANIFEST.MF file using the Plug-in Manifest Editor.
- Create the MyHander and MyActivitor classes:
... import com.ibm.wsspi.webservices.handler.Handler; ... public class MyHandler implements Handler { ... public void handleFault(GlobalHandlerMessageContext arg0) { ... } public void handleMessage(GlobalHandlerMessageContext msgctxt) throws Exception { if (msgctxt.getFlowType().equalsIgnoreCase(HandlerConstants.FLOW_TYPE_OUT)) { } ... } .... } public class MyActivator implements BundleActivator { ... public void start(BundleContext context) throws Exception { final Hashtable<String, Object> handlerProps = new Hashtable<String, Object>(); handlerProps.put(HandlerConstants.ENGINE_TYPE, HandlerConstants.ENGINE_TYPE_JAXWS); handlerProps.put(HandlerConstants.FLOW_TYPE, HandlerConstants.FLOW_TYPE_IN); handlerProps.put(HandlerConstants.IS_CLIENT_SIDE, true); handlerProps.put(HandlerConstants.IS_SERVER_SIDE, true); handlerProps.put(org.osgi.framework.Constants.SERVICE_RANKING, 3); MyHandler myHandler = new MyHandler(); context.registerService(Handler.class, myHandler, handlerProps); ... } ... }
- Click File > New > Other and then expand OSGi.
- Click Liberty Feature Project and then click Next. The Liberty Feature Project window opens.
- Specify MyHandlerFeature as the Project name.
- In the Target runtime list, select WebSphere Application Server Liberty Profile and click Next. The OSGi Bundles Selection Page opens.
- On OSGi Bundles Selection Page, select MyHandler 1.0.0 as the Contained Bundles and click Finish.
- Modify Manifest:MyHandler in MyHandler project. Click the MANIFEST.MF tab and add com.ibm.wsspi.webservices.handler to the Import-pacakge element.
- Right-click the MyHandlerFeature project, click Install Feature to install the feature to Liberty runtime.
- Edit the server.xml file to enable the MyHandlerFeature:
<featureManager> ...... <feature>jsp-2.2</feature> <feature>jaxws-2.2</feature> // we can also use one of the following feature or feature combinations: jaxrs-1.1, , jaxws-2.2 and jaxrs-1.1, <feature>usr:MyHandlerFeature</feature> ...... </featureManager>