posts - 495,comments - 227,trackbacks - 0
          Web Services以及Axis2技術之二

          ??? 本篇是繼關于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配置文件:
          <servicename="MTOMService">
          ??? <description>
          ??????? This is a sample Web Service with two operations,echo and ping.
          ??? </description>
          ??? <parametername="ServiceClass"locked="false">sample.mtom.interop.service.interopService</parameter>
          ??? <operationname="upload">
          ??????? <actionMapping>urn:upload</actionMapping>
          ??????? <messageReceiverclass="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
          ??? </operation>
          ????? <operationname="download">
          ??????? <actionMapping>urn:download</actionMapping>
          ??????? <messageReceiverclass="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
          ??? </operation>
          </service>

          兩個學習網址http://www-128.ibm.com/developerworks/cn/(IBM的開發網中國站)
          ????????????????????????????????http://www.apache.org/ (Apache公司的官方網站)

          ??? 最后:祝福大家在程序的天空里越飛越高?。?!


          posted on 2006-12-27 13:42 SIMONE 閱讀(1506) 評論(0)  編輯  收藏 所屬分類: AXIS
          主站蜘蛛池模板: 甘肃省| 辉南县| 海淀区| 平和县| 出国| 利津县| 司法| 永仁县| 广平县| 内乡县| 台东市| 阿坝| 五家渠市| 沐川县| 赤壁市| 绍兴县| 太湖县| 肃北| 汉中市| 秭归县| 黄冈市| 普陀区| 南郑县| 兴业县| 大英县| 土默特右旗| 三原县| 绿春县| 武夷山市| 连云港市| 喜德县| 巨野县| 综艺| 大石桥市| 贵德县| 安西县| 镇江市| 茂名市| 呈贡县| 正安县| 礼泉县|