Skip to Content
0
Aug 27, 2023 at 08:47 AM

xml to attachment creation error at receiver side for SOAP in CPI

113 Views Last edit Aug 27, 2023 at 01:48 PM 5 rev

Dear Experts,

i have a scenario to read xml from source and pass the same content as attachment along with the soap envelop(and the attachment name as content Id inside the body elements) to receiver system.

so i used the groovy script as suggested in the following link Create Attachments by SAP Help but I'm getting the below error.

javax.script.ScriptException: java.lang.Exception: java.lang.NullPointerException: 
Cannot invoke method indexOf() on null object@ line 12 in script3.groovy, 
cause: java.lang.NullPointerException: Cannot invoke method indexOf() on null object

and the script used added below along with sample xml

Kindly let me know if any change required in script level.

<ns0:sddaa xmlns:ns0="http://sddaa.com/schema/service/Upload/1.0">
<ns0:SignRq>
  <ns0:ClientDt>2023-07-23T11:50:52</ns0:ClientDt>
  <ns0:LanguagePref>en-gb</ns0:LanguagePref>
  <ns0:SignProfile>
     <ns0:Sender>224</ns0:Sender>
     <ns0:Receiver>test-001</ns0:Receiver>
     <ns0:MsgCode>BUP</ns0:MsgCode>
  </ns0:SignProfile>
</ns0:SignRq>
</ns0:sddaa>
import com.sap.gateway.ip.core.customdev.util.Message
import org.apache.camel.impl.DefaultAttachment 
import javax.mail.util.ByteArrayDataSource 

def Message processData(Message message)
 { // Get content type 
def headers = message.getHeaders() 
def contentType = headers.get("Content-Type") 
// Define suffix 
def suffix = contentType.substring(contentType.indexOf("/")+1)
 if(suffix == "plain"){ suffix = 'txt' } 
// Get message payload as input stream 
def body = message.getBody(InputStream.class) 
// Set MIME type 
def dataSource = new ByteArrayDataSource(body, contentType) 
// Construct a DefaultAttachment object 
def attachment = new DefaultAttachment(dataSource) 
// Add the attachment to the message 
message.addAttachmentObject('attachment.' + suffix, attachment) 
return message; 
}