Grails – CXF Web Service with MTOM Attachments

Home / Blog / Grails – CXF Web Service with MTOM Attachments

I’m writing this mostly for my own reference, but I thought it would be helpful to others as well. I’ve been working a lot lately with integrating CXF into grails via the CXF-grails plugin; however, while enabling MTOM to allow SOAP attachments, I ran into a few issues that I had to work around. The following are the steps I took to successfully allow uploading files via my web service:

1. Enable MTOM on the service

@MTOM(enabled = true)
serviceName = "MyWebService",
portName = "MyWebService",
targetNamespace = "",
endpointInterface = "")
class MyWebService ...

2. Add the MIME type to the DataHandler request object.

This step allows the binding of the incoming attachment to the bean field.

// ... some request bean
private DataHandler fileData;

3. Exclude the ‘/services’ path from UrlMappings.groovy.

Without this step, the default grails URL interceptor will process the request as a file upload, which in turn will read the InputStream, causing the CXF interceptor to have an empty stream.

static excludes = ['/services*']

4. Remove mail.jar & activation.jar from the classpath in BuildConfig.groovy

My project is using the mail plugin, which manually copies over these jars from it’s /lib folder. One of the CXF interceptors will throw an error since the classes contained within these jars has already been loaded by the servlet containers classpath.

grails.war.resources = { stagingDir, args ->
println "Removing mail.jar and activation.jar from WEB-INF/lib/"
delete(file: "${stagingDir}/WEB-INF/lib/mail.jar")
delete(file: "${stagingDir}/WEB-INF/lib/activation.jar")

Now, your grails-cxf web service should be able to properly process an incoming MTOM SOAP message. One thing to note is that you may need to extend and modify the LoggingInInterceptor since it doesn’t know how to properly log the incoming messages with multiple boundaries. I can provide examples of this as well if anyone is interested. I’ve tested my service using SoapUI as a SOAP client. SoapUI allows you to specify MTOM settings on your test requests as well as adding attachments.

I’ve done a lot more work with Grails-CXF integration, so if there are other topics that anyone is interested in, email me and let me know. ([email protected])

Leave a Comment