Steps Followed:
1. We have routed the requests to two different URL's using excel file/text file.
2. The ID is provided in URI path, validation is based on the same ID. If the ID exists in excel/text file then the request is forwarded to URL1 and if does not exist in excel/text file then it should be sent to URL2.
Issue observed in above steps:
Basic Auth header is not passed along with the requests.
Script In Use:
using HP.SV.CSharp;
using System.Collections.Generic;
using System;
using System.Text;
using System.Net.Http;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using HP.SV.DotNetRuleApi;
namespace HP.SV
{
public class CSharpRule
{
private static string ID1;
private static string ExcelFilePath = @"C:/VirtualRouter.xlsx";
private static string ExcelSheetName = "Sheet1";
private static string TextFilePath = @"C:/VirtualRouter.txt";
private static string result ;
private static string delimiter = "|";
private static string searchInput = result;
private static string HttpheaderRESTAction = "RESTAction";
private static string HttpHeaderContentType = "Content-Type";
private static string HttpHeaderContentEncoding = "Content-Encoding";
/// <summary>
/// Main method.
/// </summary>;
public static void Execute(HpsvRootObject sv)
{
ID1 = sv.Request.HTTPInputParameters.UriPath;
//sv.MessageLogger.Warn(ID1);
Dictionary<string, string> httpHeaders = GetHttpHeaders(sv.Request);
byte[] payload = Convert.FromBase64String(sv.Request.BinaryContent.Data);
string targeturl = GetTargetUrl(httpHeaders, payload);
// log target URL for debugging purposes
sv.MessageLogger.Warn(targeturl);
HttpResponseMessage httpResponse = DoHttpPost(targeturl, httpHeaders, payload);
CopyToHpsvResponse(httpResponse, sv.Response);
string Uripath = sv.GetOrCreate(x => x.Request.HTTPInputParameters).UriPath.ToString();
}
/// <summary>
/// Copies data from the .NET HttpResponse structure to SV structure.
/// </summary>
public static void CopyToHpsvResponse(HttpResponseMessage httpResponse, HpsvResponse hpsvResponse)
{
hpsvResponse.HTTPOutputParameters.StatusCode = (long)httpResponse.StatusCode;
hpsvResponse.BinaryContent.Data = Convert.ToBase64String(httpResponse.Content.ReadAsByteArrayAsync().Result);
hpsvResponse.HTTPOutputParameters.Headers.Content_Type = httpResponse.Content.Headers.ContentType.ToString();
}
/// <summary>
/// Returns URL where we want to forward the request.
/// </summary>
public static string GetTargetUrl(Dictionary<string, string> headers, byte[] payload)
{
// load Excel data
Dictionary<string, string> idToUrlMap = ReadExcelData(ExcelFilePath, ExcelSheetName);
result = ID1;// this value is taken uri path
string target_endpoint_url;
idToUrlMap.TryGetValue(result, out target_endpoint_url);
if (target_endpoint_url == null)
{
bool found = false;
try
{
string[] lines = File.ReadAllLines(TextFilePath);
foreach (string line in lines)
{
string[] parts = line.Split(new string[] { delimiter },StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
string substringBeforeDelimiter = parts[0].Trim();
string substringAfterDelimiter = parts[1].Trim();
if (substringBeforeDelimiter == result)
{
target_endpoint_url = substringAfterDelimiter;
found = true;
break;
}
}
}
if (!found)
{
target_endpoint_url = "">httP://URL/customers/"+ID1;
}
}
catch (Exception e)
{
// sv.MessageLogger.Warn("Error occurred while reading from text file: " + e.Message);
target_endpoint_url = "";">httP://URL/customers/"+ID1;";
}
}
return target_endpoint_url;
}
/// <summary>
/// Extracts HTTP headers from SV request structure.
/// </summary>
private static Dictionary<string, string> GetHttpHeaders(HpsvRequest httpRequest)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (KeyValuePair<string, object> header in httpRequest.HTTPInputParameters.Headers.GetAllProperties())
{
headers.Add(header.Key, (string)header.Value);
}
return headers;
}
/// <summary>
/// Invokes the remote service and returns the response.
/// </summary>
private static HttpResponseMessage DoHttpPost(string requestUri, Dictionary<string, string> httpHeaders, byte[] payload)
{
HttpContent httpContent = CreateHttpContent(httpHeaders, payload);
HttpClientHandler handler = new HttpClientHandler();
string token = "your_basic_token"; // Replace with your actual token
string basicAuthHeader = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(token));
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
(httpRequestMessage, cert, cetChain, policyErrors) =>
{
return true;
};
HttpClient client = new HttpClient(handler,true);
client.DefaultRequestHeaders.Add("Authorization",basicAuthHeader);
HttpResponseMessage response = client.PostAsync(requestUri, httpContent).Result;
return response;
}
/// <summary>
/// Creates the .NET request object.
/// </summary>
private static HttpContent CreateHttpContent(Dictionary<string, string> headers, byte[] payload) {
HttpContent httpContent = new ByteArrayContent(payload);
foreach (KeyValuePair<string, string> header in headers)
{
if (header.Key == HttpHeaderContentType)
{
httpContent.Headers.ContentType.MediaType = header.Value;
}
else if (header.Key == HttpHeaderContentEncoding)
{
httpContent.Headers.ContentEncoding.Add(header.Value);
}
else
{
httpContent.Headers.Add(header.Key, header.Value);
}
}
return httpContent;
}
/// <summary>
/// Reads data from Excel.
/// </summary>
private static Dictionary<string, string> ReadExcelData(string excelFilePath, string SheetName) {
// read
IWorkbook workbook = ReadWorkbook(excelFilePath);
// get value
ISheet sheet = workbook.GetSheet(SheetName);
Dictionary<string, string> idToUrlMap = new Dictionary<string, string>();
bool next = true;
for (int i = 0; next; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
string ID1 = row.GetCell(0)?.ToString();
string target_endpoint_url = row.GetCell(1)?.ToString();
if( !idToUrlMap.ContainsKey(ID1))
{
idToUrlMap.Add(ID1, target_endpoint_url);
}
next = !string.IsNullOrEmpty(ID1);
}
else
{
next = false;
}
}
return idToUrlMap;
}
private static IWorkbook ReadWorkbook(string file)
{
IWorkbook workBook;
using (var fs = File.OpenRead(file))
{
workBook = new XSSFWorkbook(fs);
}
return workBook;
}
}
}