Base64 Encoding and Decoding UDFs for PI/PO (Example)

There are several ways to encode and decode Base64 messages in SAP PI/PO. In this post, we will discuss how to convert Base64 using User Defined Functions (UDF).

 

 

You can also use Java Mapping class to encode or decode Base64 format. In SAP Back-end system the same functionality can be achieved using ABAP Function Modules or ABAP class CL_HTTP_UTILITY.

 

 

Encoding and Decoding Example Overview:

 

 

Let’s assume, we have a source XML message with two fields. One field contains the Base64 encoded string and the other field contains the string we need to encode to Base64.

 

 

base64 encoding and decoding using UDFs example. Example Overview: Functionality of the UDFs and expected result. Graphical mapping tool test.

 

Example Overview: Functionality of the UDFs and expected result.

 

 

Base64 encoded message is included in source message element <EncodedString>. UDF decodes this message. Decoded message will be mapped to target field <DecodedString>.

 

 

The text we should convert to Base64 is included in the source message node <PlainString>. The value will be encoded to Base64 format using an UDF and mapped to target XML node <EncodedString>.

 

 

Sender Message Type and Sample:

 

 

Source Message Type for base64 conversion example.

 

Source Message Type

 

 

 

 

Receiver Message Type and Sample:

 

 

Target Message Type for base64 encoding and decoding UDF example in ESR

 

Target Message Type

 

 

 

 

This example will be implemented in an SAP Process Orchestration (PO) system version SAP PO7.50 single stack.

 

 


 

 

Design of the Base64 Encoding and Decoding UDFs:

 

 

We will implement two UDFs, one to encode Base64 and another one to decode Base64.

 

 

We will name,

 

 

  • Encoding UDF as Base64Encoder and
  • Decoding UDF ad Base64Decoder.

 

 

Both UDFs have an Execution Type of “Single Value“. That means the UDF accepts only one argument as input and outputs one value. For example, Base64 decoding UDF accepts Base64 encoded string as the input and returns the encoded string.

 

 

Both encoding and decoding will be carried out using standard methods of Java utility class java.util.Base64. This class contains two nested classes: Base64.Decoder decodes byte data and Base64.Encoder implements the encoder for encoding byte data.

 

 


 

 

Base64 Encoding UDF: Base64Encoder

 

 

Encoding UDF has one input variable “PlainString”. Input value is assigned from the source message element “PlainString”. UDF converts this value to Base64 format.

 

 

Encoding can be broken down to two steps,

 

 

Step 1: Convert String to Byte Array

 

 

First, input string value is converted to byte array using String method getBytes().

 

 

 

 

Step 2: Encode Byte Array to Base64 Format

 

 

Next, the static method encodeToString of nested class Base64.Encoder decodes the byte array to actual text.

 

 

getEncoder() method returns the Base64.Encoder.

 

 

 

 

Complete Encoding UDF:

 

 

Make sure to import the java class java.util.Base64 to UDF definition.

 

 

java.util.base64 class imported to UDF in function library ESR

 

java.util.base64 class imported to UDF

 

 

Function Library View:

 

 

Encoding UDF in function library

 

Encoding UDF

 

 

Code Snippet:

 

 

 

 

Mapping:

 

 

Mapping encoder from source to target in ESR graphical mapping

 

Mapping encoder from source to target

 

 


 

 

Base64 Decoding UDF:
Base64Decoder

 

 

Decoding UDF also contains only one input variable. UDF accepts Base64 encoded value using input variable “EncodedString”. Function of the UDF is to decode the encoded string value and return plain text.

 

 

Decoding can be broken down to two steps,

 

 

Step 1: Transform Encoded String to Encoded Byte Array

 

 

Decode method of nested class Base64.Decoder converts Base64-encoded string into encoded bytes array.

 

 

Method getDecoder() returns the Base64.Decoder class.

 

 

 

 

Step 2: Decode Encoded Byte Array to Text

 

 

Next, encoded byte array is converted to string.

 

 

 

 

Complete Decoding UDF:

 

 

Make sure to import the java class java.util.Base64 to UDF definition.

 

 

java.util.base64 class imported to function

 

java.util.base64 class imported

 

 

Functions Library View:

 

 

Complete decoder UDF in ESR function library

 

Complete decoder UDF

 

 

Code Snippet:

 

 

 

 

Mapping:

 

 

Source to target mapping for base64 decoder in ESR graphical mapping

 

Source to target mapping for base64 decoder

 

 


 

 

Test the UDFs:

 

 

Let’s test the User Defined Functions and mapping using graphical message mapping test tool.

 

 

Input encoded value is “RGVjb2RlIHRoaXMgdGV4dCE=”. This translates to “Decode this text!”.

 

 

Input value which should be encoded to Base64 schema is “Encode this text!”. Base64 converted value of this is “RW5jb2RlIHRoaXMgdGV4dCE=”.

 

 

Testing UDFs in graphical mapping. ESR message mapping test tool.

 

Testing UDFs in graphical mapping

 

 

Base64 decoder UDF display queue with input output in ESR graphical mapping

 

Base64 decoder UDF display queue

 

 

Base64 encoder UDF display queue in graphical mapping in ESR

 

Base64 encoder UDF display queue

 

 


 

 

If you have any questions about Base64 encoding and decoding using User Defined Functions, Java Mapping, or ABAP applications, leave a comment below! Have you used any other methods to convert Base64 schema in SAP integration scenarios?

 

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Get the latest tips and tricks on SAP!

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *