cancel
Showing results for 
Search instead for 
Did you mean: 

Java Code for Base 64 Encoding

Former Member
0 Kudos

Hi Folks,

I have an IDOC to Soap Scenario, where the IDOC data will be mapped to an intermediate structure and then the entire output XML will be sent as a single field value in one of the fields in Soap Body. and this XML needs to be in Base 64 format. So I am looking for Java code which converts the entire XML into base 64 format. Any help in this regard is highly appreciated.

Thanks,

Ramesh

Accepted Solutions (1)

Accepted Solutions (1)

former_member184720
Active Contributor
0 Kudos

Hi Ramesh - Below blog should be helpful

http://wiki.scn.sap.com/wiki/display/XI/How+to+Send+Binary+Data+to+RFC+from+XI(or)PI?original_fqdn=w...

check for Aaron's reply in the below thread for a UDF code.

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

Former Member
0 Kudos

To be more clear on my requirement, I am already using a java mapping to get the SOAP envelope format and also the XML data from Message mapping (intermediate structure) is read character by character in the java mapping with some additional custom details added to it.

Now all this data will be fed to one of the fields in the SOAP Body..

The SOAP body has 10 more fields apart from the XML Data field, and those fields are hard coded.

former_member184720
Active Contributor
0 Kudos

Hi Ramesh - Inside that java mapping, read the required bytes into a string something like myInput and assign the comverted string to desired field.

     BASE64Encoder encoder = new BASE64Encoder();

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

Former Member
0 Kudos

Hello,

>>The SOAP body has 10 more fields apart from the XML Data field, and those fields are hard coded.

I don't see any problem in that, in ur java mapping code which is actually creating target in soap envelope format u can easily read output of first message mapping convert it into base64 format (as shown below) and add it inside soap body with other hardcoded fields.

Thanks

Amit Srivastava

Former Member
0 Kudos

Hi Hareesh,

Part of my Jave code is shown below :

ediFilename,ediFile,binFile are 3 fields in the SOAP Body and the Actual Base64 conversion is needed for ediFile field.

whatever data coming in ediFile field needs to be converted into Base64 format.

Presently I am getting the proper output without base64 format, using the below code.

Code :

buffer.append( "<dig:ediFileName>");
  buffer.append( "vabcf" );
  buffer.append( "</dig:ediFileName>");

  buffer.append( "<dig:ediFile>");

  //Get header string and escape chars
  buffer.append("&lt;?xml version=\"1.0\" encoding=\"ISO-8859-1\"?&gt;");
  String headerStr = INVOICE_HEADER ;
  headerStr = headerStr.replaceAll("<", "&lt;");
  headerStr = headerStr.replaceAll(">", "&gt;");

  buffer.append(headerStr );

  String strAfterTag = payloadStr.substring( startIndex );
  // Adding invoice header so that that portions a


  // Remove name spaces from tags


  // Remove dig CreditNote from end tag
  strAfterTag = strAfterTag.replaceAll("ns0:Invoice", "Invoice:Invoice");

  //Replace name space prefixes..


  strAfterTag = strAfterTag.replaceAll("ns0:", "dig:");
  strAfterTag = strAfterTag.replaceAll("ns2:", "cbc:");
  strAfterTag = strAfterTag.replaceAll("ns3:", "cac:");
 
  //Replace Special characters
  strAfterTag = strAfterTag.replaceAll("&", "&amp;");
  strAfterTag = strAfterTag.replaceAll("<", "&lt;");
  strAfterTag = strAfterTag.replaceAll(">", "&gt;");

  buffer.append( strAfterTag );

  // Web Service Header Ending Portion
  buffer.append( "</dig:ediFile>");

  //return (String)



  buffer.append( "<dig:binFileName>");
  buffer.append( "per auras" );
  buffer.append( "</dig:binFileName>");
 

former_member184720
Active Contributor
0 Kudos

buffer.append( strAfterTag );

Can you try replace it with below lines

include sun.misc.BASE64Encoder- include stmnt

BASE64Encoder encoder = new BASE64Encoder(); 

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

buffer.append(encoded);

Former Member
0 Kudos

Hi Hareesh,

I have included sun.misc.BASE64Encoder  and the code is also pasted. but it is giving an error on encode method.

Do we need to add anything to the encode method... please let me know.

 

former_member184720
Active Contributor
0 Kudos

Hi Ramesh - Hope you have added the required jar file and your import statement looks fine.

If so may be you can try something like below..

String encoded = new sun.misc.BASE64Encoder().encode(strAfterTag.getBytes());

buffer.append(encoded);

Former Member
0 Kudos

Hi Hareesh,

Could you please send me the jar file needed for this code or please let me know where can I download it from.

former_member184720
Active Contributor
0 Kudos

Hi Ramesh - It comes along with the JRE.

You can download the jdk1.5.0_06 from below link and you'll be able find the jar under \jre\lib.

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase5-41941...

Former Member
0 Kudos

Hi Hareesh,

Thanks for your prompt response.

I am writing the Java code in Eclipse tool. So can you please tell me the name of the jar file which has to be used for this code.

former_member184720
Active Contributor
0 Kudos

rt.jar

former_member191435
Contributor
0 Kudos

Hi Hareesh,,

We have added below code

String encoded = new sun.misc.BASE64Encoder().encode(strAfterTag.getBytes());

            buffer.append(encoded);

and we have used import sun.misc.BASE64Encoder.*; in javamapping

But in the code it is showing an error as  The Constructor BASE64Encoder().is not accesable due to restriction on the required library c:\java\jre6\lib\rt.jar

But we already have rt.jar file in the eclipse package...

former_member184720
Active Contributor
0 Kudos

You can find the fix @

http://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-got-error-in-eclipse

Go to Window-->Preferences-->Java-->Compiler-->Error/Warnings.
Select Deprecated and Restricted API. Change it to warning.
Change forbidden and Discouraged Reference and change it to warning. (or as your need.)

http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required...

former_member191435
Contributor
0 Kudos

Thanks for the help..

Can you please let me know what does this mean...

We will test the code and if we find any errors will get back to you...

Answers (3)

Answers (3)

rhviana
Active Contributor
0 Kudos

Ramesh, my java code helps ?

How can I help ?

former_member191435
Contributor
0 Kudos

Hi Ricardo,

we are getting below error while using in UDF

                   Check Result for Object MM_File_Test

 

Source text of object Message Mapping: MM_File_Test | urn errors:  
rhviana
Active Contributor
0 Kudos

Hi Ramesh,

You must use java mapping or UDF and into java code use:


public class Base64EncodingExample{

    public static void main(String args[]) throws IOException {

        String orig = "original String before base64 encoding in Java";

        //encoding  byte array into base 64

        byte[] encoded = Base64.encodeBase64(orig.getBytes());    

     

        System.out.println("Original String: " + orig );

        System.out.println("Base64 Encoded String : " + new String(encoded));

     

        //decoding byte array into base64

        byte[] decoded = Base64.decodeBase64(encoded);     

        System.out.println("Base 64 Decoded  String : " + new String(decoded));

    }

}K

ind regatrds,

Kind regards,

BR.

Ricardo Viana

Former Member
0 Kudos

Hello,

I would suggest you to use java mapping  (just after message mapping in OM)and convert intermediate XML into base64:

Adjust below code as per ur structure


import java.io.*;

import java.util.*;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.DynamicConfiguration;

import com.sap.aii.mapping.api.DynamicConfigurationKey;

import com.sap.aii.mapping.api.StreamTransformationConstants;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

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

import java.util.Map;

public class Base64Conversion extends AbstractTransformation {

    ;

    String StartMessage="";

    String EndMessage="";

    String TargetMessage ="";

   

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)

    throws StreamTransformationException

    {

        //Converting Transformation Input to Input Stream       

        InputStream inputstream = transformationInput.getInputPayload().getInputStream();

        OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

        //Defining Byte Array Output Stream

        byte[] buffer = new byte[1024*5000];

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int len;

           

        //Creating Target Message

        StartMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:MT xmlns:ns0=\"http://test\">";

        EndMessage = "</ns0:MT>";

        try

        {

            while ((len = inputstream.read(buffer)) > 0)

                baos.write(buffer, 0, len);

            String encode = Base64.encode(baos.toByteArray());   

            TargetMessage = StartMessage + "<Base64> " + encode + "</Base64>"  + EndMessage;

            outputstream.write(TargetMessage.getBytes());

        }

        catch(Exception exception)

        {

            throw new StreamTransformationException(exception.toString());

        }

    }

}

Thanks

Amit Srivastava