
Postman Sample
package sap.com;
import com.sap.aii.mapping.api.*;
import org.json.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class CalculateSignaturePost extends AbstractTransformation {
private List<String> array_nodes = new ArrayList<String>();
// Adding Dynamic Attributes
private static final DynamicConfigurationKey Sign = DynamicConfigurationKey
.create("http://sap.com/xi/XI/System/REST", "Signature");
private static final DynamicConfigurationKey TimeStamp = DynamicConfigurationKey
.create("http://sap.com/xi/XI/System/REST", "TimeStamp");
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
try {
// Getting Input Parameters - Secret Key, Host, Request Method, Method
String SecretKey = transformationInput.getInputParameters().getString("Secret_Key");
String Host = transformationInput.getInputParameters().getString("Host");
String Method = transformationInput.getInputParameters().getString("Method");
String RequestMethod = transformationInput.getInputParameters().getString("Request_Method");
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
String sourcexml = "";
String targetxml = "";
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
while ((line = br.readLine()) != null)
sourcexml += line + "\n";
br.close();
// Converts XML to JSON
JSONObject xmlJSONObj = XML.toJSONObject(sourcexml);
// Making the Node as Array and Converting Numbers to string
array_nodes.add("arraynode");
xmlJSONObj = handleJSONData(xmlJSONObj);
// Converts JSON to string
String jsonstring = xmlJSONObj.toString(0);
//Removing Namespace Tag
jsonstring = jsonstring
.replaceAll("\"" + "xmlns:ns0" + "\":" + "\"" + "your namespace" + "\"" + ",", "");
//Removing the outer element
jsonstring = jsonstring.replaceAll("\"" + "ns0:Message Type Name" + "\":", "");
int len = jsonstring.length();
len = len - 1;
jsonstring = jsonstring.substring(1, len);
// Encoding JSON Message string to base64
jsonstring = jsonstring.trim();
Base64 base64 = new Base64();
String jsonBase64 = new String(base64.encode(jsonstring.getBytes()));
// Getting Current TimeStamp
Long CurrentTimeStamp = new Date().getTime();
getTrace().addDebugMessage("TimeStamp = " + CurrentTimeStamp);
// Creating the string to calculate signature
String stringToSing = RequestMethod + "\n" + Host+ "\n" +Method+ "\n" + "id="
+ "&t="+CurrentTimeStamp+ "&ed=" + jsonBase64;
getTrace().addDebugMessage(stringToSing);
// Calculating signature using HMAC SHA256 algorithm
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(SecretKey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
// Encoding signature calculated to base64
String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(stringToSing.getBytes()));
getTrace().addDebugMessage("Signature = " + hash);
// Add Signature & Timestamp as dynamic attributes
DynamicConfiguration conf = transformationInput.getDynamicConfiguration();
conf.put(Sign, hash);
conf.put(TimeStamp, String.valueOf(CurrentTimeStamp));
targetxml = jsonstring.trim();
transformationOutput.getOutputPayload().getOutputStream().write(targetxml.getBytes("UTF-8"));
} catch (Exception exception) {
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
public JSONObject handleJSONData(JSONObject jsonObj) {
/*
* Parse the JSON Structure to Delete a record or convert it to an array
* Input: JSONObject -> Json Sub structure to be updated Output:
* JSONObject -> Updated Json Sub structure with deleted records and
* arrays.
*/
try {
// Create an array of keyset to loop further
String arr[] = new String[jsonObj.keySet().size()];
int k = 0;
for (String key : jsonObj.keySet())
arr[k++] = key;
// Loop through all the keys in a JSONObject
for (String key : arr) {
// If there are records to be converted to Array, convert it.
if (array_nodes.contains(key)) {
jsonObj = forceToJSONArray(jsonObj, key);
}
// If the sub node is a JSONArray or JSONObject, step inside the
// Object
if (jsonObj.get(key) instanceof JSONArray) {
JSONArray sjao = jsonObj.getJSONArray(key);
for (int i = 0; i < sjao.length(); i++) {
sjao.put(i, handleJSONData(sjao.getJSONObject(i)));
}
jsonObj.put(key, sjao);
} else if (jsonObj.get(key) instanceof JSONObject) {
jsonObj.put(key, handleJSONData(jsonObj.getJSONObject(key)));
} else {
// Convert number to String
Object val = jsonObj.get(key);
if (val instanceof Integer || val instanceof Float || val instanceof Double || val instanceof Long
|| val instanceof Short)
jsonObj.put(key, jsonObj.get(key).toString());
}
}
} catch (Exception e) {
// Handle all exceptions
if (getTrace() != null) {
getTrace().addDebugMessage("Exception while Updating Payload: ", e);
} else
e.printStackTrace();
}
return jsonObj;
}
public static JSONObject forceToJSONArray(JSONObject jsonObj, String key) throws org.json.JSONException {
/*
* Force Convert a record to JSON Array Input: 1) JSONObject -> JSON Sub
* structure to be updated 2) key -> Key whose value is to be converted
* to JSONArray Output: JSONObject -> Updated Json Sub structure with
* deleted records and arrays.
*/
// Get the key value from JSONObject using opt() and not get(), as it
// can also return null value.
Object obj = jsonObj.opt(key);
// If the obj doesn't exist inside my the JsonObject structure, create
// it empty
if (obj == null) {
jsonObj.put(key, new JSONArray());
}
// if exist but is a JSONObject, force it to JSONArray
else if (obj instanceof JSONObject) {
JSONArray jsonArray = new JSONArray();
jsonArray.put((JSONObject) obj);
jsonObj.put(key, jsonArray);
}
// if exist but is a primitive entry, force it to a "primitive"
// JSONArray
else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceof Double
|| obj instanceof Long || obj instanceof Boolean) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj);
jsonObj.put(key, jsonArray);
}
return jsonObj;
}
}
Java Class in Operation Mapping and Binding
Binding Parameters
Setting Parameters in Integration Configuration
Passing Dynamic Headers in Communication Channel
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
15 | |
11 | |
7 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |