CXF MTOM Client

CXF MTOM Client explains step by step details of creating a MTOM Client in order to upload large attachments in SOAP based services
MTOM is used for encoding binary data in base64Binary and send as binary attachment than keeping it with actual SOAP message. MTOM is approved by WC3 and is a standard. MTOM very useful for transfering binary data such as MS documents, PDF, images etc. MTOM uses XML-binary Optimized Packaging (XOP) packages for transmitting binary data
For publishing a CXF Web MTOM Service, you need to follow CXF File Upload With SOAP MTOM.
This MTOM client is generated after the deployment of above service.
Required Libraries
You need to download following libraries in order to run CXF MTOM Client
- OpenJDK 8 / Corretto
- Eclipse 4.15
- For running this client you need to refer the classes generated on CXF File Upload With SOAP MTOM
CXF MTOM Client
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.student.FileUploader;
import com.student.UploadService;
public final class Client {
public static void main(String args[]) throws Exception {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
Map<String,Object> props = new HashMap<String, Object>();
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(UploadService.class);
factory.setAddress("http://localhost:8080/CXFUpload/Upload?wsdl");
UploadService client = (UploadService) factory.create();
FileUploader file = new FileUploader();
file.setName("copy");
file.setFileType("txt");
DataSource source = new FileDataSource(new File("/opt/upload/original.txt"));
file.setDfile(new DataHandler(source));
client.uploadFile(file);
System.exit(0);
}
}
Output
May 17, 2020 6:23:36 PM org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass INFO: Creating Service {http://student.com/}UploadServiceService from class com.student.UploadService May 17, 2020 6:23:36 PM org.apache.cxf.ext.logging.slf4j.Slf4jEventSender performLogging INFO: REQ_OUT Address: http://localhost:8080/CXFUpload/Upload?wsdl HttpMethod: POST Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:91626194-8a24-430e-ad43-e6df8fac836f"; start="<root.message@cxf.apache.org>"; start-info="text/xml" ExchangeId: d0a4fd95-65f4-43fe-a5a9-b805202430ff ServiceName: UploadServiceService PortName: UploadServicePort PortTypeName: UploadService Headers: {SOAPAction="", Accept=*/*} Payload: --uuid:91626194-8a24-430e-ad43-e6df8fac836f Content-Type: application/xop+xml; charset=UTF-8; type="text/xml" Content-Transfer-Encoding: binary Content-ID: <root.message@cxf.apache.org> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:uploadFile xmlns:ns2="http://student.com/"><file><dfile><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:6fd21531-3a71-415e-96d7-3651df59b95d-1@cxf.apache.org"/></dfile><fileType>txt</fileType><name>copy</name></file></ns2:uploadFile></soap:Body></soap:Envelope> --uuid:91626194-8a24-430e-ad43-e6df8fac836f Content-Type: text/plain Content-Transfer-Encoding: binary Content-ID: <6fd21531-3a71-415e-96d7-3651df59b95d-1@cxf.apache.org> Content-Disposition: attachment;name="original.txt" This is a test for mtom upload --uuid:91626194-8a24-430e-ad43-e6df8fac836f-- May 17, 2020 6:23:36 PM org.apache.cxf.ext.logging.slf4j.Slf4jEventSender performLogging INFO: RESP_IN Address: http://localhost:8080/CXFUpload/Upload?wsdl Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:c9d34db7-375b-4551-8e14-c101eaa26c62"; start="<root.message@cxf.apache.org>"; start-info="text/xml" ResponseCode: 200 ExchangeId: d0a4fd95-65f4-43fe-a5a9-b805202430ff ServiceName: UploadServiceService PortName: UploadServicePort PortTypeName: UploadService Headers: {Keep-Alive=timeout=20, connection=keep-alive, content-type=multipart/related; type="application/xop+xml"; boundary="uuid:c9d34db7-375b-4551-8e14-c101eaa26c62"; start="<root.message@cxf.apache.org>"; start-info="text/xml", Content-Length=407, Date=Sun, 17 May 2020 16:23:36 GMT} Payload: --uuid:c9d34db7-375b-4551-8e14-c101eaa26c62 Content-Type: application/xop+xml; charset=UTF-8; type="text/xml" Content-Transfer-Encoding: binary Content-ID: <root.message@cxf.apache.org> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:uploadFileResponse xmlns:ns2="http://student.com/"/></soap:Body></soap:Envelope> --uuid:c9d34db7-375b-4551-8e14-c101eaa26c62--
Below you can see original.txt is uploaded as copy.txt using mtom