Thursday, July 18, 2013

WS-Discovery with CXF-based JAX-WS/JAX-RS in WSO2 AS

Apache CXF has added out-of-the-box support for WS-Discovery for CXF based JAX-WS and JAX-RS services starting from CXF 2.7.x versions. This post describes how to add this support to WSO2 Application Server with WSO2 Governance Registry (GReg) acting as the DiscoveryProxy receiver.

All the WSO2 Carbon-based service hosting products like WSO2 Application Server supported WS-Discovery for axis2 services (including data services, proxy services etc.) for a long time. These products can be configured to announce the axis2 services to a Governance Registry instance. But we did not have a way to announce CXF-based JAX-WS and JAX-RS services. But with the out-of-the-box support for WS-Discovery that comes with CXF this is now possible. Following steps describes how to add WS-Discovery to jax webapps.

  1. Download the latest WSO2 Application Server (AS) pack.
  2. Copy the following three jars to {WSO2-AS-HOME}/lib/runtimes/cxf
    • cxf-services-ws-discovery-service  - version 2.7.5
    • cxf-services-ws-discovery-api  - version 2.7.5
    • mina-core - version 2.0.7
  3. Add a CXF Configuration File (cxf.xml) to the WEB-INF/classes/ location of a webapp.
  4. Add a bus property "org.apache.cxf.service.ws-discovery.address" which points to the DiscoveryProxy instance residing in GReg. To do that,
    1. Open up cxf.xml we added to the webapp before. There, under the 'cxf' bean (which uses the class org.apache.cxf.bus.spring.SpringBus), add the property "org.apache.cxf.service.ws-discovery.address" and set it to the DiscoveryProxy service URL of GReg. The default service URL is https://localhost:9443/services/DiscoveryProxy for a WSO2 GReg instance running in local machine with port offset 0. The properties are stored as a map. A sample cxf.xml would look like the following. [1]


<property name="properties">
    <map>
        <entry key="org.apache.cxf.service.ws-discovery.address" value="https://localhost:9443/services/DiscoveryProxy" />
    </map>
</property>


     5. Deploy the webapp


[1] Sample cxf.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown">
        <property name="properties">
            <map>
                <entry key="org.apache.cxf.service.ws-discovery.address" value="http://localhost:8080/services/DiscoveryProxy" />
            </map>
        </property>
    </bean>

    <bean id="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor" 
        class="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"/>
    <bean id="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor" 
        class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
    <bean id="org.apache.cxf.bus.spring.BusExtensionPostProcessor" 
        class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/>

</beans>
 




Sample Hello Message sent from AS to GReg

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:wsa="http://www.w3.org/2005/08/addressing"
               xmlns:tns="http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01">
    <soap:Header>
        <wsa:Action>http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/Hello</wsa:Action>
        <wsa:MessageID>urn:uuid:5063d2d5-4c8c-4f86-b1a0-9c3a17421af2</wsa:MessageID>
        <wsa:To>http://localhost:8080/services/DiscoveryProxy</wsa:To>
        <wsa:ReplyTo>
            <wsa:Address>http://www.w3.org/2005/08/addressing/none</wsa:Address>
        </wsa:ReplyTo>
    </soap:Header>
    <soap:Body>
        <ns2:Hello xmlns="http://www.w3.org/2005/08/addressing"
                   xmlns:ns2="http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01">
            <EndpointReference>
                <Address>urn:uuid:e67f250f-9402-4c53-bb1f-da1ffe985ebc</Address>
            </EndpointReference>
            <ns2:Types xmlns:ns3="http://server.hw.demo/">ns3:HelloWorld</ns2:Types>
            <ns2:Scopes/>
            <ns2:XAddrs>/hello_world</ns2:XAddrs>
            <ns2:MetadataVersion>1</ns2:MetadataVersion>
        </ns2:Hello>
    </soap:Body>
</soap:Envelope>