<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發布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 閱讀(1502) 評論(0)  編輯  收藏 所屬分類: AXIS
    主站蜘蛛池模板: 夜夜爽妓女8888视频免费观看| 无码午夜成人1000部免费视频| 亚洲午夜福利在线观看| 日韩在线不卡免费视频一区| 亚洲а∨天堂久久精品9966 | 日本视频一区在线观看免费| 亚洲 欧洲 日韩 综合在线| 亚洲日韩人妻第一页| 3344永久在线观看视频免费首页| 亚洲av永久无码精品秋霞电影秋| 精品亚洲一区二区三区在线观看| 亚洲电影免费观看| 日韩在线观看视频免费| 亚洲精品国产手机| 亚洲视频在线一区二区| 黄页网站在线看免费| 巨胸狂喷奶水视频www网站免费| 亚洲一级毛片在线观| 亚洲日韩av无码| 日本最新免费不卡二区在线| 久久成人免费播放网站| 狠狠入ady亚洲精品| 亚洲无成人网77777| 国产中文在线亚洲精品官网| 欧美a级在线现免费观看| 免费看男人j放进女人j免费看| 亚洲精品无码久久| 久久亚洲精品无码VA大香大香| 亚洲精品456播放| 在线观看人成网站深夜免费| 91精品手机国产免费| 国产精品福利在线观看免费不卡| 亚洲人成未满十八禁网站| 亚洲AV乱码久久精品蜜桃| 亚洲高清无码在线观看| 免费观看黄网站在线播放| 99精品一区二区免费视频| 91在线视频免费观看| 一道本不卡免费视频| 亚洲成在人线在线播放无码| 亚洲成人黄色在线|