uCosminexus Application Server, Web Service Development Guide

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

33.3.1 Creating the Web Service Implementation Class

Create a Web Service Implementation Class to code the processing of Web Services. This subsection describes how to calculate the contents of the received request message and create the Web Service Implementation Class that returns response messages.

The following is an example for creating a Web Service Implementation Class:

package com.sample;
 
 
import javax.activation.DataHandler;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import javax.xml.bind.annotation.XmlMimeType;
import com.sun.xml.ws.developer.StreamingAttachment;
import com.sun.xml.ws.developer.StreamingDataHandler;
 
@MTOM
@StreamingAttachment(dir="C:/TMP", parseEagerly=true, memoryThreshold=50000L)
@WebService(serviceName="UserInfoService",targetNamespace="http://sample.com")
public class UserInfoImpl {
 
    public UserData getUserData(String in0, @XmlMimeType("application/octet-stream")DataHandler in1)
        throws UserInfoException {
 
        if (in1 != null) {
            if (in1 instanceof StreamingDataHandler) {
                StreamingDataHandler sdh = null;
                try {
                    //Registering a photograph to the employee information
                    sdh = (StreamingDataHandler)in1;
                    ......
                } catch(Exception e) {
                    throw new UserInfoException("Exception occurred.", e.getMessage());
                } finally {
                    try {
                        if (sdh != null) {
                            //Closing the data
                            sdh.close();
                        }
                    } catch(Exception ex) {
                        ......
                    }
                }
            }
        }
 
        UserData userdata = new UserData();
        //Setting the name and affiliation of the registered employee
        if (in0.equals("1")) {
            userdata.setName("Hitachi Taro ");
            userdata.setSection("The personnel section");
        } if (....) {
            ....
        } ...
 
        //Setting the registration confirmation message
        if (in1 == null) {
            userdata.setMessage("Failure(no image).");
        } else {
            userdata.setMessage("Success.");
        }
        return userdata;
    }
}

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

Next, create the main Web Service that implements SEI. This subsection describes how to calculate the contents of the received request message and create the Web Service Implementation Class com.sample.AddNumbersImpl that is returned as a response message.

The following is an example for creating the main Web Service:

package com.sample;
 
import javax.jws.WebService;
import javax.xml.ws.soap.Addressing;
 
@Addressing
@WebService(endpointInterface = "com.sample.AddNumbers")
public class AddNumbersImpl implements AddNumbers {
 
    public int add(int number1, int number2) throws AddNumbersFault {
        return impl(number1, number2);
    }
 
    public int add2(int number1, int number2) throws AddNumbersFault {
        return impl(number1, number2);
    }
 
    public int add3(int number1, int number2) throws AddNumbersFault {
        return impl(number1, number2);
    }
 
    int impl(int number1, int number2) throws AddNumbersFault {
        if (number1 < 0 || number2 < 0) {
            throw new AddNumbersFault("Negative numbers can't be added!",
                                      "Numbers: " + number1 + ", " + number2);
        }
        return number1 + number2;
    }
}

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

Also create an exception class com.sample.AddNumbersFault thrown in the com.sample.AddNumbersImpl class.

The following is an example for creating an exception class:

package com.sample;
 
public class AddNumbersFault extends Exception {
 
    String detail;
 
    public AddNumbersFault(String message, String detail) {
        super(message);
        this.detail = detail;
    }
 
    public String getDetail() {
        return detail;
    }
}

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