uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

33.5.2 Creating an implementation class for the Web Service client

Create an implementation class for the Web Service client that uses the Web Services.

The following is an example for creating a Web Service client that invokes the Web Service thrice:

package com.sample.client;
 
import javax.xml.namespace.QName;
import javax.xml.ws.soap.AddressingFeature;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
 
import com.sample.AddNumbers;
import com.sample.AddNumbersImplService;
import com.sample.AddNumbersFault_Exception;
 
public class TestClient {
 
    int number1 = 10;
    int number2 = 10;
    int negativeNumber = -10;
 
    public static void main(String[] args) {
        TestClient client = new TestClient();
 
        client.existActionAnnotation1();
        client.existActionAnnotation2();
        client.notExistActionAnnotation();
        client.existFaultActionAnnotation();
        client.notExistFaultActionAnnotation();
    }
 
    public void existActionAnnotation1() {
        System.out.println("existActionAnnotation1");
        try {
            AddressingFeature feature = new AddressingFeature();
 
            AddNumbersImplService service = new AddNumbersImplService();
            AddNumbers stub = service.getAddNumbersImplPort(feature);
            int result = stub.add(number1, number2);
            assert result == 20;
        } catch (Exception ex) {
            ex.printStackTrace();
            assert false;
        }
    }
 
    public void existActionAnnotation2() {
        System.out.println("existActionAnnotation2");
        try {
            AddressingFeature feature = new AddressingFeature();
            W3CEndpointReferenceBuilder eprBuilder = new W3CEndpointReferenceBuilder();
            eprBuilder.address("http://webhost:8085/addressing/AddNumbersImplService");
 
            eprBuilder.serviceName(new QName("http://sample.com/", "AddNumbersImplService"));
            eprBuilder.endpointName(new QName("http://sample.com/", "AddNumbersImplPort"));
            W3CEndpointReference epr = eprBuilder.build();
 
            AddNumbersImplService service = new AddNumbersImplService();
            AddNumbers stub = service.getPort(epr, AddNumbers.class, feature); 
            int result = stub.add(number1, number2);
            assert result == 20;
        } catch (Exception ex) {
            ex.printStackTrace();
            assert false;
        }
    }
 
    public void notExistActionAnnotation() {
        System.out.println("notExistActionAnnotation");
        try {
            AddressingFeature feature = new AddressingFeature();
 
            AddNumbersImplService service = new AddNumbersImplService();
            AddNumbers stub = service.getAddNumbersImplPort(feature); 
            int result = stub.add2(number1, number2);
            assert result == 20;
        } catch (Exception ex) {
            ex.printStackTrace();
            assert false;
        }
    }
 
    public void existFaultActionAnnotation() {
        System.out.println("existFaultActionAnnotation");
        try {
            AddressingFeature feature = new AddressingFeature();
 
            AddNumbersImplService service = new AddNumbersImplService();
            AddNumbers stub = service.getAddNumbersImplPort(feature);
            stub.add3(negativeNumber, number2);
            assert false;
        } catch (AddNumbersFault_Exception e) {
            System.out.println("This is expected exception");
        } catch (Exception e) {
            e.printStackTrace();
            assert false;
        }
    }
 
    public void notExistFaultActionAnnotation() {
        System.out.println("notExistFaultActionAnnotation");
        try {
            AddressingFeature feature = new AddressingFeature();
 
            AddNumbersImplService service = new AddNumbersImplService();
            AddNumbers stub = service.getAddNumbersImplPort(feature);
            stub.add(negativeNumber, number2);
            assert false;
        } catch (AddNumbersFault_Exception ex) {
            System.out.println("This is expected exception");
        } catch (Exception e) {
            e.printStackTrace();
            assert false;
        }
    }
}
 

The created TestClient.java is saved in the c:\temp\jaxws\works\addressing\client\src\com\sample\client\ directory with the UTF-8 format.

Note that com.sample, AddNumbers, AddNumbersImplService, AddNumbersImplPort, add, add2, and add3 differ as per the package names, class names, and the method names in the classes of the generated Java source. For developing Web Services with different configurations, you must review the coding of the package names, class names, and method names in the classes.