ReliableSessionBindingElementImporter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Runtime;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Description;
  11. using System.Xml;
  12. static class ReliableSessionPolicyStrings
  13. {
  14. public const string AcknowledgementInterval = "AcknowledgementInterval";
  15. public const string AtLeastOnce = "AtLeastOnce";
  16. public const string AtMostOnce = "AtMostOnce";
  17. public const string BaseRetransmissionInterval = "BaseRetransmissionInterval";
  18. public const string DeliveryAssurance = "DeliveryAssurance";
  19. public const string ExactlyOnce = "ExactlyOnce";
  20. public const string ExponentialBackoff = "ExponentialBackoff";
  21. public const string InactivityTimeout = "InactivityTimeout";
  22. public const string InOrder = "InOrder";
  23. public const string Milliseconds = "Milliseconds";
  24. public const string NET11Namespace = "http://schemas.microsoft.com/ws-rx/wsrmp/200702";
  25. public const string NET11Prefix = "netrmp";
  26. public const string ReliableSessionName = "RMAssertion";
  27. public const string ReliableSessionFebruary2005Namespace = "http://schemas.xmlsoap.org/ws/2005/02/rm/policy";
  28. public const string ReliableSessionFebruary2005Prefix = "wsrm";
  29. public const string ReliableSession11Namespace = "http://docs.oasis-open.org/ws-rx/wsrmp/200702";
  30. public const string ReliableSession11Prefix = "wsrmp";
  31. public const string SequenceSTR = "SequenceSTR";
  32. public const string SequenceTransportSecurity = "SequenceTransportSecurity";
  33. }
  34. public sealed class ReliableSessionBindingElementImporter : IPolicyImportExtension
  35. {
  36. void IPolicyImportExtension.ImportPolicy(MetadataImporter importer, PolicyConversionContext context)
  37. {
  38. if (importer == null)
  39. {
  40. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("importer");
  41. }
  42. if (context == null)
  43. {
  44. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  45. }
  46. bool gotAssertion = false;
  47. XmlElement reliableSessionAssertion = PolicyConversionContext.FindAssertion(context.GetBindingAssertions(),
  48. ReliableSessionPolicyStrings.ReliableSessionName,
  49. ReliableSessionPolicyStrings.ReliableSessionFebruary2005Namespace, true);
  50. if (reliableSessionAssertion != null)
  51. {
  52. ProcessReliableSessionFeb2005Assertion(reliableSessionAssertion, GetReliableSessionBindingElement(context));
  53. gotAssertion = true;
  54. }
  55. reliableSessionAssertion = PolicyConversionContext.FindAssertion(context.GetBindingAssertions(),
  56. ReliableSessionPolicyStrings.ReliableSessionName,
  57. ReliableSessionPolicyStrings.ReliableSession11Namespace, true);
  58. if (reliableSessionAssertion != null)
  59. {
  60. if (gotAssertion)
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(
  63. SR.GetString(SR.MultipleVersionsFoundInPolicy,
  64. ReliableSessionPolicyStrings.ReliableSessionName)));
  65. }
  66. ProcessReliableSession11Assertion(importer, reliableSessionAssertion,
  67. GetReliableSessionBindingElement(context));
  68. }
  69. }
  70. static ReliableSessionBindingElement GetReliableSessionBindingElement(PolicyConversionContext context)
  71. {
  72. ReliableSessionBindingElement settings = context.BindingElements.Find<ReliableSessionBindingElement>();
  73. if (settings == null)
  74. {
  75. settings = new ReliableSessionBindingElement();
  76. context.BindingElements.Add(settings);
  77. }
  78. return settings;
  79. }
  80. static bool Is11Assertion(XmlNode node, string assertion)
  81. {
  82. return IsElement(node, ReliableSessionPolicyStrings.NET11Namespace, assertion);
  83. }
  84. static bool IsElement(XmlNode node, string ns, string assertion)
  85. {
  86. if (assertion == null)
  87. {
  88. throw Fx.AssertAndThrow("Argument assertion cannot be null.");
  89. }
  90. return ((node != null)
  91. && (node.NodeType == XmlNodeType.Element)
  92. && (node.NamespaceURI == ns)
  93. && (node.LocalName == assertion));
  94. }
  95. static bool IsFeb2005Assertion(XmlNode node, string assertion)
  96. {
  97. return IsElement(node, ReliableSessionPolicyStrings.ReliableSessionFebruary2005Namespace, assertion);
  98. }
  99. static void ProcessReliableSession11Assertion(MetadataImporter importer, XmlElement element,
  100. ReliableSessionBindingElement settings)
  101. {
  102. // Version
  103. settings.ReliableMessagingVersion = ReliableMessagingVersion.WSReliableMessaging11;
  104. IEnumerator assertionChildren = element.ChildNodes.GetEnumerator();
  105. XmlNode currentNode = SkipToNode(assertionChildren);
  106. // Policy
  107. ProcessWsrm11Policy(importer, currentNode, settings);
  108. currentNode = SkipToNode(assertionChildren);
  109. // Looking for:
  110. // InactivityTimeout, AcknowledgementInterval
  111. // InactivityTimeout
  112. // AcknowledgementInterval
  113. // or nothing at all.
  114. State state = State.InactivityTimeout;
  115. while (currentNode != null)
  116. {
  117. if (state == State.InactivityTimeout)
  118. {
  119. // InactivityTimeout assertion
  120. if (Is11Assertion(currentNode, ReliableSessionPolicyStrings.InactivityTimeout))
  121. {
  122. SetInactivityTimeout(settings, ReadMillisecondsAttribute(currentNode, true), currentNode.LocalName);
  123. state = State.AcknowledgementInterval;
  124. currentNode = SkipToNode(assertionChildren);
  125. continue;
  126. }
  127. }
  128. // AcknowledgementInterval assertion
  129. if (Is11Assertion(currentNode, ReliableSessionPolicyStrings.AcknowledgementInterval))
  130. {
  131. SetAcknowledgementInterval(settings, ReadMillisecondsAttribute(currentNode, true), currentNode.LocalName);
  132. // ignore the rest
  133. break;
  134. }
  135. if (state == State.AcknowledgementInterval)
  136. {
  137. // ignore the rest
  138. break;
  139. }
  140. currentNode = SkipToNode(assertionChildren);
  141. }
  142. // Schema allows arbitrary elements from now on, ignore everything else
  143. }
  144. static void ProcessReliableSessionFeb2005Assertion(XmlElement element, ReliableSessionBindingElement settings)
  145. {
  146. // Version
  147. settings.ReliableMessagingVersion = ReliableMessagingVersion.WSReliableMessagingFebruary2005;
  148. IEnumerator nodes = element.ChildNodes.GetEnumerator();
  149. XmlNode currentNode = SkipToNode(nodes);
  150. // InactivityTimeout assertion
  151. if (IsFeb2005Assertion(currentNode, ReliableSessionPolicyStrings.InactivityTimeout))
  152. {
  153. SetInactivityTimeout(settings, ReadMillisecondsAttribute(currentNode, true), currentNode.LocalName);
  154. currentNode = SkipToNode(nodes);
  155. }
  156. // BaseRetransmissionInterval assertion is read but ignored
  157. if (IsFeb2005Assertion(currentNode, ReliableSessionPolicyStrings.BaseRetransmissionInterval))
  158. {
  159. ReadMillisecondsAttribute(currentNode, false);
  160. currentNode = SkipToNode(nodes);
  161. }
  162. // ExponentialBackoff assertion is read but ignored
  163. if (IsFeb2005Assertion(currentNode, ReliableSessionPolicyStrings.ExponentialBackoff))
  164. {
  165. currentNode = SkipToNode(nodes);
  166. }
  167. // AcknowledgementInterval assertion
  168. if (IsFeb2005Assertion(currentNode, ReliableSessionPolicyStrings.AcknowledgementInterval))
  169. {
  170. SetAcknowledgementInterval(settings, ReadMillisecondsAttribute(currentNode, true), currentNode.LocalName);
  171. }
  172. // Schema allows arbitrary elements from now on, ignore everything else
  173. }
  174. static void ProcessWsrm11Policy(MetadataImporter importer, XmlNode node, ReliableSessionBindingElement settings)
  175. {
  176. XmlElement element = ThrowIfNotPolicyElement(node, ReliableMessagingVersion.WSReliableMessaging11);
  177. IEnumerable<IEnumerable<XmlElement>> alternatives = importer.NormalizePolicy(new XmlElement[] { element });
  178. List<Wsrm11PolicyAlternative> wsrmAlternatives = new List<Wsrm11PolicyAlternative>();
  179. foreach (IEnumerable<XmlElement> alternative in alternatives)
  180. {
  181. Wsrm11PolicyAlternative wsrm11Policy = Wsrm11PolicyAlternative.ImportAlternative(importer, alternative);
  182. wsrmAlternatives.Add(wsrm11Policy);
  183. }
  184. if (wsrmAlternatives.Count == 0)
  185. {
  186. // No specific policy other than turn on WS-RM.
  187. return;
  188. }
  189. foreach (Wsrm11PolicyAlternative wsrmAlternative in wsrmAlternatives)
  190. {
  191. // The only policy setting that affects the binding is the InOrder assurance.
  192. // Even that setting does not affect the binding since InOrder is a server delivery assurance.
  193. // Transfer any that is valid.
  194. if (wsrmAlternative.HasValidPolicy)
  195. {
  196. wsrmAlternative.TransferSettings(settings);
  197. return;
  198. }
  199. }
  200. // Found only invalid policy.
  201. // This throws an exception about security since that is the only invalid policy we have.
  202. Wsrm11PolicyAlternative.ThrowInvalidBindingException();
  203. }
  204. static TimeSpan ReadMillisecondsAttribute(XmlNode wsrmNode, bool convertToTimeSpan)
  205. {
  206. XmlAttribute millisecondsAttribute = wsrmNode.Attributes[ReliableSessionPolicyStrings.Milliseconds];
  207. if (millisecondsAttribute == null)
  208. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(SR.RequiredAttributeIsMissing, ReliableSessionPolicyStrings.Milliseconds, wsrmNode.LocalName, ReliableSessionPolicyStrings.ReliableSessionName)));
  209. UInt64 milliseconds = 0;
  210. Exception innerException = null;
  211. try
  212. {
  213. milliseconds = XmlConvert.ToUInt64(millisecondsAttribute.Value);
  214. }
  215. catch (FormatException exception)
  216. {
  217. innerException = exception;
  218. }
  219. catch (OverflowException exception)
  220. {
  221. innerException = exception;
  222. }
  223. if (innerException != null)
  224. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(SR.RequiredMillisecondsAttributeIncorrect, wsrmNode.LocalName), innerException));
  225. if (convertToTimeSpan)
  226. {
  227. TimeSpan interval;
  228. try
  229. {
  230. interval = TimeSpan.FromMilliseconds(Convert.ToDouble(milliseconds));
  231. }
  232. catch (OverflowException exception)
  233. {
  234. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(SR.MillisecondsNotConvertibleToBindingRange, wsrmNode.LocalName), exception));
  235. }
  236. return interval;
  237. }
  238. else
  239. {
  240. return default(TimeSpan);
  241. }
  242. }
  243. static void SetInactivityTimeout(ReliableSessionBindingElement settings, TimeSpan inactivityTimeout, string localName)
  244. {
  245. try
  246. {
  247. settings.InactivityTimeout = inactivityTimeout;
  248. }
  249. catch (ArgumentOutOfRangeException exception)
  250. {
  251. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(
  252. SR.GetString(SR.MillisecondsNotConvertibleToBindingRange, localName), exception));
  253. }
  254. }
  255. static void SetAcknowledgementInterval(ReliableSessionBindingElement settings, TimeSpan acknowledgementInterval, string localName)
  256. {
  257. try
  258. {
  259. settings.AcknowledgementInterval = acknowledgementInterval;
  260. }
  261. catch (ArgumentOutOfRangeException exception)
  262. {
  263. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(SR.MillisecondsNotConvertibleToBindingRange, localName), exception));
  264. }
  265. }
  266. static bool ShouldSkipNodeType(XmlNodeType type)
  267. {
  268. return (type == XmlNodeType.Comment || type == XmlNodeType.SignificantWhitespace || type == XmlNodeType.Whitespace || type == XmlNodeType.Notation);
  269. }
  270. static XmlNode SkipToNode(IEnumerator nodes)
  271. {
  272. while (nodes.MoveNext())
  273. {
  274. XmlNode currentNode = (XmlNode)nodes.Current;
  275. if (ShouldSkipNodeType(currentNode.NodeType))
  276. continue;
  277. return currentNode;
  278. }
  279. return null;
  280. }
  281. static XmlElement ThrowIfNotPolicyElement(XmlNode node, ReliableMessagingVersion reliableMessagingVersion)
  282. {
  283. string policyLocalName = MetadataStrings.WSPolicy.Elements.Policy;
  284. if (!IsElement(node, MetadataStrings.WSPolicy.NamespaceUri, policyLocalName)
  285. && !IsElement(node, MetadataStrings.WSPolicy.NamespaceUri15, policyLocalName))
  286. {
  287. string wsrmPrefix = (reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  288. ? ReliableSessionPolicyStrings.ReliableSessionFebruary2005Prefix
  289. : ReliableSessionPolicyStrings.ReliableSession11Prefix;
  290. string exceptionString = (node == null)
  291. ? SR.GetString(SR.ElementRequired, wsrmPrefix,
  292. ReliableSessionPolicyStrings.ReliableSessionName, MetadataStrings.WSPolicy.Prefix,
  293. MetadataStrings.WSPolicy.Elements.Policy)
  294. : SR.GetString(SR.ElementFound, wsrmPrefix,
  295. ReliableSessionPolicyStrings.ReliableSessionName, MetadataStrings.WSPolicy.Prefix,
  296. MetadataStrings.WSPolicy.Elements.Policy, node.LocalName, node.NamespaceURI);
  297. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(exceptionString));
  298. }
  299. return (XmlElement)node;
  300. }
  301. class Wsrm11PolicyAlternative
  302. {
  303. bool hasValidPolicy = true;
  304. bool isOrdered = false;
  305. public bool HasValidPolicy
  306. {
  307. get
  308. {
  309. return this.hasValidPolicy;
  310. }
  311. }
  312. public static Wsrm11PolicyAlternative ImportAlternative(MetadataImporter importer,
  313. IEnumerable<XmlElement> alternative)
  314. {
  315. State state = State.Security;
  316. Wsrm11PolicyAlternative wsrmPolicy = new Wsrm11PolicyAlternative();
  317. foreach (XmlElement node in alternative)
  318. {
  319. if (state == State.Security)
  320. {
  321. state = State.DeliveryAssurance;
  322. if (wsrmPolicy.TryImportSequenceSTR(node))
  323. {
  324. continue;
  325. }
  326. }
  327. if (state == State.DeliveryAssurance)
  328. {
  329. state = State.Done;
  330. if (wsrmPolicy.TryImportDeliveryAssurance(importer, node))
  331. {
  332. continue;
  333. }
  334. }
  335. string exceptionString = SR.GetString(SR.UnexpectedXmlChildNode,
  336. node.LocalName,
  337. node.NodeType,
  338. ReliableSessionPolicyStrings.ReliableSessionName);
  339. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(exceptionString));
  340. }
  341. return wsrmPolicy;
  342. }
  343. public static void ThrowInvalidBindingException()
  344. {
  345. string exceptionString = SR.GetString(SR.AssertionNotSupported,
  346. ReliableSessionPolicyStrings.ReliableSession11Prefix,
  347. ReliableSessionPolicyStrings.SequenceTransportSecurity);
  348. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(exceptionString));
  349. }
  350. public void TransferSettings(ReliableSessionBindingElement settings)
  351. {
  352. settings.Ordered = this.isOrdered;
  353. }
  354. bool TryImportSequenceSTR(XmlElement node)
  355. {
  356. string wsrmNs = ReliableSessionPolicyStrings.ReliableSession11Namespace;
  357. if (IsElement(node, wsrmNs, ReliableSessionPolicyStrings.SequenceSTR))
  358. {
  359. return true;
  360. }
  361. if (IsElement(node, wsrmNs, ReliableSessionPolicyStrings.SequenceTransportSecurity))
  362. {
  363. this.hasValidPolicy = false;
  364. return true;
  365. }
  366. return false;
  367. }
  368. bool TryImportDeliveryAssurance(MetadataImporter importer, XmlElement node)
  369. {
  370. string wsrmNs = ReliableSessionPolicyStrings.ReliableSession11Namespace;
  371. if (!IsElement(node, wsrmNs, ReliableSessionPolicyStrings.DeliveryAssurance))
  372. {
  373. return false;
  374. }
  375. // Policy
  376. IEnumerator policyNodes = node.ChildNodes.GetEnumerator();
  377. XmlNode policyNode = SkipToNode(policyNodes);
  378. XmlElement policyElement = ThrowIfNotPolicyElement(policyNode, ReliableMessagingVersion.WSReliableMessaging11);
  379. IEnumerable<IEnumerable<XmlElement>> alternatives = importer.NormalizePolicy(new XmlElement[] { policyElement });
  380. foreach (IEnumerable<XmlElement> alternative in alternatives)
  381. {
  382. State state = State.Assurance;
  383. foreach (XmlElement element in alternative)
  384. {
  385. if (state == State.Assurance)
  386. {
  387. state = State.Order;
  388. if (!IsElement(element, wsrmNs, ReliableSessionPolicyStrings.ExactlyOnce)
  389. && !IsElement(element, wsrmNs, ReliableSessionPolicyStrings.AtMostOnce)
  390. && !IsElement(element, wsrmNs, ReliableSessionPolicyStrings.AtMostOnce))
  391. {
  392. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(
  393. SR.DeliveryAssuranceRequired,
  394. wsrmNs,
  395. element.LocalName,
  396. element.NamespaceURI)));
  397. }
  398. // Found required DeliveryAssurance, ignore the value and skip to InOrder
  399. continue;
  400. }
  401. if (state == State.Order)
  402. {
  403. state = State.Done;
  404. // InOrder
  405. if (IsElement(element, wsrmNs, ReliableSessionPolicyStrings.InOrder))
  406. {
  407. // set ordered
  408. if (!this.isOrdered)
  409. {
  410. this.isOrdered = true;
  411. }
  412. continue;
  413. }
  414. }
  415. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(
  416. SR.UnexpectedXmlChildNode,
  417. element.LocalName,
  418. element.NodeType,
  419. ReliableSessionPolicyStrings.DeliveryAssurance)));
  420. }
  421. if (state == State.Assurance)
  422. {
  423. string exceptionString = SR.GetString(SR.DeliveryAssuranceRequiredNothingFound, wsrmNs);
  424. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(exceptionString));
  425. }
  426. }
  427. policyNode = SkipToNode(policyNodes);
  428. if (policyNode != null)
  429. {
  430. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidChannelBindingException(SR.GetString(
  431. SR.UnexpectedXmlChildNode,
  432. policyNode.LocalName,
  433. policyNode.NodeType,
  434. node.LocalName)));
  435. }
  436. return true;
  437. }
  438. }
  439. enum State
  440. {
  441. Security,
  442. DeliveryAssurance,
  443. Assurance,
  444. Order,
  445. InactivityTimeout,
  446. AcknowledgementInterval,
  447. Done,
  448. }
  449. }
  450. }