* Methods.cs: Use the service namespace as the base for the soap action.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Discovery / DiscoveryReference.cs
1 // \r
2 // System.Web.Services.Discovery.DiscoveryReference.cs\r
3 //\r
4 // Author:\r
5 //   Dave Bettin (javabettin@yahoo.com)\r
6 //   Lluis Sanchez Gual (lluis@ximian.com)\r
7 //\r
8 // Copyright (C) Dave Bettin, 2002\r
9 //\r
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 \r
32 using System.IO;\r
33 using System.Xml.Serialization;\r
34 \r
35 namespace System.Web.Services.Discovery {\r
36         public abstract class DiscoveryReference {\r
37                 \r
38                 #region Fields\r
39 \r
40                 private string defaultFilename;\r
41                 private DiscoveryClientProtocol clientProtocol;\r
42 \r
43                 #endregion // Fields\r
44 \r
45                 #region Constructors\r
46 \r
47                 protected DiscoveryReference () \r
48                 {\r
49                 }\r
50                 \r
51                 #endregion // Constructors\r
52 \r
53                 #region Properties\r
54 \r
55                 [XmlIgnore]\r
56                 public DiscoveryClientProtocol ClientProtocol {\r
57                         get { return clientProtocol; }                  \r
58                         set { clientProtocol = value; }\r
59                         \r
60                 }\r
61                 \r
62                 [XmlIgnore]\r
63                 public virtual string DefaultFilename {\r
64                         get { return FilenameFromUrl (Url); }                   \r
65                 }\r
66                 \r
67                 [XmlIgnore]\r
68                 public abstract string Url {\r
69                         get;            \r
70                         set;\r
71                 }\r
72                 \r
73                 internal Uri BaseUri \r
74                 {\r
75                         get\r
76                         {\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
80                                 else\r
81                                         return new Uri (Url);\r
82                         }\r
83                 }\r
84                 \r
85                 #endregion // Properties\r
86 \r
87                 #region Methods\r
88 \r
89                 protected static string FilenameFromUrl (string url)\r
90                 {\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
99                         \r
100                         int i = url.LastIndexOf ("/");\r
101                         if (i != -1) url = url.Substring (i+1);\r
102                         \r
103                         i = url.IndexOfAny (new char[] {'.','?','\\'});\r
104                         if (i != -1) url = url.Substring (0,i);\r
105                         \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
109                                 \r
110                         return sb.ToString ();\r
111                 }\r
112                 \r
113                 public abstract object ReadDocument (Stream stream);\r
114                 \r
115                 public void Resolve () \r
116                 {\r
117                         if (clientProtocol == null) \r
118                                 throw new InvalidOperationException ("The ClientProtocol property is a null reference.");\r
119                         \r
120                         if (clientProtocol.Documents.Contains (Url))    // Already resolved\r
121                                 return;\r
122                         \r
123                         try\r
124                         {\r
125                                 string contentType = null;\r
126                                 string url = Url;\r
127                                 Stream stream = clientProtocol.Download (ref url, ref contentType);\r
128                                 Resolve (contentType, stream);\r
129                         }\r
130                         catch (Exception ex)\r
131                         {\r
132                                 ReportError (Url, ex);\r
133                         }\r
134                 }\r
135                 \r
136                 protected internal abstract void Resolve (string contentType, Stream stream);\r
137                 \r
138                 public abstract void WriteDocument (object document, Stream stream);\r
139 \r
140                 internal void ReportError (string url, Exception ex)\r
141                 {\r
142                         if (ex is DiscoveryException) throw ex;\r
143                         else throw new DiscoveryException (url, ex);\r
144                 }\r
145                 \r
146                 #endregion // Methods\r
147         }\r
148 }