using System; using System.Collections.Generic; using System.Xml; namespace Endpoint { public class SoapEndpoint : HttpEndpoint { private Dictionary _namespaceToUri; /// /// Gets the SOAP endpoint configuration. /// /// The SOAP endpoint configuration. public SoapEndpointConfiguration SoapEndpointConfiguration { get { return (SoapEndpointConfiguration)_httpEndpointConfiguration; } } /// /// Initializes a new instance of the class. /// /// The SoapEndpointConfiguration. 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(); _namespaceToUri.Add(split[0], split[1]); } } /// /// Gets the current status result of the endpoint /// /// Status public override Status GetStatus() { Status baseStatus = base.GetStatus(); if (baseStatus != Status.Up) return baseStatus; try { Dictionary headers = new Dictionary {{"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 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; } } }