Skip to content

Commit

Permalink
Create select_named_headers_from_sourceMap.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmano1 authored Jun 22, 2024
1 parent 8c6abc7 commit d92acf1
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions select_named_headers_from_sourceMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Creates a LinkedHashMap from provided headers and header keys.
* This function is based on original work by Tony Germano (@agermano) from Mirth Slack.
*
* @param {Object} headers - The headers object containing header lists.
* @param {Array} headerKeys - An array of header keys to be included in the map.
* @param {boolean} excludeNullValues - Flag to determine whether to exclude null values.
* @returns {java.util.LinkedHashMap} - A LinkedHashMap with header keys and their corresponding last values.
*/
function createHeaderMap(headers, headerKeys, excludeNullValues) {
// Initialize a new LinkedHashMap
const headerMap = new java.util.LinkedHashMap();

// Function to get the last value of a header key
const keyToEntryWithLastValue = key => {
const headerList = headers.getHeaderList(key);
const value = headerList && headerList.get(headerList.size() - 1);
return [key, value];
}

// Populate the headerMap based on the excludeNullValues flag
if (excludeNullValues == true) {
headerKeys.map(keyToEntryWithLastValue)
.filter(entry => entry[1] != null)
.forEach(entry => headerMap.put.apply(headerMap, entry));
} else {
headerKeys.map(keyToEntryWithLastValue)
.forEach(entry => headerMap.put.apply(headerMap, entry));
}

// Return the populated LinkedHashMap
return headerMap;
}

// Example usage:
const headers = $s('headers');
const headerKeys = ["host", "content-type", "date", "Authorization", "content-md5", "Accept"];
const headerMap = createHeaderMap(headers, headerKeys, false);
$co('headerMap',headerMap)
// In your sender, selct "Use Map" and type in "headerMap" (no double quotes, just the word)

0 comments on commit d92acf1

Please sign in to comment.