Files
ServiceDashBored/Endpoint/SoapEndpoint.cs
T
2025-10-11 09:44:08 -04:00

96 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Xml;
namespace Endpoint
{
public class SoapEndpoint : HttpEndpoint
{
private Dictionary<string, string> _namespaceToUri;
/// <summary>
/// Gets the SOAP endpoint configuration.
/// </summary>
/// <value>The SOAP endpoint configuration.</value>
public SoapEndpointConfiguration SoapEndpointConfiguration
{
get { return (SoapEndpointConfiguration)_httpEndpointConfiguration; }
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpEndpoint"/> class.
/// </summary>
/// <param name="config">The SoapEndpointConfiguration.</param>
public SoapEndpoint(SoapEndpointConfiguration config)
: base(config)
{
if (!string.IsNullOrEmpty(SoapEndpointConfiguration.XpathNamespaces))
{
string[] split = SoapEndpointConfiguration.XpathNamespaces.Split(',');
if (split.Length != 2)
throw new ArgumentException("Need a comma separated pair in XpathNamespaces.", "config");
_namespaceToUri = new Dictionary<string, string>();
_namespaceToUri.Add(split[0], split[1]);
}
}
/// <summary>
/// Gets the current status result of the endpoint
/// </summary>
/// <returns>Status</returns>
public override Status GetStatus()
{
Status baseStatus = base.GetStatus();
if (baseStatus != Status.Up)
return baseStatus;
try
{
Dictionary<string, string> headers = new Dictionary<string, string>
{{"SOAPAction", SoapEndpointConfiguration.SoapAction}};
XmlDocument xml = new XmlDocument();
xml.LoadXml(GetUrlContent(SoapEndpointConfiguration.Uri, "text/xml; charset=utf-8", SoapEndpointConfiguration.SoapRequest, headers));
XmlNodeList nodes;
if (_namespaceToUri != null)
{
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xml.NameTable);
foreach (KeyValuePair<string, string> namespaceToUri in _namespaceToUri)
{
nsMgr.AddNamespace(namespaceToUri.Key, namespaceToUri.Value);
}
nodes = xml.SelectNodes(SoapEndpointConfiguration.XpathQuery, nsMgr);
}
else
nodes = xml.SelectNodes(SoapEndpointConfiguration.XpathQuery);
// xml.Save(@"c:\soap.xml");
// verify response has expected value
if (nodes == null || nodes.Count == 0)
{
StatusDescription = "Couldn't find expected value in SOAP response";
return Status.Error;
}
string value = nodes[0].Value.Trim();
if (value != SoapEndpointConfiguration.ExpectedXpathResult)
{
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
SoapEndpointConfiguration.ExpectedXpathResult);
return Status.Error;
}
}
catch (Exception ex)
{
StatusDescription = ex.Message;
return Status.Error;
}
return Status.Up;
}
}
}