2 // System.Web.Services.Discovery.DiscoveryReference.cs
\r
5 // Dave Bettin (javabettin@yahoo.com)
\r
6 // Lluis Sanchez Gual (lluis@ximian.com)
\r
8 // Copyright (C) Dave Bettin, 2002
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Xml.Serialization;
\r
35 namespace System.Web.Services.Discovery {
\r
36 public abstract class DiscoveryReference {
\r
40 private string defaultFilename;
\r
41 private DiscoveryClientProtocol clientProtocol;
\r
43 #endregion // Fields
\r
45 #region Constructors
\r
47 protected DiscoveryReference ()
\r
51 #endregion // Constructors
\r
56 public DiscoveryClientProtocol ClientProtocol {
\r
57 get { return clientProtocol; }
\r
58 set { clientProtocol = value; }
\r
63 public virtual string DefaultFilename {
\r
64 get { return FilenameFromUrl (Url); }
\r
68 public abstract string Url {
\r
73 internal Uri BaseUri
\r
77 int i = Url.IndexOf ("://");
\r
78 if (i == -1 || !Uri.CheckSchemeName (Url.Substring (0,i)))
\r
79 return new Uri ("file://" + Url);
\r
81 return new Uri (Url);
\r
85 #endregion // Properties
\r
89 protected static string FilenameFromUrl (string url)
\r
91 if (url.ToLower().EndsWith ("/wsdl"))
\r
92 url = url.Substring (0,url.Length-5);
\r
93 else if (url.ToLower().EndsWith ("/soap"))
\r
94 url = url.Substring (0,url.Length-5);
\r
95 else if (url.ToLower().EndsWith ("/wsdl.jsp"))
\r
96 url = url.Substring (0,url.Length-9);
\r
97 else if (url.ToLower().EndsWith ("/soap.wsdl"))
\r
98 url = url.Substring (0,url.Length-10);
\r
100 int i = url.LastIndexOf ("/");
\r
101 if (i != -1) url = url.Substring (i+1);
\r
103 i = url.IndexOfAny (new char[] {'.','?','\\'});
\r
104 if (i != -1) url = url.Substring (0,i);
\r
106 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
\r
107 for (int n=0; n<url.Length; n++)
\r
108 if (Char.IsLetterOrDigit (url[n]) || url[n] == '_') sb.Append (url[n]);
\r
110 return sb.ToString ();
\r
113 public abstract object ReadDocument (Stream stream);
\r
115 public void Resolve ()
\r
117 if (clientProtocol == null)
\r
118 throw new InvalidOperationException ("The ClientProtocol property is a null reference.");
\r
120 if (clientProtocol.Documents.Contains (Url)) // Already resolved
\r
125 string contentType = null;
\r
127 Stream stream = clientProtocol.Download (ref url, ref contentType);
\r
128 Resolve (contentType, stream);
\r
130 catch (Exception ex)
\r
132 ReportError (Url, ex);
\r
136 protected internal abstract void Resolve (string contentType, Stream stream);
\r
138 public abstract void WriteDocument (object document, Stream stream);
\r
140 internal void ReportError (string url, Exception ex)
\r
142 if (ex is DiscoveryException) throw ex;
\r
143 else throw new DiscoveryException (url, ex);
\r
146 #endregion // Methods
\r