Upload attachment (document) using CXF ( MTOM ) and 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;
02.
03.import javax.jws.WebParam;
04.import javax.jws.WebService;
05.
06.import com.srack.dto.Resume;
07.
08.@WebService
09.public interface ResumeUploadService {
10.
11. void uploadResume(@WebParam(name = "resume") Resume resume);
12.}

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

01.package com.srack.service;
02.
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;
08.
09.import javax.activation.DataHandler;
10.import javax.jws.WebService;
11.
12.import com.srack.dto.Resume;
13.
14.@WebService(endpointInterface = "com.srack.service.ResumeUploadService",
15. serviceName = "ResumeService")
16.public class ResumeUploadServiceImpl implements ResumeUploadService {
17.
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"
08.
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>
19.</beans>

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"
04.
05.<web-app>
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>
26.</web-app>

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 {
02.
03. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
04.
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();
11.
12. Resume resume=new Resume();
13. resume.setCandidateName("CandidateNameXXX");
14. resume.setResumeFileType("doc");
15.
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);
20.
21. }

3 thoughts on “Upload attachment (document) using CXF ( MTOM ) and Tomcat”

Leave a Reply

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

*
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