Upload attachment (document) using CXF ( MTOM ) and Tomcat

September 29, 2009
Filed under: Featured, Tomcat 
Upload attachment (document) using CXF ( MTOM ) and Tomcat
Let us now create a resume upload web service using Apache CXF, Spring and Tomcat and consume it. The web service will be used to upload an attachment [resume]. The scenario considered here is a candidate uploading his/her resume and the resume [word document] is retrieved and stored on the server.

Please download the zip file containing both the server and client Maven projects 

We have two Maven projects attached. The first project is the web application deployed on the JEE server with a context cxfupload and the second is the Java client which invokes the web service.

Let us start with the web service interface which is provided below.

01.package com.srack.service;
03.import javax.jws.WebParam;
04.import javax.jws.WebService;
06.import com.srack.dto.Resume;
09.public interface ResumeUploadService {
11. void uploadResume(@WebParam(name = "resume") Resume resume);

The implementation for the above interface is provided by ResumeUploadServiceImpl.java which is partly provided below (the complete source is in the zip attached).

view source


01.package com.srack.service;
03.import java.io.File;
04.import java.io.FileOutputStream;
05.import java.io.IOException;
06.import java.io.InputStream;
07.import java.io.OutputStream;
09.import javax.activation.DataHandler;
10.import javax.jws.WebService;
12.import com.srack.dto.Resume;
14.@WebService(endpointInterface = "com.srack.service.ResumeUploadService",
15. serviceName = "ResumeService")
16.public class ResumeUploadServiceImpl implements ResumeUploadService {
18. public void uploadResume(Resume resume) {

Next comes the CXF configuration file cxf.xml which has the configuration which will be loaded by Spring and used by CXF about the web service. We have mentioned jaxws:properties which enables mtom capability of CXF where in the resume is sent as an attachment giving better performance [by avoiding base 64 encoding].

03. xmlns:jaxws="http://cxf.apache.org/jaxws"
09. <import resource="classpath:META-INF/cxf/cxf.xml" />
10. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
11. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
12. <jaxws:endpoint id="uploadresume"
13. implementor="com.srack.service.ResumeUploadServiceImpl"
14. address="/UploadResumeWS">
15. <jaxws:properties>
16. <entry key="mtom-enabled" value="true"/>
17. </jaxws:properties>
18. </jaxws:endpoint>

The web.xml configure the CXF servlet to listen at the desired URL pattern as below.

01.<?xml version="1.0"?>
02.<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
06. <display-name>ResumeUpload</display-name>
07. <context-param>
08. <param-name>contextConfigLocation</param-name>
09. <param-value>classpath:cxf.xml</param-value>
10. </context-param>
11. <listener>
12. <listener-class>
13. org.springframework.web.context.ContextLoaderListener
14. </listener-class>
15. </listener>
16. <servlet>
17. <servlet-name>CXFServlet</servlet-name>
18. <servlet-class>
19. org.apache.cxf.transport.servlet.CXFServlet
20. </servlet-class>
21. </servlet>
22. <servlet-mapping>
23. <servlet-name>CXFServlet</servlet-name>
24. <url-pattern>/services/*</url-pattern>
25. </servlet-mapping>

Client Application consuming the web services:

Client.java populates values for candidate name, file attachment type, and the resume [which is path to a file] and invokes the web service as below.

01.public static void main(String args[]) throws Exception {
03. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
05. factory.getInInterceptors().add(new LoggingInInterceptor());
06. factory.getOutInterceptors().add(new LoggingOutInterceptor());
07. factory.setServiceClass(ResumeUploadService.class);
08. factory.setAddress
09. ("http://localhost:8080/cxfupload/services/UploadResumeWS");
10. ResumeUploadService client = (ResumeUploadService) factory.create();
12. Resume resume=new Resume();
13. resume.setCandidateName("CandidateNameXXX");
14. resume.setResumeFileType("doc");
16. DataSource source = new FileDataSource(new File("/home/pathtofile/resume.doc"));
17. resume.setResume(new DataHandler(source));
18. client.uploadResume(resume);
19. System.exit(0);
21. }


3 Comments on Upload attachment (document) using CXF ( MTOM ) and Tomcat

  1. wallmart on Tue, 20th Oct 2009 2:14 pm
  2. Can not imagine how glad I am that got to your post

  3. macafee on Wed, 21st Oct 2009 9:34 am
  4. I’m always amazed how you can so easily write your message

  5. stanley on Thu, 24th Jun 2010 5:58 am
  6. Many thanks

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image