关于Web Serviece以及Axis2技术(客户端和服务器端实现)

来源:百度文库 编辑:神马文学网 时间:2024/04/26 22:45:10
本篇是继关于Axis2发布Web Service之后的又一随笔,此篇的主要任务是讲述使用Axis2开发客户端和服务器端的具体实现。相信大家在看本篇的时候已经具备了一定的Web Service开发基础,因此我主要是以例子的方式来说明,文字会相对较少。同时,我会在最后提供几个优秀的网址给大家。
例一(简单的字符串传送服务实现)
客户端:
com.udm.iudmservice.IUDMServiceStub stub = new com.udm.iudmservice.IUDMServiceStub();
com.udm.iudmservice.types.GetUserAllRegServices param234 =  (com.udm.iudmservice.types.GetUserAllRegServices) getTestObject (com.udm.iudmservice.types.GetUserAllRegServices.class);
GetUserAllRegServicesResponse resp = stub.getUserAllRegServices(param234);
OMElement ret = resp.get_return()[0];
System.out.println(ret.getText());                               //此为测试语句
服务器端:
GetUserAllRegServicesResponse res = new GetUserAllRegServicesResponse();
OMElement resp = fac.createOMElement("Return", omNs);
resp.setText("返回的结果!");
System.out.println("返回结果为:" + resp.getText());  //此为测试语句
res.set_return(new OMElement[] { resp });
例二(文件传送的具体实现)
客户端:
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.InputStream;
public class FileTransferClient {
private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/interop");
public static boolean upload(String mailboxnum, short greetingType, File file, String fileType) {
try {
OMElement data = buildUploadEnvelope(mailboxnum, greetingType, file, fileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println(data);
OMElement ome = sender.sendReceive(data);
System.out.println(ome);
String b = ome.getText();
return Boolean.parseBoolean(b);
}
catch(Exception e) {
}
return false;
}
public static InputStream download(String mailboxnum, short greetingType, String FileType) {
try {
OMElement data = buildDownloadEnvelope(mailboxnum, greetingType, FileType);
Options options = buildOptions();
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println(data);
OMElement ome = sender.sendReceive(data);
System.out.println(ome);
OMText binaryNode = (OMText) ome.getFirstOMChild();
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
return actualDH.getInputStream();
}
catch(Exception e) {
}
return null;
}
private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
DataHandler expectedDH;
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
OMElement data = fac.createOMElement("upload", omNs);
OMElement fileContent = fac.createOMElement("fileContent", omNs);
FileDataSource dataSource = new FileDataSource(file);
expectedDH = new DataHandler(dataSource);
OMText textData = fac.createOMText(expectedDH, true);
fileContent.addChild(textData);
OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
mboxnum.setText(mailboxnum);
OMElement gtType = fac.createOMElement("greetingType", omNs);
gtType.setText(greetingType+"");
OMElement fileType=fac.createOMElement("fileType", omNs);
fileType.setText(FileType);
data.addChild(mboxnum);
data.addChild(gtType);
data.addChild(fileType);
data.addChild(fileContent);
return data;
}
private static OMElement buildDownloadEnvelope(String mailboxnum, short greetingType, String FileType) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
OMElement data = fac.createOMElement("download", omNs);
OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
mboxnum.setText(mailboxnum);
OMElement gtType = fac.createOMElement("greetingType", omNs);
gtType.setText(greetingType+"");
OMElement fileType=fac.createOMElement("fileType", omNs);
fileType.setText(FileType);
data.addChild(mboxnum);
data.addChild(gtType);
data.addChild(fileType);
return data;
}
private static Options buildOptions() {
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTo(targetEPR);
// enabling MTOM in the client side
options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}
public static void main(String agrs[]) {    //此为测试主函数
String file = "C:/deploy.wsdd";
short gt = 1;
String mn = "20060405";
String ft="wsdd";
boolean rtv = upload(mn,gt,new File(file),ft);
System.out.println(rtv);
InputStream is = download(mn,gt,ft);
}
}
服务器端:
import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import java.io.FileOutputStream;
import java.io.*;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
public class interopService {
public static final String TMP_PATH = "c:/tmp";
public OMElement upload(OMElement element) throws Exception {
OMElement _fileContent = null;
OMElement _mailboxnum = null;
OMElement _greetingType = null;
OMElement _fileType = null;
System.out.println(element);
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
_fileContent = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
_mailboxnum = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
_greetingType = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}
if (_fileContent == null || _mailboxnum == null || _greetingType== null || _fileType==null) {
throw new AxisFault("Either Image or FileName is null");
}
OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
String mboxNum = _mailboxnum.getText();
String greetingType = _greetingType.getText();
String fileType = _fileType.getText();
String greetingstoreDir = TMP_PATH+"/"+mboxNum;
File dir = new File(greetingstoreDir);
if(!dir.exists()) {
dir.mkdir();
}
String filePath = greetingstoreDir+"/"+greetingType+"."+fileType;
File greetingFile = new File(filePath);
if(greetingFile.exists()) {
greetingFile.delete();
greetingFile = new File(filePath);
}
// Extracting the data and saving
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream imageOutStream = new FileOutputStream(greetingFile);
InputStream is = actualDH.getInputStream();
imageOutStream.write(IOUtils.getStreamAsByteArray(is));
// setting response
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data", "x");
OMElement ele = fac.createOMElement("response", ns);
ele.setText("true");
return ele;
}
public OMElement download(OMElement element) throws Exception {
System.out.println(element);
OMElement _mailboxnum = null;
OMElement _greetingType = null;
OMElement _fileType = null;
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
_mailboxnum = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
_greetingType = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}
String mboxNum = _mailboxnum.getText();
String greetingType = _greetingType.getText();
String fileType = _fileType.getText();
String filePath = TMP_PATH+"/"+mboxNum+"/"+greetingType+"."+fileType;
FileDataSource dataSource = new FileDataSource(filePath);
DataHandler expectedDH = new DataHandler(dataSource);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data", "x");
OMText textData = fac.createOMText(expectedDH, true);
OMElement ele = fac.createOMElement("response", ns);
ele.addChild(textData);
return ele;
}
}
Services.xml配置文件:


This is a sample Web Service with two operations,echo and ping.

sample.mtom.interop.service.interopService

urn:upload



urn:download



两个学习网址https://www6.software.ibm.com/(IBM的官方网站)
http://www.apache.org/ (Apache公司的官方网站)