随笔-159  评论-114  文章-7  trackbacks-0

1.   WebService 开发

1.1. 开发 JavaBean 封装的 WebService

1.1.1.    Java

/*

 * 创建日期 2006-7-13

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

package com.ibm.localjava;

 

import com.ibm.localjava.data.FolderObject;

 

/**

 * @author Flyiky

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

public class DimensionCao {

      

       public FolderObject getSingleFolder(int level)

       {

              FolderObject obj = new FolderObject();

              obj.setLevel(level);

              return obj;

       }

      

       public FolderObject[] getMultiFolder(int multilevel)

       {

              FolderObject obj1 = new FolderObject();

              obj1.setLevel(multilevel);

              FolderObject obj2 = new FolderObject();

              obj2.setLevel(multilevel-1);

              FolderObject[] objArry = new FolderObject[2];

              objArry[0] = obj1;

              objArry[1] = obj2;

              return objArry;

       }

 

}

 

自定义类

 

package com.ibm.localjava.data;

 

public class FolderObject implements java.io.Serializable {

 

    private String objectName = null;   //the name of the folder

    private String objectId = null;    //r_object_id of the folder

    private String parentId = null;    //the r_object_id of its parent folder

    private String fullPath = null;    //fullpath of the folder,e.g. /test/test1103

    private int level = 0; 

   

    /**

     * @return Returns the fullPath.

     */

    public String getFullPath() {

        return fullPath;

    }

    /**

     * @param fullPath The fullPath to set.

     */

    public void setFullPath(String fullPath) {

        this.fullPath = fullPath;

    }

    /**

     * @return Returns the level.

     */

    public int getLevel() {

        return level;

    }

    /**

     * @param level The level to set.

     */

    public void setLevel(int level) {

        this.level = level;

    }

    /**

     * @return Returns the objectId.

     */

    public String getObjectId() {

        return objectId;

    }

    /**

     * @param objectId The objectId to set.

     */

    public void setObjectId(String objectId) {

        this.objectId = objectId;

    }

    /**

     * @return Returns the objectName.

     */

    public String getObjectName() {

        return objectName;

    }

    /**

     * @param objectName The objectName to set.

     */

    public void setObjectName(String objectName) {

        this.objectName = objectName;

    }

    /**

     * @return Returns the parentId.

     */

    public String getParentId() {

        return parentId;

    }

    /**

     * @param parentId The parentId to set.

     */

    public void setParentId(String parentId) {

        this.parentId = parentId;

    }

 

}

1.1.2.    使用 RSA 生成接入点代码和 WSDL

image001.png

 

 

image004.png

 

image006.png

 

image008.png

1.1.3.    WSDL 分析

预先需要了解 WSDL 通用结构,看看电子书。

image010.png

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions targetNamespace="http://localjava.ibm.com" xmlns:impl="http://localjava.ibm.com" xmlns:intf="http://localjava.ibm.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns2="http://data.localjava.ibm.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <wsdl:types>

  <schema targetNamespace="http://localjava.ibm.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:impl="http://localjava.ibm.com" xmlns:intf="http://localjava.ibm.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns2="http://data.localjava.ibm.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <import namespace="http://data.localjava.ibm.com"/>

   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

   <element name="level" type="xsd:int"/>

   <element name="multilevel" type="xsd:int"/>

   <complexType name="ArrayOf_tns2_FolderObject">

    <complexContent>

     <restriction base="soapenc:Array">

      <attribute ref="soapenc:arrayType" wsdl:arrayType="tns2:FolderObject[]"/>

     </restriction>

    </complexContent>

   </complexType>

   <element name="ArrayOf_tns2_FolderObject" nillable="true" type="impl:ArrayOf_tns2_FolderObject"/>

  </schema>

  <schema targetNamespace="http://data.localjava.ibm.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:impl="http://localjava.ibm.com" xmlns:intf="http://localjava.ibm.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

   <complexType name="FolderObject">

    <sequence>

     <element name="fullPath" nillable="true" type="xsd:string"/>

     <element name="level" type="xsd:int"/>

      <element name="objectId" nillable="true" type="xsd:string"/>

     <element name="objectName" nillable="true" type="xsd:string"/>

     <element name="parentId" nillable="true" type="xsd:string"/>

    </sequence>

   </complexType>

   <element name="FolderObject" nillable="true" type="tns2:FolderObject"/>

  </schema>

 </wsdl:types>

 

   <wsdl:message name="getMultiFolderRequest">

 

      <wsdl:part name="multilevel" type="xsd:int"/>

 

   </wsdl:message>

 

   <wsdl:message name="getMultiFolderResponse">

 

      <wsdl:part name="getMultiFolderReturn" type="impl:ArrayOf_tns2_FolderObject"/>

 

   </wsdl:message>

 

   <wsdl:message name="getSingleFolderResponse">

 

      <wsdl:part name="getSingleFolderReturn" type="tns2:FolderObject"/>

 

   </wsdl:message>

 

   <wsdl:message name="getSingleFolderRequest">

 

      <wsdl:part name="level" type="xsd:int"/>

 

   </wsdl:message>

 

   <wsdl:portType name="DimensionCao">

 

      <wsdl:operation name="getSingleFolder" parameterOrder="level">

 

         <wsdl:input message="impl:getSingleFolderRequest" name="getSingleFolderRequest"/>

 

         <wsdl:output message="impl:getSingleFolderResponse" name="getSingleFolderResponse"/>

 

      </wsdl:operation>

 

      <wsdl:operation name="getMultiFolder" parameterOrder="multilevel">

 

         <wsdl:input message="impl:getMultiFolderRequest" name="getMultiFolderRequest"/>

 

         <wsdl:output message="impl:getMultiFolderResponse" name="getMultiFolderResponse"/>

 

      </wsdl:operation>

 

   </wsdl:portType>

 

   <wsdl:binding name="DimensionCaoSoapBinding" type="impl:DimensionCao">

 

      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

 

      <wsdl:operation name="getSingleFolder">

 

         <wsdlsoap:operation soapAction=""/>

 

         <wsdl:input name="getSingleFolderRequest">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localjava.ibm.com" use="encoded"/>

 

         </wsdl:input>

 

         <wsdl:output name="getSingleFolderResponse">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localjava.ibm.com" use="encoded"/>

 

         </wsdl:output>

 

      </wsdl:operation>

 

      <wsdl:operation name="getMultiFolder">

 

         <wsdlsoap:operation soapAction=""/>

 

         <wsdl:input name="getMultiFolderRequest">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localjava.ibm.com" use="encoded"/>

 

         </wsdl:input>

 

         <wsdl:output name="getMultiFolderResponse">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localjava.ibm.com" use="encoded"/>

 

         </wsdl:output>

 

      </wsdl:operation>

 

   </wsdl:binding>

 

   <wsdl:service name="DimensionCaoService">

 

      <wsdl:port binding="impl:DimensionCaoSoapBinding" name="DimensionCao">

 

         <wsdlsoap:address location="http://localhost:9080/AddressWeb/services/DimensionCao"/>

 

      </wsdl:port>

 

   </wsdl:service>

 

</wsdl:definitions>

targetNameSpace 是个命名空间。

 

wsdl:address wsdl 的位置

 

getSingleFolder 是一个方法的名称

 

   该方法的参数

   <wsdl:message name="getSingleFolderRequest">

      <wsdl:part name="level" type="xsd:int"/>

   </wsdl:message>

  

该方法的返回值

   <wsdl:message name="getSingleFolderResponse">

      <wsdl:part name="getSingleFolderReturn" type="tns2:FolderObject"/>

   </wsdl:message>

  

1.2. WebService 客户端的开发

wsif-all-2.0

 

http://mirror.vmmatrix.net/apache/ws/wsif/2_0/

 

编译环境中所需要的 jar 文件基本都包含了

 

还差一个 activation.jar mail.jar ,自己找一下,哪都有。

 

建立一个 Java Application 程序。

 

添加 jar 文件。

image012.png

1.2.1.    拷贝所有自定义类型的类文件到工程中

package com.ibm.localjava.data;

 

public class FolderObject implements java.io.Serializable {

 

    private String objectName = null;   //the name of the folder

    private String objectId = null;    //r_object_id of the folder

    private String parentId = null;    //the r_object_id of its parent folder

    private String fullPath = null;    //fullpath of the folder,e.g. /test/test1103

    private int level = 0; 

   

    /**

     * @return Returns the fullPath.

     */

    public String getFullPath() {

        return fullPath;

    }

    /**

     * @param fullPath The fullPath to set.

     */

    public void setFullPath(String fullPath) {

        this.fullPath = fullPath;

    }

    /**

     * @return Returns the level.

     */

    public int getLevel() {

        return level;

    }

    /**

     * @param level The level to set.

     */

    public void setLevel(int level) {

        this.level = level;

    }

    /**

     * @return Returns the objectId.

     */

    public String getObjectId() {

        return objectId;

    }

    /**

     * @param objectId The objectId to set.

     */

    public void setObjectId(String objectId) {

        this.objectId = objectId;

    }

    /**

     * @return Returns the objectName.

     */

    public String getObjectName() {

        return objectName;

    }

    /**

     * @param objectName The objectName to set.

     */

    public void setObjectName(String objectName) {

        this.objectName = objectName;

    }

    /**

     * @return Returns the parentId.

     */

    public String getParentId() {

        return parentId;

    }

    /**

     * @param parentId The parentId to set.

     */

    public void setParentId(String parentId) {

        this.parentId = parentId;

    }

 

}

 

/*

 * 创建日期 2006-7-13

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

package com.address.client;

 

import javax.xml.namespace.QName;

 

import org.apache.wsif.WSIFMessage;

import org.apache.wsif.WSIFOperation;

import org.apache.wsif.WSIFPort;

import org.apache.wsif.WSIFService;

import org.apache.wsif.WSIFServiceFactory;

 

import com.ibm.localjava.data.FolderObject;

 

/**

 * @author Flyiky

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

public class TestClient {

      

       public static void main(String[] args) throws Exception

       {

//            WSIFServiceFactory factory = WSIFServiceFactory.newInstance();

//            WSIFService service = factory.getService("http://localhost:9080/AddressWeb/services/DimensionCao?wsdl",null,null,"http://localjava.ibm.com","DimensionCao");

//            service.mapType(

//                 new QName("http://data.localjava.ibm.com", "FolderObject"),

//                 Class.forName("com.ibm.localjava.data.FolderObject"));

//            WSIFPort port = service.getPort();

//           

//            getAFolderObj(port);

             

              WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); [f1]  

              WSIFService service = factory.getService("http://localhost:9080/AddressWeb/services/DimensionCao?wsdl",null,null,"http://localjava.ibm.com","DimensionCao"); [f2]  

             

              service.mapType(

                   new QName("http://data.localjava.ibm.com", "FolderObject"),

                   Class.forName("com.ibm.localjava.data.FolderObject")); [f3]  

             

              WSIFPort port = service.getPort(); [f4]  

             

              getMFolder(port);

             

       }

      

       private static void getMFolder(WSIFPort port) throws Exception

       {

              WSIFOperation operation = port.createOperation("getMultiFolder"); [f5]  

              WSIFMessage input = operation.createInputMessage();

        WSIFMessage output = operation.createOutputMessage();

        WSIFMessage fault = operation.createFaultMessage();

[f6]           input.setIntPart("multilevel", 5);

        if (operation.executeRequestResponseOperation(input, output, fault) [f7]   ) {

            // invocation succeeded

            // extract the address from the output message

            FolderObject[] folderObj = (FolderObject[]) output.getObjectPart("getMultiFolderReturn"); [f8]  

            System.out.println("Service returned FolderS:");

            System.out.println(folderObj[0].getLevel());

        } else {

            // invocation failed, check fault message

        }

       }

      

       private static void getAFolderObj(WSIFPort port) throws Exception

       {

              WSIFOperation operation = port.createOperation("getSingleFolder");

              WSIFMessage input = operation.createInputMessage();

        WSIFMessage output = operation.createOutputMessage();

        WSIFMessage fault = operation.createFaultMessage();

        input.setIntPart("level", 4);

        if (operation.executeRequestResponseOperation(input, output, fault)) {

            // invocation succeeded

            // extract the address from the output message

            FolderObject folderObj = (FolderObject) output.getObjectPart("getSingleFolderReturn");

            System.out.println("Service returned Folder:");

            System.out.println(folderObj.getLevel());

        } else {

            // invocation failed, check fault message

        }

       }

 

}

 

1.3. 部署 WebService

注意:任何一次 WebService 的生成和部署,请保证服务器在开启状态,避免不必要的麻烦。

image014.png

 

image016.png

 

测试一下

 

http://localhost:9080/AddressWeb/services/DimensionCao?wsdl

 

注意:如果修改了服务类的接口,需要重新生成 webservice ,如果修改了服务类的内部行为,直接发布一下即可。

 

 

 

2.   DataHandler 问题

由于使用 DataHandler 类处理大附件,对于客户端来说,如果使用自动生成工具生成代码,肯定会与平台,至少与应用服务器提供的 API 依赖。所以必须使用 WebService Invoke Framework Wsif 接口。

2.1. 服务代码的开发

任何 WebService 都是以一个动态 web 应用为基础。

 

/*

 * 创建日期 2006-7-13

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

package com.test;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.util.Date;

 

import javax.activation.DataHandler;

 

/**

 * @author Flyiky

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

public class TestFileTransform {

      

       public String storeDocumentService(DataHandler dh) {

              try {

                     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\abc"));

                     BufferedInputStream in = new BufferedInputStream(dh     .getInputStream());

 

                     byte[] buffer = new byte[256];

                     while (true) {

                            int bytesRead = in.read(buffer);

                            if (bytesRead == -1)

                                   break;

                            out.write(buffer, 0, bytesRead);

                     }

                     in.close();

                     out.close();

              } catch (Exception e) {

                     System.out.println(e);

                     return e.toString();

              }

              return ("File processes succesfully " + new Date());

       }

 

}

 

2.2. WSDL 文件

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions targetNamespace="http://test.com" xmlns:impl="http://test.com" xmlns:intf="http://test.com" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <wsdl:types>

  <schema targetNamespace="http://test.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:impl="http://test.com" xmlns:intf="http://test.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <import namespace="http://ws-i.org/profiles/basic/1.1/xsd"/>

   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>

   <element name="dh" nillable="true" type="wsi:swaRef"/>

   <element name="storeDocumentServiceReturn" nillable="true" type="xsd:string"/>

  </schema>

 </wsdl:types>

 

   <wsdl:message name="storeDocumentServiceResponse">

 

      <wsdl:part name="storeDocumentServiceReturn" type="xsd:string"/>

 

   </wsdl:message>

 

   <wsdl:message name="storeDocumentServiceRequest">

 

      <wsdl:part name="dh" type="wsi:swaRef"/>

 

   </wsdl:message>

 

   <wsdl:portType name="TestFileTransform">

 

      <wsdl:operation name="storeDocumentService" parameterOrder="dh">

 

         <wsdl:input message="impl:storeDocumentServiceRequest" name="storeDocumentServiceRequest"/>

 

         <wsdl:output message="impl:storeDocumentServiceResponse" name="storeDocumentServiceResponse"/>

 

      </wsdl:operation>

 

   </wsdl:portType>

 

   <wsdl:binding name="TestFileTransformSoapBinding" type="impl:TestFileTransform">

 

      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

 

      <wsdl:operation name="storeDocumentService">

 

         <wsdlsoap:operation soapAction=""/>

 

         <wsdl:input name="storeDocumentServiceRequest">

 

            <mime:multipartRelated>

 

               <mime:part>

 

                  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://test.com" use="encoded"/>

 

               </mime:part>

 

               <mime:part>

 

                  <mime:content part="dh" type="application/octetstream"/>

 

               </mime:part>

 

            </mime:multipartRelated>

 

         </wsdl:input>

 

         <wsdl:output name="storeDocumentServiceResponse">

 

            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://test.com" use="encoded"/>

 

         </wsdl:output>

 

      </wsdl:operation>

 

   </wsdl:binding>

 

   <wsdl:service name="TestFileTransformService">

 

      <wsdl:port binding="impl:TestFileTransformSoapBinding" name="TestFileTransform">

 

         <wsdlsoap:address location="http://localhost:9080/FileTransform/services/TestFileTransform"/>

 

      </wsdl:port>

 

   </wsdl:service>

 

</wsdl:definitions>

2.3. 客户端编写

/*

 * 创建日期 2006-7-13

 *

 * TODO 要更改此生成的文件的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

package com.testabc;

 

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

 

import org.apache.wsif.WSIFMessage;

import org.apache.wsif.WSIFOperation;

import org.apache.wsif.WSIFService;

import org.apache.wsif.WSIFServiceFactory;

 

/**

 * @author Flyiky

 *

 * TODO 要更改此生成的类型注释的模板,请转至

 * 窗口 首选项 Java 代码样式 代码模板

 */

public class TestClient {

      

       public static void main(String[] args) throws Exception

       {

              DataHandler dh = new DataHandler(new FileDataSource("C:\\DocumentumServiceImpl.wsdl"));

              WSIFServiceFactory factory = WSIFServiceFactory.newInstance();

              WSIFService service = factory.getService("D:\\TestFileTransform.wsdl",null,null,"http://test.com","TestFileTransform"); [f9]  

              WSIFOperation op = service.getPort().createOperation("storeDocumentService");

              WSIFMessage input = op.createInputMessage();

              WSIFMessage output = op.createOutputMessage();

              WSIFMessage fault = op.createFaultMessage();

              input.setObjectPart("dh",dh);

              if(op.executeRequestResponseOperation(input,output,fault)){

                    

                     // 调用成功,从返回信息中获取我们需要的信息

                    

                     String returninfo = (String)output.getObjectPart("storeDocumentServiceReturn");

                     System.out.println(returninfo);

                    

              } else {

                     System.out.println("Invocation failed");              

              }

             

       }

 

}

3.   附加信息

 

如果有问题,请发邮件给 lhulcn618@gmail.com

 



posted on 2006-10-27 11:56 北国狼人的BloG 阅读(2605) 评论(0)  编辑  收藏 所属分类: 原创

只有注册用户登录后才能发表评论。


网站导航: