使用Axis2传输附件(AXIS2 MTOM)--2

5 .测试<o:p></o:p>
FileTransferClient.java<o:p></o:p>
package sample;
<o:p> </o:p>
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
<o:p> </o:p>
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
<o:p> </o:p>
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;<o:p></o:p>
import org.apache.axiom.om.OMText;<o:p></o:p>
import org.apache.axiom.soap.SOAP11Constants;<o:p></o:p>
import org.apache.axis2.AxisFault;<o:p></o:p>
import org.apache.axis2.Constants;<o:p></o:p>
import org.apache.axis2.addressing.EndpointReference;<o:p></o:p>
import org.apache.axis2.client.Options;<o:p></o:p>
import org.apache.axis2.client.ServiceClient;<o:p></o:p>
 <o:p></o:p>
public class FileTransferClient {
   private static EndpointReference targetEPR =
 new EndpointReference("http://127.0.0.1:8080/axis2/services/FileOperation");
  
   public static boolean upload(String fileName, File file, String fileType) {
     try {
      OMElement data = buildUploadEnvelope(fileName, file, fileType);
      Options options = buildOptions();
      ServiceClient sender = new ServiceClient();
      sender.setOptions(options);
      System.out.println("The data in method upload: "+data);
      OMElement ome = sender.sendReceive(data);
      System.out.println("Convert the data to element in method upload: "+ome);
      String b = ome.getText();
      return Boolean.parseBoolean(b);
     }
     catch(Exception e) {
       e.printStackTrace();
     }
     return false;
   }
  
   public static boolean download(String userName, String fileName, String fileType) {
     try {
       OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       System.out.println("The data in method download: "+data);
       OMElement ome = sender.sendReceive(data);
       System.out.println("Convert the data to element in method download: "+ome);
       OMText binaryNode = (OMText) ome.getFirstOMChild();
       binaryNode.setOptimize(true); // 必须加此句,否则会出现 ContentID is null 的异常 !
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
       FileOutputStream imageOutStream = new FileOutputStream("D:/userTemp/xx.gif");
       InputStream is = actualDH.getInputStream();
       imageOutStream.write(IOUtils.getStreamAsByteArray(is));
       return true;
      }
      catch(Exception e) {
        e.printStackTrace();
      }
     return false;
   }
  
   private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) {
     DataHandler expectedDH;
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");<o:p></o:p>
     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 _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType = fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_fileName);
     data.addChild(_fileType);
     data.addChild(fileContent);
     return data;
   }
  
   private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType) {
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
     OMElement data = fac.createOMElement("download", omNs);
     OMElement _userName = fac.createOMElement("userName", omNs);
     _userName.setText(userName);
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType=fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_userName);
     data.addChild(_fileName);
     data.addChild(_fileType);
     return data;
   }
   private static Options buildOptions() throws AxisFault {
     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 = "D:/userTemp/ya.gif";
     String fn = "testUser";
     String ft="gif";
     boolean rtv = upload(fn,new File(file),ft);
     System.out.println("is upload success: "+rtv);
     String un="zj";
     String downfn="1";
     if(download(un,downfn,ft)){
            System.out.println("download success.");
     }
     else System.out.println("download fail.");
     System.out.println("Client main end.");
   }
}
<o:p> </o:p>
6. 结果<o:p></o:p>
察看 soap 消息,我们可以发现
<fd:upload xmlns:fd="http://example.org/filedata">
<fd:fileName>testUser</fd:fileName>
<fd:fileType>gif</fd:fileType>
<fd:fileContent>RHQMLJJ4/AMZkEBAEAOw (省略部分 2 进制代码) </fd:fileContent>
</fd:upload>
<o:p> </o:p>
Convert the data to element in method upload:
<fd:response xmlns:fd=http://example.org/filedata xmlns:tns="http://ws.apache.org/axis2">true</fd:response>
<o:p> </o:p>
The data in method download:
<fd:download xmlns:fd="http://example.org/filedata">
<fd:userName>zj</fd:userName><o:p></o:p>
<fd:fileName>1</fd:fileName>
<fd:fileType>gif</fd:fileType>
</fd:download>
<o:p> </o:p>
Convert the data to element in method download:
<fd:response xmlns:fd="http://example.org/filedata" xmlns:tns="http://ws.apache.org/axis2">
eIqGRwjkQAAAOw== (省略部分 2 进制代码)
</fd:response>
<o:p> </o:p>
7 .代码分析<o:p></o:p>
   利用 Axis2 Mtom 发送附件应用了 builder 模式。要向一个 webserive 发送请求,首先就要构建一个请求的 Envelope,Axis2 构建 Envelope 的时候是利用的 Axis2 AXIOM api( 就是 axis2 java object xml 的映射处理机制 ), 其编程模式和 DOM 差不多的 . 看这一段 :
private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
DataHandler expectedDH;
     OMFactory fac = OMAbstractFactory.getOMFactory();
...
return data;
}
这一段其实是构建的 data 对像是这样一段 xml java object 代表:
<fd:upload xmlns:fd="http://example.org/filedata">
<fd:fileName>testUser</fd:fileName>
<fd:fileType>gif</fd:fileType>
<fd:fileContent>RHQMLJJ4/AMZkEBAEAOw (省略部分 2 进制代码) </fd:fileContent>
</fd:upload>
其中的 Dwvc2VydmljZT4NCjwvZGVwbG95bWVudD4NCg0K 是要传送的文件的内容代表,至于什么编码,我没有深究。注意这一句 :
OMElement data = fac.createOMElement("upload", omNs);
这里的“ upload ”参数对应的是 webservice 的一个操作的名称,这个操作的名称是跟 webservice server 端实现类的方法名和 services.xml 的所定义的
<operationname="upload">
   <actionMapping>urn:upload</actionMapping>
   <messageReceiverclass="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
要一致的。
再看看这一段,
 private static Options buildOptions() {
     Options options = new Options();
     ...
     return options;
   }
这里构建的 Options 对象,顾名思义就是调用 webservice 的相应的选项:比如这里就指定了 Soap 协议为 1.1 , 指定了所请求的 service EPR( 就是地址 ) ,声明在 client 应用 MTOM 指定传输协议为 HTTP
构建好要传送的 data options , 所执行的代码为:
ServiceClient sender = new ServiceClient();
// 设定选项
sender.setOptions(options);
// 打印要传送的数据,为一段 xml
System.out.println(data);
// 传送数据,得到返回值
OMElement ome = sender.sendReceive(data);
// 打印返回值,为一段 xml
System.out.println(ome);
// 析取返回值中的数据
String b = ome.getText();
// 返回
return Boolean.parseBoolean(b);
可以发现, server 端和 client 的中间传递数据都是通过       org.apache.axiom.om.OMElement 对象的,这个对象是一段 xml java 对象映射。

你可能感兴趣的:(apache,编程,xml,webservice,SOAP)