Search code examples
javajakarta-eejax-wsmtom

How do you set the filename for a JAXWS MTOM Attachment?


I'll update this with a code example in the morning but I have a basic JAX-WS implementation used to download a generated file via MTOM and the the file that get's attached when I test it in SOAPUI has a random filename even though the DataSource I'm passing the DataHandler has the correct filename via the getName() method on the DataSource implementation. Just trying to figure out what I'm missing.

Here is my Endpoint

import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

@Controller
@WebService
@MTOM(enabled = true, threshold = 1024)
public class DataExtractsEndpoint extends SpringBeanAutowiringSupport{

    private final Logger LOGGER = Logger.getLogger(this.getClass());

    @Autowired
    private DataExtractDao dataExtractDao;

    @WebMethod
    public DataHandler getDownload() {
        LOGGER.debug(dataExtractDao.getDataSource().getName());
        LOGGER.debug(dataExtractDao.getDataSource().getContentType());
        DataHandler dh = new DataHandler(dataExtractDao.getDataSource());     
        return dh;
    }
}

And my custom DataSource Class

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
import org.springframework.stereotype.Service;

@Service
public class ZipFileDataSource implements DataSource{

    private byte[] fileData;
    private final String fileContentType = "application/zip";
    private String fileName;
    private ByteArrayOutputStream baos;
    private ByteArrayInputStream bais;

    @Override
    public InputStream getInputStream() throws IOException {
        bais = new ByteArrayInputStream(fileData);
        return bais;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        baos = new ByteArrayOutputStream();
        return baos;
    }

    public void saveOutputStream(){
        fileData = baos.toByteArray();
    }

    @Override
    public String getContentType() {
        return fileContentType;
    }

    @Override
    public String getName() {
        return fileName;
    }

    public void setName(String fileName){
        this.fileName = fileName;
    }

}

Example file name "attachment-2da67c29-c8de-455a-968c-a310eb470f8eexamplejaxwssuncom7224513233547289534.zip"


Solution

  • The file name isn't used in MTOM. If you want to send the file name, add another element to your XML.