Cant download file from webservice using MTOM and Axis2 stub

1.6k views Asked by At

I m trying to download a file from my Axis2 webservice server using MTOM and ADB. I can download the file if I dont enable the MTOM both on server and the client sides. Any suggestions or code sample would be nice :)

Client side

ServerWSStub stub = new ServerWSStub();
stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE);

Server side axis2.xml

<parameter name="enableMTOM">optional</parameter>

This is my Server

public DataHandler download(String konum) throws Exception {
        System.out.println("The filePath for download: " + konum);
        FileDataSource dataSource = new FileDataSource(konum);
        DataHandler datahandler = new DataHandler(dataSource);

        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace ns = fac.createOMNamespace("http://benim.projem.org/dosya", "dosyam");
        OMText textData = fac.createOMText(datahandler, true);
        OMElement ele = fac.createOMElement("sonuc", ns);
        ele.addChild(textData);
        System.out.println(ele);
        return datahandler;

This is my Client

ServerWSStub stub = new ServerWSStub();          

//stub._getServiceClient().getOptions().setProperty(Constants.Configuration.ENABLE_MTOM,Constants.VALUE_TRUE); 

//when uncommented i get java.lang.NoClassDefFoundError: org/apache/james/mime4j/MimeException 
//while trying to invoke _operationClient.execute(true); in ServerWSStub 
//I guess it is because of wrong unparsing


Download download = new Download();
download.setKonum(konum);
try {
    DownloadResponse downloadResponse = stub.download(download);
    DataHandler dh =(DataHandler) downloadResponse.get_return();
    File file = new File("C:/dosya/"+fileNameType);

    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();
    if(!file.exists()){
        file.createNewFile();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    dh.writeTo(fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
} catch (ServerWSExceptionException e) {
    e.printStackTrace();
}

Any

2

There are 2 answers

0
RespiroDiDio On BEST ANSWER

I finally got the solution I guess. The Stream closes before the client gets the whole file thats why first I used the getOptions().setTimeOutInMilliSeconds(10000) method but it was also useless and then in the Stub file I commented

_messageContext.getTransportOut().getSender().cleanup(_messageContext);//look for the method's finally part

part so that during a large file transportation the stream had not been closed and i could download the whole file without any silly exceptions :)

--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:downloadResponse xmlns:ns="http://servis.ws.projem.tez.benim"><ns:return><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:[email protected]" /></ns:return></ns:downloadResponse></soapenv:Body></soapenv:Envelope>
--MIMEBoundary_e56e8a77b94fbdd7678582aa5ca53f50b1d56c0d828499ea
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
binary code here
0
Piotr Kepka On

Simply add apache-mime4j-core-0.7.2.jar to WEB-INF/lib on the service (server) side. The jar can be found here.