Consuming FedEx Web Services from Grails using Apache CXF

A recent development task was integrating a FedEx web service consumer (client) in a Grails application. Users require the ability to generate FedEx shipping labels from within the app. To accomplish this, we call the SOAP based web services provided by FedEx.

I decided to use Apache CXF for the consumer code.

There were a couple of tricky parts:

1) The FedEx sample client code is Axis based.
2) Integrating the Apache CXF code, libraries and configuration into Grails
3) FedEx service is a secure (https) call

Let’s take a look at how each one of these items can be addressed:

1) The FedEx sample client code is Axis based.
This one is easily solved by using CXF wsdl2java against the FedEx wsdl. CXF tends to provide List based API instead of Arrays that Axis wsdl2java seems to prefer. So, if you review the FedEx sample code which is for Axis, you’ll see much use of Arrays; just switch those over to use Lists.

2) Integrating the CXF code with Grails
Like Grails, CXF also relies on Spring Framework. Create a cxf.xml file for CXF configuration and drop it in the “conf/spring”
directory in your Grails app.

conf/spring/cxf.xml:

<beans xmlns="http://www.springframework.org/schema/beans">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xsi:schemalocation="http://cxf.apache.org/configuration/security
http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
 
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
<http:conduit name="{http://fedex.com/ws/ship/v3}ShipServicePort.http-conduit">
<http:tlsclientparameters securesocketprotocol="SSL">
</http:tlsclientparameters></http:conduit>
</beans>

Include this file from the Grails/Spring conf/spring/resources.xml file:
<beans>
<import resource="ImageCaptchaFactories.xml" />
<import resource="ImageCaptchaService.xml" />
<import resource="EmailService.xml" />
<import resource="cxf.xml" />
</beans>

Next, download the GroovyWS library from the Groovy site and drop it in your Grails lib directory. At the time of this writing it was 0.2.0. Finally,
add the following jars from the Apache CXF - asm-2.2.3.jar, groovyws-standalone-0.2.0.jar, jaxws-api-2.0.jar, saaj-api-1.3.jar, saaj-impl-1.3.jar to your lib directory.

3) FedEx is a secure (https) call
CXF doesn’t support SSL web service calls without a little configurary. Ok, I made the word “configurary” up. I’m pretty sure you can do that when you’re a blog author.

Anyhow, you’ve already seen how to do in step 2:

<http:conduit name="{http://fedex.com/ws/ship/v3}ShipServicePort.http-conduit">
<http:tlsclientparameters securesocketprotocol="SSL">
</http:tlsclientparameters>
</http:conduit>

For more information on CXF configuration see links [1], [2] below.

Ok, you’re ready to roll. You can call the FedEx web service from within Grails.

I wrote this article to try save you some time. If you see something that can be improved and save me some time, leave a comment. Pasting XML code is a bit tricky, so hopefully it’s formatted ok for you.

As you can tell, this article is geared towards configuration rather than code. Hopefully, it seems easy. If it does seem easy, then I’ve achieved my objective.

If you want to see some more code that will probably blow-you-away on how technically gifted I am (oh, common, I’m not serious. It’s just another bad joke like “configurary”.), let me know. Or, if you are looking for a Grails developer, I’m always interested in hearing about opportunities.

Reference

[1] CXF Site SSL Configuration http://cwiki.apache.org/CXF20DOC/client-http-transport-including-ssl-support.html

[2] An article that helped with CXF SSL config: http://techpolesen.blogspot.com/2007/08/using-ssl-with-xfirecxf-battling.html

ServiceCycle is a registered trademark of Supergloo, inc..