cancel
Showing results for 
Search instead for 
Did you mean: 

calculating Checksum and passing it as parameter to File URL

ChandraMahajan
Active Contributor
0 Kudos

Hi,

We are having Idoc to File scenario. Sender system is creating XML file based on Idoc and passing this file to legacy application.

our requirement is to calculate the checksum of the file and send it via file url to legacy application. On reciever application end, checksum is recalculated for the recieved file to ensure data intigraty.

Is there any way to calculate checksum for the file and pass it thru URL?

Please let me know.

Thanks,

Chandra

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

package com.test.example;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import java.security.MessageDigest;

public final class CreateChecksum implements StreamTransformation {

  private Map _param;     

  public void setParameter(Map param) {
    _param = param;
  }

  public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

    try {     
      byte[] buf = new byte[1024];
      MessageDigest md = MessageDigest.getInstance("MD5");
      int read;
      do {
        read = in.read(buf);
        if (read > 0) {
          md.update(buf, 0, read); 
          out.write(buf, 0, read);
        }
      } while (read != -1);

      byte[] b = md.digest();
      String checksum = "";
      for (int i = 0; i < b.length; i++) {
        checksum += Integer.toString((b<i> & 0xff) + 0x100, 16).substring(1);
      }
      
      DynamicConfiguration conf = (DynamicConfiguration)_param.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);  
      
      DynamicConfigurationKey urlParamOne = DynamicConfigurationKey.create( "http://sap.com/xi/XI/System/HTTP", "UrlParamOne");
      conf.put(urlParamOne, checksum);
      
      DynamicConfigurationKey fileName = DynamicConfigurationKey.create( "http://sap.com/xi/XI/System/File", "FileName");
      conf.put(fileName, checksum);
      
    } catch (Exception e) {              
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      e.printStackTrace(pw);
      throw new StreamTransformationException(sw.toString());  
    }
  }    
}

Edited by: Russell Darr on Jun 26, 2009 11:02 PM

siddhesh_pathak4
Contributor
0 Kudos

Hello Russ,

In this case, can explain us how the checkSum is getting calculated... like which payload it will take as input input payload or the final out. Like i have the scenario IDOC to file. Which payload it will take as file to calculate the checkSum from java mapping, iDOC or file payload.

It would be gr8 help.

Thanks alot for your help in advance.

Thanks,

Sidd.

Former Member
0 Kudos

Hi Sidd,

It depends on where in your interface mapping you place the CreateChecksum map. If you want to create a MD5 checksum for the file payload you will need to put this map as the last message mapping in your interface mapping. It just passes the payload through (unaltered) and in the process calculates the checksum. You can then verify the checksum using a number of utilities depending on what you have available.

For example:

md5sum file_payload.xml openssl md5 < file_payload.xml

-Russ

Former Member
0 Kudos

Hi Chandra,

Add the java message mapping below (next post) to an Interface Mapping. If you already

have a Message Mapping in the Interface Mapping, then add it as the second step.

This java map is a pass-through in terms of the payload, but in the process it

will calculate the md5 checksum and add it as a dynamic variable. Once you have

the checksum in a dynamic variable you can use it in a receiver adapter. In my

sample code I'm setting the FileName (which can be used by the file

adapter) and UrlParamOne (which can be used by the HTTP adapter). There are many

blogs about how to setup and use these Adapter-Specific Message Attributes.

In the XI XML Montior you can see the values set by looking at the DynamicConfiguration

SOAP Header after the map is executed. You should see something like this:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SAP:DynamicConfiguration xmlns:SAP="http://sap.com/xi/XI/Message/30" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" SOAP:mustUnderstand="1">
  <SAP:Record namespace="http://sap.com/xi/XI/System/HTTP" name="UrlParamOne">26cb954895c28ebc73dc5c289e44d559</SAP:Record>
  <SAP:Record namespace="http://sap.com/xi/XI/System/File" name="FileName">26cb954895c28ebc73dc5c289e44d559</SAP:Record>
</SAP:DynamicConfiguration>

Thanks,

-Russ