Developing a Web Service Dispatch Client

Operating at the XML Message Level 16-7 ?xml version=1.0 encoding=UTF-8 standalone=yes? bindings wsdlLocation=SimpleClientService.wsdl xmlns=http:java.sun.comxmlnsjaxws bindings node=wsdl:definitions package name=provider.server providertrueprovider bindings

16.4 Developing a Web Service Dispatch Client

A Web service Dispatch client, implemented using the javax.xml.ws.DispatchT interface, enables clients to work with messages at the XML level. The following procedure describes the typical steps for programming a Web service Dispatch client.

16.4.1 Example of a Web Service Dispatch Client

The following sample shows how to implement a basic Web service Dispatch client. The sample is described in detail in the sections that follow. Example 16–2 Example of a Web Service Dispatch Client package jaxws.dispatch.client; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.StringReader; import java.net.URL; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; Table 16–4 Steps to Develop a Web Service Provider-based Endpoint Step Description 1 Import the JWS annotations that will be used in your Web service Provider-based JWS file. The standard JWS annotations for a Web service Provider-based JWS file include: import javax.xml.ws.Service; import javax.xml.ws.Dispatch; import javax.xml.ws.ServiceMode; Import additional annotations, as required. For a complete list of JWS annotations that are supported, see Web Service Annotation Support in WebLogic Web Services Reference for Oracle WebLogic Server. 2 Create a Dispatch instance. See Section 16.4.2, Creating a Dispatch Instance . 3 Invoke a Web service operation. You can invoke a Web service operation synchronously one-way or two-way or asynchronously polling or asynchronous handler. See Section 16.4.3, Invoking a Web Service Operation . 16-8 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server import javax.xml.ws.Dispatch; import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import javax.xml.ws.soap.SOAPBinding; public class WebTest extends TestCase { private static String in_str = wiseking; private static String request = ns1:sayHello xmlns:ns1=\http:example.org\arg0+in_str+arg0ns1:sayHello; private static final QName portQName = new QNamehttp:example.org, SimplePort; private Service service = null; protected void setUp throws Exception { String url_str = System.getPropertywsdl; URL url = new URLurl_str; QName serviceName = new QNamehttp:example.org, SimpleImplService; service = Service.createserviceName; service.addPortportQName, SOAPBinding.SOAP11HTTP_BINDING, url_str; System.out.printlnSetup complete.; } public void testSayHelloSource throws Exception { setUp; DispatchSource sourceDispatch = service.createDispatchportQName, Source.class, Service.Mode.PAYLOAD; System.out.println\nInvoking xml request: + request; Source result = sourceDispatch.invokenew StreamSourcenew StringReaderrequest; String xmlResult = sourceToXMLStringresult; System.out.printlnReceived xml response: + xmlResult; assertTruexmlResult.indexOfHELLO:+in_str=0; } private String sourceToXMLStringSource result { String xmlResult = null; try { TransformerFactory factory = TransformerFactory.newInstance; Transformer transformer = factory.newTransformer; transformer.setOutputPropertyOutputKeys.OMIT_XML_DECLARATION, yes; transformer.setOutputPropertyOutputKeys.METHOD, xml; OutputStream out = new ByteArrayOutputStream; StreamResult streamResult = new StreamResult; streamResult.setOutputStreamout; transformer.transformresult, streamResult; xmlResult = streamResult.getOutputStream.toString; } catch TransformerException e { e.printStackTrace; } return xmlResult; } } Operating at the XML Message Level 16-9

16.4.2 Creating a Dispatch Instance

The javax.xml.ws.Service interface acts as a factory for the creation of Dispatch instances. So to create a Dispatch instance, you must first create a Service instance. Then, create the Dispatch instance using the Service.createDispatch method. For example: ... String url_str = System.getPropertywsdl; QName serviceName = new QNamehttp:example.org, SimpleImplService; service = Service.createserviceName; service.addPortportQName, SOAPBinding.SOAP11HTTP_BINDING, url_str; DispatchSource sourceDispatch = service.createDispatchportQName, Source.class, Service.Mode.PAYLOAD; ... In the example above, the createDispatch method takes three parameters: ■ Qualified name QName of the target service endpoint. ■ Class of the type parameter T. In this example, the javax.xml.transform.Source format is used. For valid values, see Table 16–2 . ■ Usage mode. In this example, the message payload is specified. For valid usage modes, see Table 16–1 . Alternatively, you can pass the JAXB context to operate on XML messages using custom annotated JAXB classes. For more information about the valid parameters that can be used to call the Service.createDispatch method, see the javax.xml.ws.Service Javadoc at: https:jax-ws.dev.java.netnonav2.1.1docsapijavaxxmlwsSe rvice.html .

16.4.3 Invoking a Web Service Operation

Once the Dispatch instance is created, use it to invoke a Web service operation. You can invoke a Web service operation synchronously one-way or two-way or asynchronously polling or asynchronous handler. For complete details about the synchronous and asynchronous invoke methods, see the javax.xml.ws.Dispatch Javadoc at: https:jax-ws.dev.java.netnonav2.1.1docsapijavaxxmlwsDi spatch.html For example, in the following code excerpt, the XML message is encapsulated as a javax.xml.transform.stream.StreamSource object and passed to the synchronous invoke method. The response XML is returned in the result variable as a Source object, and transformed back to XML. The sourcetoXMLString method used to transform the message back to XML is shown in Example 16–2 . ... private static String request = ns1:sayHello xmlns:ns1=\http:example.org\arg0+in_ str+arg0ns1:sayHello; Source result = sourceDispatch.invokenew StreamSourcenew StringReaderrequest; String xmlResult = sourceToXMLStringresult; ... 16-10 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server 17 Programming RESTful Web Services 17-1 17 Programming RESTful Web Services The following sections describe how to program RESTful Web services: ■ Section 17.1, Overview of RESTful Web Services