Handle dynamic HTTP 302 redirects
// This script looks to see if the response code was 302
// if so, it resets the target of the next message
// to use the host name and service path specified by the Location header
// Get the Message that immediately precedes this Script.
var msg = $context.currentItem.previousItem;
var origText = msg.getResponse(msg.RESPONSE_TEXT);
//from the response, determine if this a 302 response
var pos = origText.indexOf("HTTP/1.1 302 Found");
if (pos == -1)
{
$context.result.postMessage($context.result.LEVEL_INFO, "did not find 302");
}
else
{
//now get the value of the location header and then the protocol following
var firstPos = origText.indexOf("Location: ");
firstPos = origText.indexOf("http", firstPos + 1);
var lastPos = origText.indexOf("\n", firstPos + 1);
var location = origText.substring(firstPos, lastPos);
//now we need to separate this into host name and service path
var doubleSlashPos = location.indexOf("//");
firstSlashPos = location.indexOf("/", doubleSlashPos + 2);
//extract out the host name and the service path from the location
var hostName = location.substring(doubleSlashPos + 2, firstSlashPos);
var servicePath = location.substring(firstSlashPos);
//set the host name and service path for the target of the next message
$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HostName", hostName);
$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("ServicePath", servicePath);
}