cancel
Showing results for 
Search instead for 
Did you mean: 

Base64 Encoding using UDF

Former Member
0 Kudos

Hi,

I have a File to SOAP scenario.  I am getting a csv file and would like to send that file as base encoded 64.

I am thinking of concatenating all fields of source file in the message mapping and convert it as base64 and map it to soap envelope.

Can anyone let me know any UDF which can be used for this.  What are the java classes that needs to be uploaded for this?

Please do let me know the same.

Regards

Anandh.B

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I know this thread is old but it comes up as result number 1 when searching for "base64 udf"

My system could not import the class mentioned by Baskar so I'm posting what worked for me:

Create UDF named b64encode with one input called myInput

include sun.misc.BASE64Encoder

public String b64encode(String myInput, Container container) throws StreamTransformationException{

     BASE64Encoder encoder = new BASE64Encoder();

     String encoded = encoder.encode(myInput.getBytes());

     return encoded;

}

nabendu_sen
Active Contributor
0 Kudos

Hi Aaron,

Thanks for adding valuable inputs.

Former Member
0 Kudos

Hi Aaron,

I've tried to use the udf you've posted but i get an error while validating:

I've imported the sun.misc.BASE64Decoder.jar.

can u help ?

Answers (5)

Answers (5)

steve_coombes
Participant
0 Kudos

Hello,

Oracle recommend against the use of 'sun' packages, although I wouldn't expect you to experience any problems.

http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

An alternative would be to use the Apache Commons Codec jar.

regards

Steve

Former Member
0 Kudos

Steve,

Thanks for the idea, do you have the import path and method details for the Apache version?

engswee
Active Contributor
0 Kudos

Steve is right, Oracle discourages the usage of the Sun library.

The Apache Commons codec can to be downloaded from the link below

Codec – Home

Other options for a Base64 encoder/decoder are below:-

steve_coombes
Participant
0 Kudos

Hello Aaron

An example of Base64-encoding a string using Commons Codec :

String usernamePassword = username + ":" + password;

byte[] usernamePasswordBytes = usernamePassword.getBytes();

byte[] encodedUsernamePasswordBytes = Base64.encodeBase64( usernamePasswordBytes );

String encodedUsernamePassword = new String( encodedUsernamePasswordBytes );

But I think javax.xml.bind.DatatypeConverter is a better suggestion. I'd use that as it requires less code and you wouldn't have to import an extra jar into PI.

regards

Steve

Former Member
0 Kudos

Thanks Eng!

When I tried with SAP's version it says:

When I tried with javax it says:


Doesn't it seem like PI should have this feature built into a library? I mean do we really need to download new software into the system for this?

engswee
Active Contributor
0 Kudos

Hi Aaron,

You don't need to download the library for the DatatypeConverter.

If you try copying the transform() method from my answer in the following thread.

And use it in a message mapping in ESR, entering all the necessary imports, including javax.xml.bind.*.

As you see below, the object check goes fine and it can be compiled.

Even if you do this in NWDS, the library is already part of the standard JRE system library.

I'm not sure what environment you are developing in, and where do you get the not exists error. Probably if you can provide some further details or screenshot, then we can try and resolve the mystery

Rgds

Eng Swee

PS: In the code you can also see how easily DatatypeConverter method can be used for parsing base64.

Former Member
0 Kudos

Thanks Eng. Apparently javax.xml.bind comes in a newer version of PI. We are planning an upgrade this year but it involves a lot of testing. We are still on 7.11, and still do all development inside the ESB.

engswee
Active Contributor
0 Kudos

Oh the nuances of the different versions of PI!!!

Well I know do know for a fact that SAP's base 64 library is available as of PI 7.11 SP05. I have downloaded a copy of com.sap.aii.utilxi.core.jar before using this trick from Praveen

Former Member
0 Kudos

Hi,

Can i use the same class for Decoding a base64 field ? does anyone have a UDF ready  ?

xavisanse
Active Participant
Former Member
0 Kudos

Hi Xavier,

Do you have a sample UDF for Decoding a base64 field ?

Former Member
0 Kudos

Hi Zevik,

Have you tried writing it very similar to the previously mentioned encoding UDF example? What error do you get?

Former Member
0 Kudos

Give it a try Zevik, you may surprise yourself!

Former Member
0 Kudos

I gave it a try, and I surprised myself with an error haa haa.

Anyone know how to make this sun decoder work? Input is a long base64 string.

import sun.misc.BASE64Decoder

public String b64decode(String myInput, Container container) throws StreamTransformationException{

BASE64Decoder decoder = new BASE64Decoder();

byte[] decoded = decoder.decodeBuffer(myInput);

String myOutput = new String(decoded, "UTF-8");

return myOutput;

}

I get error:

Function b64decode, Line 3:

unreported exception java.io.IOException;

must be caught or declared to be thrown byte[] decoded = decoder.decodeBuffer(myInput);

There is a caret/hat/^ pointing at string inside decodeBuffer(myInput)

Former Member
0 Kudos

Well, maybe answered my question but here is a solution which works for me:

BASE64Decoder decoder = new BASE64Decoder();

try {

byte[] decoded = decoder.decodeBuffer(myInput);

String myOutput = new String(decoded, "UTF-8");

return myOutput;

}

catch (IOException e) {

return e.toString();

}

xavisanse
Active Participant
0 Kudos

sun.misc.BASE64Encoder 

Former Member
0 Kudos

Hi Xavier,

I Couldn't find sun.misc.BASE64Encoder download, i read in the description that sun.misc.BASE64Decoder.jar allegedly contains the sun.misc.BASE64Encoder  class.

can you direct me to the sun.misc.BASE64Encoder  download ?

Former Member
0 Kudos

Hi Xavier,


Thank you very much, your answer has solved my problem.

Have a great day.

xavisanse
Active Participant
0 Kudos

You are welcome!

Former Member
0 Kudos

Hi,

Java mapping code in this wiki will help u:

http://wiki.sdn.sap.com/wiki/display/XI/How+to+Send+Binary+Data+to+RFC+from+XI(or)PI

Thanks

Amit Srivastava

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>Can anyone let me know any UDF which can be used for this.  What are the java classes that needs to be uploaded for this?

You need to write import statement for this class

com.sap.aii.utilxi.base64.api.Base64

and this class contains two static methods namely encode and decode.  Refer more about knowing this in the thread

http://scn.sap.com/thread/1079238