cancel
Showing results for 
Search instead for 
Did you mean: 

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

AbdulHammed
Explorer
0 Kudos

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; 
}
View Entire Topic
alex_bundschuh
Product and Topic Expert
Product and Topic Expert

The example that you refer to is triggered by a soap call which passes the content-type header to the integration flow, based on the content-type the file suffix is defined accordingly, in your case obviously the content-type is not set, so you need to adapt your script accordingly, from what I understand you read an xml file and like to attach the same, so your suffix is fixed .xml

Alex