2 // System.ServiceModel.EndpointAddress.cs
4 // Author: Duncan Mak (duncan@novell.com)
5 // Atsushi Enomoto (atsushi@ximian.com)
7 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Reflection;
32 using System.Resources;
33 using System.Runtime.Serialization;
34 using System.Security.Cryptography.X509Certificates;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
38 using System.ServiceModel.Channels;
39 using System.ServiceModel.Description;
41 namespace System.ServiceModel
43 public class EndpointAddress
45 static readonly Uri w3c_anonymous = new Uri (Constants.WsaAnonymousUri);
46 static readonly Uri anonymous_role = new Uri ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous");
47 static readonly Uri none_role = new Uri ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/None");
49 public static Uri AnonymousUri {
50 get { return anonymous_role; }
53 public static Uri NoneUri {
54 get { return none_role; }
58 AddressHeaderCollection headers;
59 EndpointIdentity identity;
60 XmlDictionaryReader metadata_reader;
61 XmlDictionaryReader extension_reader;
63 static XmlSchema schema;
65 public EndpointAddress (string uri)
66 : this (new Uri (uri), new AddressHeader [0])
70 public EndpointAddress (Uri uri, params AddressHeader [] headers)
71 : this (uri, null, new AddressHeaderCollection (headers), null, null) {}
73 public EndpointAddress (Uri uri, EndpointIdentity identity, params AddressHeader [] headers)
74 : this (uri, identity, new AddressHeaderCollection (headers), null, null) {}
76 public EndpointAddress (Uri uri, EndpointIdentity identity, AddressHeaderCollection headers)
77 : this (uri, identity, headers, null, null) {}
79 public EndpointAddress (
80 Uri uri, EndpointIdentity identity,
81 AddressHeaderCollection headers,
82 XmlDictionaryReader metadataReader,
83 XmlDictionaryReader extensionReader)
86 throw new ArgumentNullException ("uri");
87 if (!uri.IsAbsoluteUri)
88 throw new ArgumentException ("The argument uri must be absolute");
90 this.identity = identity;
91 this.headers = headers;
92 metadata_reader = metadataReader;
93 extension_reader = extensionReader;
96 public bool IsAnonymous {
97 get { return address.Equals (anonymous_role); }
101 get { return address.Equals (none_role); }
104 public AddressHeaderCollection Headers {
105 get { return headers; }
108 public EndpointIdentity Identity {
109 get { return identity; }
113 get { return address; }
117 internal static XmlSchema Schema {
119 if (schema == null) {
120 Assembly a = Assembly.GetCallingAssembly ();
121 Stream s = a.GetManifestResourceStream ("WS-Addressing.schema");
122 schema = XmlSchema.Read (s, null);
131 public void ApplyTo (Message message)
133 throw new NotImplementedException ();
136 public override bool Equals (object obj)
138 EndpointAddress other = obj as EndpointAddress;
140 other.Uri == null || !other.Uri.Equals (this.Uri) ||
141 other.Headers.Count != this.Headers.Count)
144 foreach (AddressHeader h in this.Headers) {
146 foreach (AddressHeader o in other.Headers)
158 public override int GetHashCode ()
160 return address.GetHashCode ();
163 public XmlDictionaryReader GetReaderAtExtensions ()
165 return extension_reader;
168 public XmlDictionaryReader GetReaderAtMetadata ()
170 return metadata_reader;
173 public static bool operator == (EndpointAddress address1, EndpointAddress address2)
175 if ((object) address1 == null)
176 return (object) address2 == null;
177 if ((object) address2 == null)
179 return address1.Equals (address2);
182 public static bool operator != (EndpointAddress address1, EndpointAddress address2)
184 return ! (address1 == address2);
189 public static EndpointAddress ReadFrom (
190 XmlDictionaryReader reader)
193 throw new ArgumentNullException ("reader");
195 return ReadFromInternal (null, reader);
199 public static EndpointAddress ReadFrom (
200 AddressingVersion addressingVersion,
201 XmlDictionaryReader reader)
203 return ReadFrom (addressingVersion, (XmlReader) reader);
207 public static EndpointAddress ReadFrom (
208 AddressingVersion addressingVersion,
211 if (addressingVersion == null)
212 throw new ArgumentNullException ("addressingVersion");
214 throw new ArgumentNullException ("reader");
216 return ReadFromInternal (addressingVersion, reader);
220 public static EndpointAddress ReadFrom (
221 XmlDictionaryReader reader,
222 XmlDictionaryString localName,
223 XmlDictionaryString ns)
225 return ReadFrom (AddressingVersion.WSAddressing10,
226 reader, localName, ns);
231 public static EndpointAddress ReadFrom (
232 AddressingVersion addressingVersion,
233 XmlDictionaryReader reader,
234 XmlDictionaryString localName,
235 XmlDictionaryString ns)
237 throw new NotImplementedException ();
241 public static EndpointAddress ReadFrom (
242 AddressingVersion addressingVersion,
243 XmlReader reader, string localname, string ns)
245 throw new NotImplementedException ();
248 private static EndpointAddress ReadFromInternal (
249 AddressingVersion addressingVersion,
252 reader.MoveToContent ();
253 if (reader.NodeType != XmlNodeType.Element ||
254 reader.IsEmptyElement)
255 throw new ArgumentException ("Cannot detect appropriate WS-Addressing Address element.");
258 reader.MoveToContent ();
260 if (addressingVersion == null) {
261 if (reader.NamespaceURI == AddressingVersion.WSAddressing10.Namespace)
262 addressingVersion = AddressingVersion.WSAddressing10;
264 if (reader.NamespaceURI == AddressingVersion.WSAddressingAugust2004.Namespace)
265 addressingVersion = AddressingVersion.WSAddressingAugust2004;
267 throw new ArgumentException ("Cannot detect appropriate WS-Addressing version.");
270 EndpointAddress ea = ReadContents (addressingVersion, reader);
272 reader.MoveToContent ();
273 reader.ReadEndElement ();
277 private static EndpointAddress ReadContents (
278 AddressingVersion addressingVersion, XmlReader reader)
281 MetadataSet metadata = null;
282 EndpointIdentity identity = null;
283 reader.MoveToContent ();
284 if (reader.LocalName == "Address" &&
285 reader.NamespaceURI == addressingVersion.Namespace &&
286 reader.NodeType == XmlNodeType.Element &&
287 !reader.IsEmptyElement)
288 uri = new Uri (reader.ReadElementContentAsString ());
290 throw new XmlException (String.Format (
291 "Expecting 'Address' from namespace '{0}', but found '{1}' from namespace '{2}'",
292 addressingVersion.Namespace, reader.LocalName, reader.NamespaceURI));
294 reader.MoveToContent ();
295 if (reader.LocalName == "Metadata" &&
296 reader.NamespaceURI == addressingVersion.Namespace &&
297 !reader.IsEmptyElement) {
299 metadata = (MetadataSet) new XmlSerializer (typeof (MetadataSet)).Deserialize (reader);
300 reader.MoveToContent ();
301 reader.ReadEndElement ();
303 reader.MoveToContent ();
304 if (reader.LocalName == "Identity" &&
305 reader.NamespaceURI == Constants.WsaIdentityUri) {
310 if (addressingVersion == AddressingVersion.WSAddressing10 && uri == w3c_anonymous)
311 uri = anonymous_role;
313 if (metadata == null)
314 return new EndpointAddress (uri, identity);
315 return new EndpointAddress (uri, identity,
316 AddressHeader.CreateAddressHeader (metadata));
320 public override string ToString ()
322 return address.ToString ();
326 public void WriteContentsTo (
327 AddressingVersion addressingVersion,
328 XmlDictionaryWriter writer)
331 writer.WriteString (Uri.AbsoluteUri);
333 if (addressingVersion == AddressingVersion.WSAddressing10) {
334 ((IXmlSerializable) EndpointAddress10.FromEndpointAddress (this)).WriteXml (writer);
336 writer.WriteString (Uri.AbsoluteUri);
341 public void WriteContentsTo (
342 AddressingVersion addressingVersion,
345 WriteContentsTo (addressingVersion,
346 XmlDictionaryWriter.CreateDictionaryWriter (writer));
349 public void WriteTo (
350 AddressingVersion addressingVersion,
351 XmlDictionaryWriter writer)
353 WriteTo (addressingVersion, writer, "EndpointReference", addressingVersion.Namespace);
356 public void WriteTo (
357 AddressingVersion addressingVersion, XmlWriter writer)
359 WriteTo (addressingVersion,
360 XmlDictionaryWriter.CreateDictionaryWriter (writer));
363 public void WriteTo (
364 AddressingVersion addressingVersion,
365 XmlDictionaryWriter writer,
366 XmlDictionaryString localname,
367 XmlDictionaryString ns)
369 writer.WriteStartElement (localname, ns);
370 WriteContentsTo (addressingVersion, writer);
371 writer.WriteEndElement ();
374 public void WriteTo (
375 AddressingVersion addressingVersion,
376 XmlWriter writer, string localname, string ns)
378 writer.WriteStartElement (localname, ns);
379 WriteContentsTo (addressingVersion, writer);
380 writer.WriteEndElement ();