Using SOAP Attachment from Coldfusion

I came accros the issue on accessing the java web service which will return SOAP attachment . Tried goole to check whether there are any implementation using coldfusion but we could’t find . So we decided to use SAAJ API to get the soap attachment

Follwoing are the steps i used

Get these saaj-api.jar , saaj-impl.jar jar file  can be get from

copied the jar file in coldfusion lib directory

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<title>Soap Atachment</title>


<cfobject action=”create” type=”java” class=”javax.xml.soap.AttachmentPart” name=”Apart”>
<cfobject action=”create” type=”java” class=”” name=”URL”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.MessageFactory”name=”MF”>
<cfobject action=”create” type=”java”   class=”javax.xml.soap.Name” name=”N”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPBody” name=”SB”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPBodyElement” name=”SBE”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPConnection” name=”SPC”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPConnectionFactory” name=”SCPF”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPElement” name=”SEL”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPEnvelope” name=”ENV”>
<cfobject action=”create” type=”java” class=”javax.xml.soap.SOAPMessage” name=”SM”>
<cfobject action=”create” type=”java”  class=”javax.xml.soap.SOAPPart” name=”SP”>

<!— Following are the information that required by the web service as a parameter ” Starts here —>
<cfset dm_lib = “<Sometext>”>
<cfset  doc_ver = “”>
<cfset  documentId = “<Number>”>
<cfset  profile_form = “EKRIS_LAD_CPD_PF”>
<cfset app_user_id = “userid”>
<cfset user_id = “userid”>
<!— Following are the information that required by the web service as a parameter ” Ends here —>

<!— Calling methods of SAAJ api to create a the connection made the reques  —>
<cfset scFactory = SCPF.newInstance()>
<cfset con= scFactory.createConnection()>
<cfset factory = MF.newInstance()>
<cfset Message =  factory.createMessage()>
<cfset soapPart = message.getSOAPPart()>
<cfset envelope = soapPart.getEnvelope()>
<cfset body = envelope.getBody()>

<!— here mentioned the methodname—>
<cfset bodyName = envelope.createName(“RetrieveDocument”, “”, “;)>

<!— Create the element for Sopa which will be part of Soap Request Starts here —>
<cfset gltp = body.addBodyElement(bodyName)>
<cfset elementName = envelope.createName(“DMlib”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(dm_lib)>
<cfset elementName = envelope.createName(“DocumentNumber”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(documentId)>
<cfset elementName = envelope.createName(“VersionNo”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(“”)>

<cfset elementName = envelope.createName(“ProfileForm”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(profile_form)>

<cfset     elementName = envelope.createName(“appUserID”)>
<cfset  element = gltp.addChildElement(elementName)>
<cfset     element.addTextNode(app_user_id)>

<cfset elementName = envelope.createName(“userID”)>
<cfset element = gltp.addChildElement(elementName)>
<cfset element.addTextNode(user_id)>
<!— Create the element for Sopa which will be part of Soap Request ends here —>

<!— Calling the WS starts here—->
<cfset strUrl =”http://Domainname/webservices/jws/<webservicename&gt;.jws”>

<cfset jUrl = CreateObject( “java”, “” ).Init(strUrl) />
<cfset response =, jUrl)>
<!— Calling the WS ends here—->

<!— getting the attachment part starts here—->
<cfset retrievedAttachments = response.getAttachments()>
<cfset AttachmentPart =>
<cfset dataHandler = AttachmentPart.getDataHandler()>
<!— This will have the attachment part ends here—->

<!— Write the file from the attachment to the mentioned location starts  here—->

//… other code

saveToFileName = “C:\Vishnu123.jpg”;
inputStream =  dataHandler.getInputStream();
outStream = createObject(“java”,””).init();
// create byte array. read up to the first
// 1024 bytes (or however many) into the array
byteClass = createObject(“java”, “java.lang.Byte”).TYPE;
byteArray = createObject(“java”,”java.lang.reflect.Array”).newInstance(byteClass, javacast(“int”, 1024));
length =;

// if there is any data to read
offset = 0;
while ( length GT 0) {
outStream.write( byteArray, offset, length );
length = byteArray );


FileWrite( saveToFileName, outStream.toByteArray() );
<!— Write the file from the attachment to the mentioned location starts  here—->


Using this we saved the SOAP attachment to the file server .

If anyone think about the alternative way or any suggetion for improvment are welcome


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s