cancel
Showing results for 
Search instead for 
Did you mean: 

Outbound proxy attachment to CPI Encoding Problem

dan166
Discoverer
0 Kudos

I am working on a scenario where I want to upload an XML file in ABAP and send it as an attachment through an outbound proxy to the CPI/IS for further processing.

As a PoC, I am uploading an XML file from the frontend and attaching it to the proxy as string using the corresponding attachment classes and methods. This generally works fine and in SXMB_MONI I see the attachment correctly presented, even German umlaute like <POTX1>äöüÄÖÜ</POTX1>.

In the CPI/IS, I have a groovy script based on Morten Wittrocks blog that retrieves the attachment from the proxy message and puts it into the message body for further processing. Everything looks fine, except that the German umlaute are showing up as <POTX1>äöüÄÖÜ</POTX1> now.

Did anybody experience a similar issue and solved it or has an idea how this could be handled to show those characters correctly?

Thanks in advance for the feedback,

Daniel

View Entire Topic
Muniyappan
Active Contributor
0 Kudos

Thanks for providing the code of abap client proxy and groovy script. I was able to replicate your issue.

This is the slightly modifed groovy is giving me the correct output.

 import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

def Message processData(Message message) {
      
    //get attachment
    def attach = message.getAttachments();
    def datahandler = attach.values()[0];
    def content =  datahandler.	getInputStream();
 
     def content_string = getString(content)
  
    // Read from properties to get an attachment name
    def mapProperties = message.getProperties();
    def attachmentName = mapProperties.get("AttachmentName");
    def attachmentType = mapProperties.get("AttachmentType");
    // Set default values
	if (!attachmentName?.trim()) {
	    attachmentName = "Attachment";
	}
	if (!attachmentType?.trim()) {
	    attachmentType = "text/plain";
	}

    def messageLog = messageLogFactory.getMessageLog(message);
    if(messageLog != null){
        messageLog.setStringProperty("Logging", "Payload As Attachment");
        messageLog.addAttachmentAsString(attachmentName, content_string, attachmentType);
    }
    message.setBody(content);
    return message;
}

def String getString(def inputStream){
     ByteArrayOutputStream result = new ByteArrayOutputStream();
 byte[] buffer = new byte[1024];
 for (int length; (length = inputStream.read(buffer)) != -1; ) {
     result.write(buffer, 0, length);
 }
 // StandardCharsets.UTF_8.name() > JDK 7
 return result.toString(StandardCharsets.UTF_8.name());

}

Output :

the issue you are facing could be due to java 8 is using utf-16 as default encoding. I might be wrong here. But feel free to correct me.

dan166
Discoverer
0 Kudos

Hi Muni

Fantastic, thanks for your solution which also works great on my end! Being an UTF-16 issue seems plausible, nevertheless getting the defaultCharset in the script returns UTF-8 and not 16. Anyhow, I'll play around a bit more but very much appreciate your solution.

BR

Daniel