PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.ServiceModel/EndpointAddress.cs

https://github.com/sdether/mono
C# | 390 lines | 308 code | 51 blank | 31 comment | 60 complexity | f0bc1b86810e2242358a859ca9d737e5 MD5 | raw file
  1. //
  2. // System.ServiceModel.EndpointAddress.cs
  3. //
  4. // Author: Duncan Mak (duncan@novell.com)
  5. // Atsushi Enomoto (atsushi@ximian.com)
  6. //
  7. // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Resources;
  32. using System.Runtime.Serialization;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Xml.Serialization;
  37. using System.ServiceModel.Channels;
  38. using System.ServiceModel.Description;
  39. namespace System.ServiceModel
  40. {
  41. public class EndpointAddress
  42. {
  43. static readonly Uri w3c_anonymous = new Uri (Constants.WsaAnonymousUri);
  44. static readonly Uri anonymous_role = new Uri ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous");
  45. static readonly Uri none_role = new Uri ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/None");
  46. public static Uri AnonymousUri {
  47. get { return anonymous_role; }
  48. }
  49. public static Uri NoneUri {
  50. get { return none_role; }
  51. }
  52. Uri address;
  53. AddressHeaderCollection headers;
  54. EndpointIdentity identity;
  55. XmlDictionaryReader metadata_reader;
  56. XmlDictionaryReader extension_reader;
  57. static XmlSchema schema;
  58. public EndpointAddress (string uri)
  59. : this (new Uri (uri), new AddressHeader [0])
  60. {
  61. }
  62. public EndpointAddress (Uri uri, params AddressHeader [] headers)
  63. : this (uri, null, new AddressHeaderCollection (headers), null, null) {}
  64. public EndpointAddress (Uri uri, EndpointIdentity identity, params AddressHeader [] headers)
  65. : this (uri, identity, new AddressHeaderCollection (headers), null, null) {}
  66. public EndpointAddress (Uri uri, EndpointIdentity identity, AddressHeaderCollection headers)
  67. : this (uri, identity, headers, null, null) {}
  68. public EndpointAddress (
  69. Uri uri, EndpointIdentity identity,
  70. AddressHeaderCollection headers,
  71. XmlDictionaryReader metadataReader,
  72. XmlDictionaryReader extensionReader)
  73. {
  74. if (uri == null)
  75. throw new ArgumentNullException ("uri");
  76. if (!uri.IsAbsoluteUri)
  77. throw new ArgumentException ("The argument uri must be absolute");
  78. this.address = uri;
  79. this.identity = identity;
  80. this.headers = headers;
  81. metadata_reader = metadataReader;
  82. extension_reader = extensionReader;
  83. }
  84. public bool IsAnonymous {
  85. get { return address.Equals (anonymous_role); }
  86. }
  87. public bool IsNone {
  88. get { return address.Equals (none_role); }
  89. }
  90. public AddressHeaderCollection Headers {
  91. get { return headers; }
  92. }
  93. public EndpointIdentity Identity {
  94. get { return identity; }
  95. }
  96. public Uri Uri {
  97. get { return address; }
  98. }
  99. #if !NET_2_1
  100. internal static XmlSchema Schema {
  101. get {
  102. if (schema == null) {
  103. Assembly a = Assembly.GetCallingAssembly ();
  104. Stream s = a.GetManifestResourceStream ("WS-Addressing.schema");
  105. schema = XmlSchema.Read (s, null);
  106. }
  107. return schema;
  108. }
  109. }
  110. #endif
  111. [MonoTODO]
  112. public void ApplyTo (Message message)
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. public override bool Equals (object obj)
  117. {
  118. EndpointAddress other = obj as EndpointAddress;
  119. if (other == null ||
  120. other.Uri == null || !other.Uri.Equals (this.Uri) ||
  121. other.Headers.Count != this.Headers.Count)
  122. return false;
  123. foreach (AddressHeader h in this.Headers) {
  124. bool match = false;
  125. foreach (AddressHeader o in other.Headers)
  126. if (h.Equals (o)) {
  127. match = true;
  128. break;
  129. }
  130. if (!match)
  131. return false;
  132. }
  133. return true;
  134. }
  135. public override int GetHashCode ()
  136. {
  137. return address.GetHashCode ();
  138. }
  139. public XmlDictionaryReader GetReaderAtExtensions ()
  140. {
  141. return extension_reader;
  142. }
  143. public XmlDictionaryReader GetReaderAtMetadata ()
  144. {
  145. return metadata_reader;
  146. }
  147. public static bool operator == (EndpointAddress address1, EndpointAddress address2)
  148. {
  149. if ((object) address1 == null)
  150. return (object) address2 == null;
  151. if ((object) address2 == null)
  152. return false;
  153. return address1.Equals (address2);
  154. }
  155. public static bool operator != (EndpointAddress address1, EndpointAddress address2)
  156. {
  157. return ! (address1 == address2);
  158. }
  159. //#if !NET_2_1
  160. public static EndpointAddress ReadFrom (
  161. XmlDictionaryReader reader)
  162. {
  163. if (reader == null)
  164. throw new ArgumentNullException ("reader");
  165. return ReadFromInternal (null, reader, null, null, null, null);
  166. }
  167. public static EndpointAddress ReadFrom (
  168. AddressingVersion addressingVersion,
  169. XmlDictionaryReader reader)
  170. {
  171. return ReadFrom (addressingVersion, (XmlReader) reader);
  172. }
  173. public static EndpointAddress ReadFrom (
  174. AddressingVersion addressingVersion,
  175. XmlReader reader)
  176. {
  177. if (addressingVersion == null)
  178. throw new ArgumentNullException ("addressingVersion");
  179. if (reader == null)
  180. throw new ArgumentNullException ("reader");
  181. return ReadFromInternal (addressingVersion, reader, null, null, null, null);
  182. }
  183. public static EndpointAddress ReadFrom (
  184. XmlDictionaryReader reader,
  185. XmlDictionaryString localName,
  186. XmlDictionaryString ns)
  187. {
  188. return ReadFrom (AddressingVersion.WSAddressing10,
  189. reader, localName, ns);
  190. }
  191. public static EndpointAddress ReadFrom (
  192. AddressingVersion addressingVersion,
  193. XmlDictionaryReader reader,
  194. XmlDictionaryString localName,
  195. XmlDictionaryString ns)
  196. {
  197. // Empty localName and ns will be rejected by ReadStartElement() by feeding empty strings.
  198. return ReadFromInternal (addressingVersion, reader, null, null, localName ?? XmlDictionaryString.Empty, ns ?? XmlDictionaryString.Empty);
  199. }
  200. public static EndpointAddress ReadFrom (
  201. AddressingVersion addressingVersion,
  202. XmlReader reader, string localName, string ns)
  203. {
  204. // Empty localName and ns will be rejected by ReadStartElement() by feeding empty strings.
  205. return ReadFromInternal (addressingVersion, reader, localName ?? String.Empty, ns ?? String.Empty, null, null);
  206. }
  207. private static EndpointAddress ReadFromInternal (
  208. AddressingVersion addressingVersion,
  209. XmlReader reader, string localName, string ns,
  210. XmlDictionaryString dictLocalName,
  211. XmlDictionaryString dictNS)
  212. {
  213. reader.MoveToContent ();
  214. if (reader.NodeType != XmlNodeType.Element ||
  215. reader.IsEmptyElement)
  216. throw new ArgumentException ("Cannot detect appropriate WS-Addressing Address element.");
  217. if (localName != null)
  218. reader.ReadStartElement (localName, ns);
  219. else if (dictLocalName != null)
  220. ((XmlDictionaryReader) reader).ReadStartElement (dictLocalName, dictNS);
  221. else
  222. reader.ReadStartElement ();
  223. reader.MoveToContent ();
  224. if (addressingVersion == null) {
  225. if (reader.NamespaceURI == AddressingVersion.WSAddressing10.Namespace)
  226. addressingVersion = AddressingVersion.WSAddressing10;
  227. else
  228. if (reader.NamespaceURI == AddressingVersion.WSAddressingAugust2004.Namespace)
  229. addressingVersion = AddressingVersion.WSAddressingAugust2004;
  230. else
  231. throw new ArgumentException ("Cannot detect appropriate WS-Addressing version.");
  232. }
  233. EndpointAddress ea = ReadContents (addressingVersion, reader);
  234. reader.MoveToContent ();
  235. reader.ReadEndElement ();
  236. return ea;
  237. }
  238. private static EndpointAddress ReadContents (
  239. AddressingVersion addressingVersion, XmlReader reader)
  240. {
  241. Uri uri = null;
  242. EndpointIdentity identity = null;
  243. reader.MoveToContent ();
  244. if (reader.LocalName == "Address" &&
  245. reader.NamespaceURI == addressingVersion.Namespace &&
  246. reader.NodeType == XmlNodeType.Element &&
  247. !reader.IsEmptyElement)
  248. uri = new Uri (reader.ReadElementContentAsString ());
  249. else
  250. throw new XmlException (String.Format (
  251. "Expecting 'Address' from namespace '{0}', but found '{1}' from namespace '{2}'",
  252. addressingVersion.Namespace, reader.LocalName, reader.NamespaceURI));
  253. reader.MoveToContent ();
  254. #if !NET_2_1
  255. MetadataSet metadata = null;
  256. if (reader.LocalName == "Metadata" &&
  257. reader.NamespaceURI == addressingVersion.Namespace &&
  258. !reader.IsEmptyElement) {
  259. reader.Read ();
  260. metadata = (MetadataSet) new XmlSerializer (typeof (MetadataSet)).Deserialize (reader);
  261. reader.MoveToContent ();
  262. reader.ReadEndElement ();
  263. }
  264. reader.MoveToContent ();
  265. if (reader.LocalName == "Identity" &&
  266. reader.NamespaceURI == Constants.WsaIdentityUri) {
  267. // FIXME: implement
  268. reader.Skip ();
  269. }
  270. #endif
  271. if (addressingVersion == AddressingVersion.WSAddressing10 && uri == w3c_anonymous)
  272. uri = anonymous_role;
  273. #if NET_2_1
  274. return new EndpointAddress (uri, identity);
  275. #else
  276. if (metadata == null)
  277. return new EndpointAddress (uri, identity);
  278. return new EndpointAddress (uri, identity,
  279. AddressHeader.CreateAddressHeader (metadata));
  280. #endif
  281. }
  282. public override string ToString ()
  283. {
  284. return address.ToString ();
  285. }
  286. [MonoTODO]
  287. public void WriteContentsTo (
  288. AddressingVersion addressingVersion,
  289. XmlDictionaryWriter writer)
  290. {
  291. #if NET_2_1
  292. writer.WriteString (Uri.AbsoluteUri);
  293. #else
  294. if (addressingVersion == AddressingVersion.WSAddressing10) {
  295. ((IXmlSerializable) EndpointAddress10.FromEndpointAddress (this)).WriteXml (writer);
  296. } else {
  297. writer.WriteString (Uri.AbsoluteUri);
  298. }
  299. #endif
  300. }
  301. public void WriteContentsTo (
  302. AddressingVersion addressingVersion,
  303. XmlWriter writer)
  304. {
  305. WriteContentsTo (addressingVersion,
  306. XmlDictionaryWriter.CreateDictionaryWriter (writer));
  307. }
  308. public void WriteTo (
  309. AddressingVersion addressingVersion,
  310. XmlDictionaryWriter writer)
  311. {
  312. WriteTo (addressingVersion, writer, "EndpointReference", addressingVersion.Namespace);
  313. }
  314. public void WriteTo (
  315. AddressingVersion addressingVersion, XmlWriter writer)
  316. {
  317. WriteTo (addressingVersion,
  318. XmlDictionaryWriter.CreateDictionaryWriter (writer));
  319. }
  320. public void WriteTo (
  321. AddressingVersion addressingVersion,
  322. XmlDictionaryWriter writer,
  323. XmlDictionaryString localname,
  324. XmlDictionaryString ns)
  325. {
  326. writer.WriteStartElement (localname, ns);
  327. WriteContentsTo (addressingVersion, writer);
  328. writer.WriteEndElement ();
  329. }
  330. public void WriteTo (
  331. AddressingVersion addressingVersion,
  332. XmlWriter writer, string localname, string ns)
  333. {
  334. writer.WriteStartElement (localname, ns);
  335. WriteContentsTo (addressingVersion, writer);
  336. writer.WriteEndElement ();
  337. }
  338. }
  339. }