cancel
Showing results for 
Search instead for 
Did you mean: 

Extract attachment from xop:Include using SAP CI

tiyasabiswas
Discoverer
0 Kudos

Hi,

I am using SAP CI, and have used SOAP 1.x connector from which I am receiving payload as the SOAP response. In this SOAP response returned, the below <xop:Include>contains the attachment which I need to extract and send to 3rd party system -

<ns0:File href="test.xlsm" name="">

<ns0:Contents>

<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:38618b0a-4c86-4c6f-9041-2b5a327e4451"/>

</ns0:Contents>

</ns0:File>

Need help with how I can extract the attachment contained and then send it to the 3rd party?

Thanks in advance!

View Entire Topic
BhaskarY
Participant
0 Kudos

Hi Tiyasa,

You need to Parse the response received using XML Parser to find the Include and then retrieve the attachment using the href and c-id. I think you will receive it in Base64 encoding, you need to decode it and then send to your 3rd party system.

Can you try below.

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
    def soapPayload = message.getBody()  // Get the SOAP response payload
    def xmlParser = new com.sap.it.api.mapping.xpath.XPath; // Parse SOAP response using XMLStream
    def xmlStream = new ByteArrayInputStream(soapPayload.getBytes("UTF-8"))
    def rootNode = xmlParser.executeXpath("/")
    def xopInclude = rootNode.getChild("Include", "http://www.w3.org/2004/08/xop/include") // Find the xop:Include element
    def cidValue = xopInclude.getAttribute("href").value.split(":")[1] // Retrieve the cid value
    // Find the attachment content
    def contentsNode = rootNode.getChild("Contents", "ns0") // Assuming "ns0" is your namespace prefix
    def attachmentNode = contentsNode.getChild(cidValue, "http://www.w3.org/2004/08/xop/include")
    def attachmentData = attachmentNode.text // Get the attachment data
    def decodedAttachment = javax.xml.bind.DatatypeConverter.parseBase64Binary(attachmentData) // Decode the attachment data
    message.setBody(decodedAttachment)
    return message
}

Regards,

Bhaskar.
tiyasabiswas
Discoverer
0 Kudos

Thank you but the script is failing with the below error in SAP CI -

unexpected token: ; @ line 7, column 59. sap.it.api.mapping.xpath.XPath; // Parse

I tried adding the below imports yet it returned the same error-

import com.sap.gateway.ip.core.customdev.util.Message import com.sap.it.api.mapping.xpath.XPath import groovy.xml.XmlUtil def Message processData(Message message) { def soapPayload = message.getBody() // Get the SOAP response payload def xmlParser = new com.sap.it.api.mapping.xpath.XPath; // Parse SOAP response using XMLStream def xmlStream = new ByteArrayInputStream(soapPayload.getBytes("UTF-8")) def rootNode = xmlParser.executeXpath("/") def xopInclude = rootNode.getChild("Include", "http://www.w3.org/2004/08/xop/include") // Find the xop:Include element def cidValue = xopInclude.getAttribute("href").value.split(":")[1] // Retrieve the cid value // Find the attachment content def contentsNode = rootNode.getChild("Contents", "ns0") // Assuming "ns0" is your namespace prefix def attachmentNode = contentsNode.getChild(cidValue, "http://www.w3.org/2004/08/xop/include") def attachmentData = attachmentNode.text // Get the attachment data def decodedAttachment = javax.xml.bind.DatatypeConverter.parseBase64Binary(attachmentData) // Decode the attachment data message.setBody(decodedAttachment) return message }