CXF File Upload With SOAP MTOM

Required Libraries

You need to download

  1. OpenJDK 8 / Corretto
  2. Eclipse 4.15
  3. CXF-3.3.6
  4. Tomcat 9

Following jar must be in classpath

  1. cxf-core-3.3.6.jar
  2. cxf-rt-bindings-soap-3.3.6.jar
  3. cxf-rt-databinding-jaxb-3.3.6.jar
  4. cxf-rt-features-logging-3.3.6.jar
  5. cxf-rt-frontend-jaxws-3.3.6.jar
  6. cxf-rt-frontend-simple-3.3.6.jar
  7. cxf-rt-transports-http-3.3.6.jar
  8. cxf-rt-wsdl-3.3.6.jar
  9. httpasyncclient-4.1.4.jar
  10. httpclient-4.5.12.jar
  11. httpcore-4.4.13.jar
  12. httpcore-nio-4.4.13.jar
  13. neethi-3.1.1.jar
  14. slf4j-api-1.7.29.jar
  15. slf4j-jdk14-1.7.29.jar
  16. spring-aop-5.1.14.RELEASE.jar
  17. spring-beans-5.1.14.RELEASE.jar
  18. spring-context-5.1.14.RELEASE.jar
  19. spring-core-5.1.14.RELEASE.jar
  20. spring-expression-5.1.14.RELEASE.jar
  21. spring-jcl-5.1.14.RELEASE.jar
  22. spring-web-5.1.14.RELEASE.jar
  23. stax2-api-3.1.4.jar
  24. woodstox-core-5.0.3.jar
  25. wsdl4j-1.6.3.jar
  26. 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 CXF MTOM Project CXF File Upload With SOAP MTOM

Create a FileUploader Object

This is a pojo class used to mapping the binary data, that we are going to upload

package com.student;

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

package com.student;

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

package com.student;

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

Publish MTOM On Tomcat

Note

You can Find all the deployed JAX-WS/JAX-RS services you need to append 'services' at the end of the URL so URL will become following

http://localhost:8080/CXFUpload/services

Deployed CXF MTOM Web Service

CXF MTOM WebService Running

Note

you can also see CXF MTOM Client in order to run this service





4 Responses to "CXF File Upload With SOAP MTOM"
  1. skanda 2013-04-18 10:37:17.0
  1. skanda 2013-04-19 10:37:17.0
  1. Bob Rivers 2013-04-20 10:37:17.0
  1. FerminLucky 2013-04-21 10:37:17.0

Your email address will not be published. Required fields are marked *