cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Outbound proxy attachment to CPI Encoding Problem

dan166
Explorer
0 Likes
3,674

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

Accepted Solutions (1)

Accepted Solutions (1)

Muniyappan
Active Contributor

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
Explorer
0 Likes

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

SrikanthR-P
Discoverer
0 Likes

Hi Muniyappan,

I have same requirement like outbound proxy to cpi with attachment. Please share the Abap side code and outbound proxy structure for attachment.

Thanks in advance @Muniyappan 

Thanks.

Answers (2)

Answers (2)

Muniyappan
Active Contributor
0 Likes

Hi Daniel,

It could be that when you upload a file you are using different encoding and when you read in cpi, you are using different encoding.

Could you please pass on the abap report code wich uploads the files? what is the code page are you using?

Also please share us the groovy script you are using to read the attachment.

dan166
Explorer

Hi Muni, thanks for your feedback. I am not specifically defining any codepage anywhere. I am just wondering why it shows correctly in SXMB_MONI but not in the CPI. The ABAP code reading the file and sending it through the proxy is as following:

form xml_processing.

data:
l_rc type i,
li_files type filetable,
l_action type i,
l_filesize type w3param-cont_len,
l_filetype type w3param-cont_type,
li_bin_data type w3mimetabtype,
l_xml_data type string.

data:
lcl_file type ref to zbc_co_file_out,
lcl_attachment_protocol type ref to if_wsprotocol_attachments,
lcl_attachment type ref to if_ai_attachment,
lcl_attachments type prx_attach,
ls_output type zbc_mt_file_out_request,
ls_record type zbc_dt_file_out_request.

try.

"call file open dialog
cl_gui_frontend_services=>file_open_dialog(
exporting
file_filter = |xml (*.xml)\|*.xml\|{ cl_gui_frontend_services=>filetype_all }|
multiselection = abap_false
changing
file_table = li_files
rc = l_rc
user_action = l_action ).

if l_action = cl_gui_frontend_services=>action_ok.
if lines( li_files ) > 0. "file entries selected

"call binary file upload
cl_gui_frontend_services=>gui_upload(
exporting
filename = |{ li_files[ 1 ]-filename }|
filetype = 'BIN'
importing
filelength = l_filesize
changing
data_tab = li_bin_data ).

"Convert uploaded data to string
call function 'SCMS_BINARY_TO_STRING'
exporting
input_length = l_filesize
importing
text_buffer = l_xml_data
tables
binary_tab = li_bin_data
exceptions
failed = 1
others = 2.
if sy-subrc <> 0.
message i219 with '<StringConversion>'.
exit.
endif.

endif.
endif.

catch cx_root into data(e_text).
message e_text->get_text( ) type 'S' display like 'E'.

endtry.

if lines( li_files ) = 0. "file entries selected
message i074.
exit.
endif.

"create client proxy object by passing logical port
try.
create object lcl_file exporting logical_port_name = space.
catch cx_ai_system_fault.
endtry.

* send pdf attachment xstring in request
lcl_attachment_protocol ?= lcl_file->get_protocol( if_wsprotocol=>attachments ).

"pdf mime type
lcl_attachment = lcl_attachment_protocol->get_attachment_from_text(
data = l_xml_data
type = if_ai_attachment=>c_mimetype_text_xml
name = 'File_Attachment' ).
append lcl_attachment to lcl_attachments.
lcl_attachment_protocol->set_attachments( lcl_attachments ).

* pass data in request field
ls_record-name = li_files[ 1 ]-filename.
ls_output-mt_file_out_request = ls_record.

* call proxy
if lcl_file is bound.
try.
call method lcl_file->execute
exporting
output = ls_output.
commit work.
catch cx_ai_system_fault.
endtry.
endif.

message i048. "success

endform.

The groovy script that retrieves the attachment and puts it into the body (and the MPL) is as following:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
      
    //get attachment
    def attach = message.getAttachments();
    def datahandler = attach.values()[0];
    def content =  datahandler.getContent();
  
    // 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, attachmentType);
    }
    message.setBody(content);
    return message;
}
Muniyappan
Active Contributor
0 Likes

thanks for that.

RaymondGiuseppi
Active Contributor
0 Likes

For those western Europe characters (e.g. German umlauts, French accented characters) try using ISO8859-1 (Latin-1) encoding specified in front of the root element (sample) to prevent a parsing error 201

<?xml version="1.0" encoding="ISO8859-1"?>
dan166
Explorer
0 Likes

Thank you Raymond for your suggestion. Unfortunately, this doesn't make it any better