2 // Author: Atsushi Enomoto <atsushi@ximian.com>
4 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Dispatcher;
34 using System.Xml.Schema;
35 using System.Xml.Linq;
37 namespace System.ServiceModel.Discovery
39 public class FindCriteria
41 const string SerializationNS = "http://schemas.microsoft.com/ws/2008/06/discovery";
42 const int default_max_results = int.MaxValue;
44 public static readonly Uri ScopeMatchByExact = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/strcmp0");
45 public static readonly Uri ScopeMatchByLdap = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/ldap");
46 public static readonly Uri ScopeMatchByNone = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/none");
47 public static readonly Uri ScopeMatchByPrefix = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/rfc3986");
48 public static readonly Uri ScopeMatchByUuid = new Uri ("http://schemas.microsoft.com/ws/2008/06/discovery/uuid");
50 public static FindCriteria CreateMetadataExchangeEndpointCriteria ()
52 return CreateMetadataExchangeEndpointCriteria (typeof (IMetadataExchange));
55 public static FindCriteria CreateMetadataExchangeEndpointCriteria (IEnumerable<XmlQualifiedName> contractTypeNames)
57 var fc = new FindCriteria ();
58 foreach (var type in contractTypeNames)
59 fc.ContractTypeNames.Add (type);
63 public static FindCriteria CreateMetadataExchangeEndpointCriteria (Type contractType)
65 return new FindCriteria (contractType);
68 public FindCriteria ()
70 ContractTypeNames = new Collection<XmlQualifiedName> ();
71 Extensions = new Collection<XElement> ();
72 Scopes = new Collection<Uri> ();
73 ScopeMatchBy = ScopeMatchByPrefix;
74 MaxResults = default_max_results;
75 Duration = TimeSpan.FromSeconds (20);
78 public FindCriteria (Type contractType)
81 var cd = ContractDescription.GetContract (contractType);
82 ContractTypeNames.Add (new XmlQualifiedName (cd.Name, cd.Namespace));
85 public Collection<XmlQualifiedName> ContractTypeNames { get; private set; }
86 public TimeSpan Duration { get; set; }
87 public Collection<XElement> Extensions { get; private set; }
88 public int MaxResults { get; set; }
89 public Uri ScopeMatchBy { get; set; }
90 public Collection<Uri> Scopes { get; private set; }
92 [MonoTODO ("find out conformant behavior, and implement remaining bits")]
93 public bool IsMatch (EndpointDiscoveryMetadata endpointDiscoveryMetadata)
95 var edm = endpointDiscoveryMetadata;
97 throw new ArgumentNullException ("endpointDiscoveryMetadata");
98 if (ContractTypeNames.Count > 0) {
100 foreach (var qn in ContractTypeNames)
101 if (edm.ContractTypeNames.Contains (qn))
106 if (Scopes.Count > 0) {
108 foreach (var scope in Scopes) {
109 if (ScopeMatchBy == null || ScopeMatchBy.Equals (ScopeMatchByPrefix)) {
110 if (edm.Scopes.Contains (scope))
112 } else if (ScopeMatchBy.Equals (ScopeMatchByExact)) {
113 if (edm.Scopes.Any (s => s.AbsoluteUri == scope.AbsoluteUri))
116 else if (ScopeMatchBy.Equals (ScopeMatchByUuid))
117 throw new NotImplementedException ();
118 else if (ScopeMatchBy.Equals (ScopeMatchByNone))
119 throw new NotImplementedException ();
120 else if (ScopeMatchBy.Equals (ScopeMatchByLdap))
121 throw new NotImplementedException ();
123 throw new InvalidOperationException (String.Format ("Unexpected ScopeMatchBy value: {0}", ScopeMatchBy));
128 if (Extensions.Count > 0)
129 throw new NotImplementedException (String.Format ("{0} extensions are found", Extensions.Count));
134 internal static FindCriteria ReadXml (XmlReader reader, DiscoveryVersion version)
137 throw new ArgumentNullException ("reader");
139 var ret = new FindCriteria ();
141 reader.MoveToContent ();
142 if (!reader.IsStartElement ("Probe", version.Namespace) || reader.IsEmptyElement)
143 throw new XmlException (String.Format ("Non-empty ProbeType element is expected. Got '{0}' {1} node in namespace '{2}' instead.", reader.LocalName, reader.NodeType, reader.NamespaceURI));
144 reader.ReadStartElement ("Probe", version.Namespace);
147 reader.MoveToContent ();
148 if (reader.IsStartElement ("Types", version.Namespace))
149 ret.ContractTypeNames = new Collection<XmlQualifiedName> ((XmlQualifiedName []) reader.ReadElementContentAs (typeof (XmlQualifiedName []), null, "Types", version.Namespace));
151 reader.MoveToContent ();
152 if (reader.IsStartElement ("Types", version.Namespace)) {
153 if (reader.MoveToAttribute ("MatchBy")) {
154 ret.ScopeMatchBy = new Uri (reader.Value, UriKind.RelativeOrAbsolute);
155 reader.MoveToElement ();
158 if (reader.IsStartElement ("Scopes", version.Namespace))
159 ret.Scopes = new Collection<Uri> ((Uri []) reader.ReadElementContentAs (typeof (Uri []), null, "Scopes", version.Namespace));
161 // non-standard members
162 for (reader.MoveToContent (); !reader.EOF && reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
163 if (reader.NamespaceURI == SerializationNS) {
164 switch (reader.LocalName) {
166 ret.MaxResults = reader.ReadElementContentAsInt ();
169 ret.Duration = (TimeSpan) reader.ReadElementContentAs (typeof (TimeSpan), null);
174 ret.Extensions.Add (XElement.Load (reader));
177 reader.ReadEndElement ();
182 internal void WriteXml (XmlWriter writer, DiscoveryVersion version)
185 throw new ArgumentNullException ("writer");
188 if (ContractTypeNames.Count > 0) {
189 writer.WriteStartElement ("d", "Types", version.Namespace);
191 foreach (var qname in ContractTypeNames)
192 if (writer.LookupPrefix (qname.Namespace) == null)
193 writer.WriteAttributeString ("xmlns", "p" + p++, "http://www.w3.org/2000/xmlns/", qname.Namespace);
194 writer.WriteValue (ContractTypeNames);
195 writer.WriteEndElement ();
198 if (Scopes.Count > 0) {
199 writer.WriteStartElement ("Scopes", version.Namespace);
200 if (ScopeMatchBy != null) {
201 writer.WriteStartAttribute ("MatchBy");
202 writer.WriteValue (ScopeMatchBy);
203 writer.WriteEndAttribute ();
205 writer.WriteValue (Scopes);
206 writer.WriteEndElement ();
209 // non-standard members
210 if (MaxResults != default_max_results) {
211 writer.WriteStartElement ("MaxResults", SerializationNS);
212 writer.WriteValue (MaxResults);
213 writer.WriteEndElement ();
215 writer.WriteStartElement ("Duration", SerializationNS);
216 writer.WriteValue (Duration);
217 writer.WriteEndElement ();
219 foreach (var ext in Extensions)
220 ext.WriteTo (writer);
223 internal static XmlSchema BuildSchema (DiscoveryVersion version)
225 var schema = new XmlSchema () { TargetNamespace = version.Namespace };
227 var anyAttr = new XmlSchemaAnyAttribute () { Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax };
229 var probePart = new XmlSchemaSequence ();
230 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Types", version.Namespace), MinOccurs = 0 });
231 probePart.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName ("Scopes", version.Namespace), MinOccurs = 0 });
232 probePart.Items.Add (new XmlSchemaAny () { MinOccurs = 0, MaxOccursString = "unbounded", Namespace = "##other", ProcessContents = XmlSchemaContentProcessing.Lax });
233 var ct = new XmlSchemaComplexType () { Name = "ProbeType", Particle = probePart, AnyAttribute = anyAttr };
234 schema.Items.Add (ct);
236 schema.Items.Add (new XmlSchemaSimpleType () { Name = "QNameListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("QName", XmlSchema.Namespace) } });
238 var scr = new XmlSchemaSimpleContentRestriction () { BaseTypeName = new XmlQualifiedName ("UriListType", version.Namespace), AnyAttribute = anyAttr };
239 scr.Attributes.Add (new XmlSchemaAttribute () { Name = "matchBy", SchemaTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) });
240 schema.Items.Add (new XmlSchemaComplexType () { Name = "ScopesType", ContentModel = new XmlSchemaSimpleContent () { Content = scr } });
242 schema.Items.Add (new XmlSchemaSimpleType () { Name = "UriListType", Content = new XmlSchemaSimpleTypeList () { ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace) } });
244 schema.Items.Add (new XmlSchemaElement () { Name = "Types", SchemaTypeName = new XmlQualifiedName ("QNameListType", version.Namespace) });
245 schema.Items.Add (new XmlSchemaElement () { Name = "Scopes", SchemaTypeName = new XmlQualifiedName ("ScopesType", version.Namespace) });