CXF File Upload With SOAP MTOM

CXF File Upload With SOAP MTOM shows Implementing a SOAP service for uploading/sending large attachment using MTOM (SOAP Message Transmission Optimization Mechanism)
MTOM is used for encoding binary data in base64Binary and send as binary attachement 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
MTOM is used for sending large attachments using WSDL based services (SOAP).
You can go through following tutorial, in order to use MTOM inside CXF
Required Libraries
You need to download
Following jar must be in classpath
- cxf-core-3.3.6.jar
- cxf-rt-bindings-soap-3.3.6.jar
- cxf-rt-databinding-jaxb-3.3.6.jar
- cxf-rt-features-logging-3.3.6.jar
- cxf-rt-frontend-jaxws-3.3.6.jar
- cxf-rt-frontend-simple-3.3.6.jar
- cxf-rt-transports-http-3.3.6.jar
- cxf-rt-wsdl-3.3.6.jar
- httpasyncclient-4.1.4.jar
- httpclient-4.5.12.jar
- httpcore-4.4.13.jar
- httpcore-nio-4.4.13.jar
- neethi-3.1.1.jar
- slf4j-api-1.7.29.jar
- slf4j-jdk14-1.7.29.jar
- spring-aop-5.1.14.RELEASE.jar
- spring-beans-5.1.14.RELEASE.jar
- spring-context-5.1.14.RELEASE.jar
- spring-core-5.1.14.RELEASE.jar
- spring-expression-5.1.14.RELEASE.jar
- spring-jcl-5.1.14.RELEASE.jar
- spring-web-5.1.14.RELEASE.jar
- stax2-api-3.1.4.jar
- woodstox-core-5.0.3.jar
- wsdl4j-1.6.3.jar
- xmlschema-core-2.2.5.jar
CXF MTOM File Upload Example
I am creating a simple web service project used for uploading different file formats using CXF MTOM implementation.
The service is using simple POJO (Plain Old Java Object) bean.
Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFUpload" according to following screenshot
Create a FileUploader Object
This is a pojo class used to mapping the binary data, that we are going to upload
import javax.activation.DataHandler;
public class FileUploader {
private String Name;
private String FileType;
private DataHandler Dfile;
public String getName() {
return this.Name;
}
public void setName(String Name) {
this.Name = Name;
}
public DataHandler getDfile() {
return this.Dfile;
}
public void setDfile(DataHandler Dfile) {
this.Dfile = Dfile;
}
public String getFileType() {
return FileType;
}
public void setFileType(String FileType) {
this.FileType = FileType;
}
}
Create a Service Interface
This service interface will defines which methods of web service, to be invoked by the client
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface UploadService {
void uploadFile(@WebParam(name = "file") FileUploader file);
}
Implement the Service Interface
Here we will implement the service interface created on the previous step
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.jws.WebService;
@WebService(endpointInterface = "com.student.UploadService", serviceName = "UploadService")
public class UploadServiceImpl implements UploadService {
public void uploadFile(FileUploader Dfile) {
DataHandler handler = Dfile.getDfile();
try {
InputStream is = handler.getInputStream();
OutputStream os = new FileOutputStream(new File("C:/uploads/" + Dfile.getName() + "." + Dfile.getFileType()));
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
cxf.xml
CXF is using Spring internally, Finding classes by spring we need to add service implementation class on "jaxws:endpoint" tag
we also added following line in order to enable the MTOM support inside CXF framework
<entry key="mtom-enabled" value="true" />
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:endpoint id="uploadfile" implementor="com.student.UploadServiceImpl" address="/Upload"> <jaxws:properties> <entry key="mtom-enabled" value="true" /> </jaxws:properties> </jaxws:endpoint> </beans>
web.xml
Change the web.xml file to find CXF servlet and cxf.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/cxf.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Publishing CXF MTOM Web Service
Deployed CXF MTOM Web Service