Wednesday 15 May 2013

Calling SOAP web services in OSGi using Apache cxf

Calling SOAP web services in OSGi using Apache cxf

Calling a SOAP service in OSGi is not as straight forward as it seems. Although there are a lot of frameworks that can accomplish this, not all of them are OSGi bundle(s), and we want to keep the footprint as low as possible. This example has a footprint of 8.5Mb so its not that bad and in this example we will be using Java 7, BND tools 2.0, apache felix framework (4.0.3) and Apache cxf (2.7.4)

Oldy enough cxf has a required package javax.mail, javax.mail.internet and javax.activation, these packages are provided by the jvm but we need to explicitly configure them as run system packages. (this configuration can be found in the run.bndrun of the complete example link is below)

Download the cxf zip from the website: http://cxf.apache.org/download.html
And we must use the following bundles from the lib folder:
org.apache.cxf.bundle -> cxf-2.7.4.jar
org.apache.neethi -> neethi-3.0.2.jar
stax2-api -> stax2-api-3.1.1.jar
woodstox-core-asl -> woodstox-core-asl-4.2.0.jar
org.apache.ws.xmlschema.core -> xmlschema-core-2.0.3.jar

The wsdl4j bundle and another dependency xmlresolver in the lib folder in cxf are not OSGi bundles so we need to download them form somewhere else, I have chosen to download them form the org.apache.servicemix maven repository:
org.apache.servicemix.bundles.wsdl4j -> http://repo1.maven.org/maven2/.../org.apache.servicemix.bundles.wsdl4j-1.6.2_6.jar
org.apache.servicemix.bundles.xmlresolver -> http://repo1.maven.org/maven2/.../org.apache.servicemix.bundles.xmlresolver-1.2_5.jar

The application can start now but when we try to make a SOAP call cxf will be missing the async.httpclient. We can download that one from here:
org.apache.httpcomponents.httpclient -> http://repo1.maven.org/maven2/.../httpasyncclient-4.0-beta3.jar

You might want to rename the org.apache.httpcomponents.httpclient bundle to org.apache.httpcomponents.async.httpclient as what I have done in the example as there is a other bundle named 'org.apache.httpcomponents.httpclient' that does not export the same interfaces or packages and is there for not compatible. It is however possible to start felix with both bundles as they differ in versioning but it is not very clear. A prepared version can also be directly downloaded from the example git repository.

The example git repository contains a full example project with a hello world service client and can be found here: https://github.com/Japio007/OSGi_cxf

You can run it with the run.bndrun file (when you have a mock soap service for example with soapUI)

1 comment:

  1. Hello, thanks for your tutorial.
    I have an error when trying to run it:
    Creating Service {http://apache.org/hello_world_soap_http}GreeterService from class org.apache.hello_world_soap_http.Greeter
    org.apache.cxf.service.factory.ServiceConstructionException: Could not resolve a binding for null.
    Caused by: org.apache.cxf.BusException: No binding factory for namespace http://schemas.xmlsoap.org/soap/ registered.

    The only difference I have is placing bndrun in a separate bundle.

    ReplyDelete