Tuesday, August 19, 2014

JAX-WS Provider

Web Service endpoints may choose to work at the XML message level by implementing the Provider interface. This is achieved by implementing either Provider<Source> or Provider<SOAPMessage> or Provider<DataSource>. The endpoint accesses the message or message payload using this low-level, generic API. This has to be a WSDL first approach. Let's assume that we have WSDL for getting the Price of the Product. the WSDL looks as follows:

<definitions name="RequestPriceProvider"
    targetNamespace="http://www.lalit.com/om"
    xmlns:omTypes="http://www.lalit.com/omTypes"
    xmlns:tns="http://www.lalit.com/om"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> 

 <types>
    <xsd:schema targetNamespace="http://www.lalit.com/omTypes"
            xmlns:s1="http://www.lalit.com/omTypes">
    <xsd:element name="product" type="s1:product" />
    <xsd:complexType name="product">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string" />
            <xsd:element name="value" type="xsd:float" />
        </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>
</types>

    <message name="RequestPrice">
        <part name="name" element="omTypes:product" />
    </message>

    <portType name="RequestOrderPort">
        <operation name="getPrice">
            <input message="tns:RequestPrice" />
            <output message="tns:RequestPrice" />
        </operation>
    </portType>    

<binding name="RequestPriceBinding" type="tns:RequestOrderPort">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="document" />
    <operation name="getPrice">
        <soap:operation soapAction="" />
        <input>
        <soap:body use="literal" namespace="http://www.lalit.com/jws/wsdl" />
        </input>
        <output>
        <soap:body use="literal"
            namespace="http://www.lalit.com/jws/wsdl" />
        </output>
    </operation>
    </binding>

    <service name="RequestPrice_Service">
        <port name="RequestOrderPort"
            binding="tns:RequestPriceBinding">
            <soap:address location="REPLACE_WITH_ACTUAL_URL" />
        </port>
    </service>
</definitions>

Write the Provider class

@WebServiceProvider(serviceName = "RequestPriceProvider",
    portName="RequestOrderPort",
    targetNamespace = "http://www.lalit.com/om",
     wsdlLocation="WEB-INF/wsdl/RequestPriceProvider.wsdl")
@ServiceMode(value=Service.Mode.PAYLOAD)
public class RequestPriceProvider implements Provider<Source>{

    public Source invoke(Source request) {
        …
        return resp;
    }

}

Here you have to deal at XML level and build the response yourself.

More on Web services

No comments:

Post a Comment