Thursday, April 3, 2008

SOAP attachments with AXIS

I wanted to create a Web Service that accepts a JPEG image as the input. The preferred way to send an image in a SOAP message is using SOAP attachments.

Below is a sample WSDL file for a service that accepts an image as input.

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="newWSDL" targetNamespace=""

   <message name="isLabelAuthenticRequest">
       <part name="labelImage" type="xsd:hexBinary"/>
   <message name="isLabelAuthenticReply">
       <part name="isAuthentic" type="xsd:boolean"/>
   <portType name="isLabelAuthenticPortType">
       <operation name="isLabelAuthenticOperation">
           <input name="authenticInput" message="tns:isLabelAuthenticRequest"/>
           <output name="authenticOutput"
   <binding name="isLabelAuthenticBinding" type="tns:isLabelAuthenticPortType">
       <soap:binding style="rpc" transport=""/>
       <operation name="isLabelAuthenticOperation">
           <input name="authenticInput">
                       <soap:body use="encoded"    
   encodingStyle="" namespace=""/>
                       <mime:content part="labelImage" type="image/jpeg"/>
           <output name="authenticOutput">
               <soap:body use="encoded"
  encodingStyle="" namespace=""/>
   <service name="smartLabelDemoService">
       <port name="isLabelAuthenticPort" binding="tns:isLabelAuthenticBinding">
           <soap:address location=""/>

The highlights of the WSDL file:

  • The following namespace for SOAP must be used to enable support for attachments:
  • The message that will include an attachment is declared to be of type xsd:hexBinary
  • The actual structure of this message is specified in the binding element. It is specified as a multipart message, with the MIME type of the second part set to image/jpeg.

The WSDL2Java utility offered by Axis, should be able to parse this wsdl document and generate the stubs successfully. (I have been using version 1.4 of Axis). Of course, for WSDL2Java to work, all the axis-related jars (ie all the jars in the folder $AXIS_HOME/lib) must be present in the CLASSPATH. Furthermore, for attachment support, we also need mail.jar and activation.jar. The first one is part of the Java Mail API and the second on can be found in the JavaBeans Activation Framework.

However I was getting the following error message when I tried to implement and test the web service:}stackTrace:org.xml.sax.SAXException: Bad envelope tag:  html

I could only understand what was causing this when I used the tcpmon utility to monitor the TCP connections. This actually revealed that the server was returning an HTML document (rather than a SOAP message) containing an error message:

java.lang.RuntimeException: No support for attachments

So, it was a matter of just enabling attachment support on the server side. This can be done by simply copying mail.jar and activation.jar under the location where axis has been deployed, ie under the directory $CATALINA_HOME/webapps/axis/lib.

No comments: