Hi,

I wrote one test web service (see[1] [2]) to return binary attachment.
However I found the binary data is returned by value, not by reference(see
[3]). I have enabled both MTOM and SwA in the server side by chaning the
values of enableMTOM and enableSwA to true the axis2.xml. Do I miss
something? Many thanks inadvance.

best regards
donald

[1] TestService.java
import java.io.*;
import javax.activation.DataHandler;
import org.apache.axiom.attachments.Attachments;
import org.apache.axis2.context.MessageContext;

public class TestService
{
 public TransferResp transfer(String name, String attchmentID) throws
IOException
 {
       //retrieve the attachment
       MessageContext msgCtx = MessageContext.getCurrentMessageContext();
       Attachments attachment = msgCtx.getAttachmentMap();
       DataHandler dataHandler = attachment.getDataHandler(attchmentID);
       File file = new File(name);
       FileOutputStream fileOutputStream = new FileOutputStream(file);
       dataHandler.writeTo(fileOutputStream);
       fileOutputStream.flush();
       fileOutputStream.close();

      TransferResp resp = new TransferResp();
      resp.setUploadSuccess(true);
      resp.setDownloadFile("d:\\test.txt");
      return resp;
}
}

[2] TransferResp.java
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;

public class TransferResp
{
 private boolean uploadSuccess;
 private OMElement downloadFile;

 public TransferResp()
 {
 }
 public void setUploadSuccess(boolean b)
 {
   uploadSuccess = b;
 }

 public void setDownloadFile(String fileName)
 {

   OMFactory fac = OMAbstractFactory.getOMFactory();

   OMNamespace omNs = fac.createOMNamespace(
                    "http://ws.apache.org/axis2/xsd";, "test1");
   //create attachment element
   downloadFile = fac.createOMElement("downloadFile", null);

   //attach the blob with the element
   File file = new File(fileName);
   FileDataSource fileDataSource = new FileDataSource(file);
   DataHandler dataHandler = new DataHandler(fileDataSource);
   OMText textData = fac.createOMText(dataHandler, true);
   textData.setOptimize(true);
   textData.setBinary(true);
   downloadFile.addChild(textData);

 }
 public boolean getUploadSuccess()
 {
   return uploadSuccess;
 }
 public OMElement getDownloadFile()
 {
   return downloadFile;
 }
}

[3]
<ns:transferResponse xmlns:ns="http://ws.apache.org/axis2/xsd";>
   <ns:return>
        <downloadFile>dGVzdCBmaWxl</downloadFile>
        <uploadSuccess xmlns="http://ws.apache.org/axis2/xsd
">true</uploadSuccess>
   </ns:return></ns:transferResponse>
upload success is : true

Reply via email to