<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    ??? 本篇是繼關于Axis2發(fā)布Web Service之后的又一隨筆,此篇的主要任務是講述使用Axis2開發(fā)客戶端和服務器端的具體實現。相信大家在看本篇的時候已經具備了一定的Web Service開發(fā)基礎,因此我主要是以例子的方式來說明,文字會相對較少。同時,我會在最后提供幾個優(yōu)秀的網址給大家。

    ??? 例一(簡單的字符串傳送服務實現)

    客戶端
    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的開發(fā)網中國站)
    ????????????????????????????????http://www.apache.org/ (Apache公司的官方網站)

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


    posted on 2006-12-27 13:42 SIMONE 閱讀(1503) 評論(0)  編輯  收藏 所屬分類: AXIS
    主站蜘蛛池模板: 99在线热视频只有精品免费| 鲁啊鲁在线视频免费播放| 免费无码又爽又刺激网站| 亚洲国产成人精品无码久久久久久综合| 亚洲国产成人久久三区| 国产成人免费高清激情明星 | 免费播放在线日本感人片| 亚洲美女在线国产| 国产精品小视频免费无限app| 亚洲毛片不卡av在线播放一区| 九一在线完整视频免费观看| 亚洲乱码国产一区网址| 黄色视频在线免费观看| 亚洲线精品一区二区三区| a在线观看免费视频| 亚洲美女一区二区三区| 国产成人免费高清激情视频| 亚洲AV日韩综合一区尤物| 好爽…又高潮了毛片免费看 | 亚洲视频在线免费| 亚洲精品无码久久千人斩| 日本在线免费播放| 亚洲国色天香视频| 国产精品极品美女免费观看 | xxxxx免费视频| 亚洲中文字幕久久精品蜜桃 | 两个人看的www免费视频中文| 亚洲AV成人片色在线观看| 亚洲黄色免费网站| 精品无码专区亚洲| 久久精品国产亚洲网站| 国产在线a免费观看| 全部在线播放免费毛片| 亚洲丝袜美腿视频| 免费看大黄高清网站视频在线| 一级特黄特色的免费大片视频| 久久久久亚洲精品天堂| 国产片免费在线观看| 免费网站看av片| 亚洲精品乱码久久久久蜜桃| 亚洲人成网77777色在线播放|