2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Discovery / ContractReference.cs
index 39cfbc54397848a0c6255579d58756a3bcb52dbd..75fb86b568579ff2c0b88f9f68a089ce44ea55a9 100755 (executable)
@@ -3,17 +3,41 @@
 //\r
 // Author:\r
 //   Dave Bettin (javabettin@yahoo.com)\r
+//   Lluis Sanchez Gual (lluis@ximian.com)\r
 //\r
 // Copyright (C) Dave Bettin, 2002\r
 //\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System.IO;\r
 using System.Web.Services.Description;\r
 using System.Xml.Serialization;\r
+using System.Xml;\r
+using System.Xml.Schema;\r
 \r
 namespace System.Web.Services.Discovery {\r
 \r
-       [XmlRootAttribute("contractRef", Namespace="https://schemas.xmlsoap.org/disco/scl/", IsNullable=true)]\r
+       [XmlRootAttribute("contractRef", Namespace="http://schemas.xmlsoap.org/disco/scl/", IsNullable=true)]\r
        public class ContractReference : DiscoveryReference {\r
 \r
                #region Fields\r
@@ -24,28 +48,24 @@ namespace System.Web.Services.Discovery {
                private string defaultFilename;\r
                private string docRef;\r
                private string href;\r
-               private string url;\r
                \r
                #endregion // Fields\r
                \r
                #region Constructors\r
 \r
-               [MonoTODO]\r
                public ContractReference () \r
                {\r
-                       throw new NotImplementedException ();\r
                }\r
                \r
-               [MonoTODO]\r
                public ContractReference (string href) : this() \r
                {\r
-                       throw new NotImplementedException ();\r
+                       this.href = href;\r
                }\r
                \r
-               [MonoTODO]\r
-               public ContractReference (string href, string docRef) : this(href) \r
+               public ContractReference (string href, string docRef)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       this.href = href;\r
+                       this.docRef = docRef;\r
                }\r
                \r
                #endregion // Constructors\r
@@ -54,12 +74,21 @@ namespace System.Web.Services.Discovery {
 \r
                [XmlIgnore]\r
                public ServiceDescription Contract {\r
-                       get { return contract; }                        \r
+                       get {\r
+                               if (ClientProtocol == null) \r
+                                       throw new InvalidOperationException ("The ClientProtocol property is a null reference");\r
+                               \r
+                               ServiceDescription desc = ClientProtocol.Documents [Url] as ServiceDescription;\r
+                               if (desc == null)\r
+                                       throw new Exception ("The Documents property of ClientProtocol does not contain a WSDL document with the url " + Url);\r
+                                       \r
+                               return desc; \r
+                       }\r
                }\r
 \r
                [XmlIgnore]\r
                public override string DefaultFilename {\r
-                       get { return defaultFilename; }\r
+                       get { return FilenameFromUrl (Url) + ".wsdl"; }\r
                }\r
                \r
                [XmlAttribute("docRef")]\r
@@ -76,32 +105,87 @@ namespace System.Web.Services.Discovery {
                \r
                [XmlIgnore]\r
                public override string Url {\r
-                       get { return url;}                      \r
-                       set { url = value; }\r
+                       get { return href;}                     \r
+                       set { href = value; }\r
                }\r
                \r
                #endregion // Properties\r
 \r
                #region Methods\r
 \r
-               [MonoTODO]\r
                public override object ReadDocument (Stream stream)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       return ServiceDescription.Read (stream);\r
                }\r
                 \r
-               [MonoTODO]\r
-                protected internal override void Resolve (string contentType, Stream stream) \r
+               protected internal override void Resolve (string contentType, Stream stream) \r
+               {\r
+                       ServiceDescription wsdl = ServiceDescription.Read (stream);\r
+                       \r
+                       if (!ClientProtocol.References.Contains (Url))\r
+                               ClientProtocol.References.Add (this);\r
+\r
+                       ClientProtocol.Documents.Add (Url, wsdl);\r
+                       ResolveInternal (ClientProtocol, wsdl);\r
+               }\r
+               \r
+               internal void ResolveInternal (DiscoveryClientProtocol prot, ServiceDescription wsdl) \r
                {\r
-                       throw new NotImplementedException ();\r
+                       if (wsdl.Imports == null) return;\r
+                       \r
+                       foreach (Import import in wsdl.Imports)\r
+                       {\r
+                               // Make relative uris to absoulte\r
+\r
+                               Uri uri = new Uri (BaseUri, import.Location);\r
+                               string url = uri.ToString ();\r
+\r
+                               if (prot.Documents.Contains (url))      // Already resolved\r
+                                       continue;\r
+\r
+                               try\r
+                               {\r
+                                       string contentType = null;\r
+                                       Stream stream = prot.Download (ref url, ref contentType);\r
+                                       XmlTextReader reader = new XmlTextReader (url, stream);\r
+                                       reader.MoveToContent ();\r
+                                       \r
+                                       DiscoveryReference refe;\r
+                                       if (ServiceDescription.CanRead (reader))\r
+                                       {\r
+                                               ServiceDescription refWsdl = ServiceDescription.Read (reader);\r
+                                               refe = new ContractReference ();\r
+                                               refe.ClientProtocol = prot;\r
+                                               refe.Url = url;\r
+                                               ((ContractReference)refe).ResolveInternal (prot, refWsdl);\r
+                                               prot.Documents.Add (url, refWsdl);\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               XmlSchema schema = XmlSchema.Read (reader, null);\r
+                                               refe = new SchemaReference ();\r
+                                               refe.ClientProtocol = prot;\r
+                                               refe.Url = url;\r
+                                               prot.Documents.Add (url, schema);\r
+                                       }\r
+                                       \r
+                                       if (!prot.References.Contains (url))\r
+                                               prot.References.Add (refe);\r
+                                               \r
+                                       reader.Close ();\r
+                               }\r
+                               catch (Exception ex)\r
+                               {\r
+                                       ReportError (url, ex);\r
+                               }\r
+                       }\r
                }\r
                 \r
-               [MonoTODO]\r
-                public override void WriteDocument (object document, Stream stream) \r
+        public override void WriteDocument (object document, Stream stream) \r
                {\r
-                       throw new NotImplementedException ();\r
+                       ((ServiceDescription)document).Write (stream);\r
                }\r
 \r
                #endregion // Methods\r
        }\r
-}\r
+}