f47f7ac117504cd81ec265e68f886aa65349e9ae
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / ServerType.cs
1 // 
2 // ServerType.cs
3 //
4 // Author:
5 //   Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System.Reflection;
31 using System.Web.Services;
32 using System.Web.Services.Description;
33 using System.Collections;
34
35 namespace System.Web.Services.Protocols
36 {
37 #if NET_2_0
38         public
39 #else
40         internal
41 #endif
42         class ServerType
43         {
44                 LogicalTypeInfo type;
45
46                 public ServerType (Type type)
47                 {
48                         this.type = TypeStubManager.GetLogicalTypeInfo (type);
49                 }
50
51                 internal LogicalTypeInfo LogicalType {
52                         get { return type; }
53                 }
54         }
55
56         //
57         // This class has information about a web service. Through providess
58         // access to the TypeStubInfo instances for each protocol.
59         //
60         internal class LogicalTypeInfo
61         {
62                 LogicalMethodInfo[] logicalMethods;
63
64                 internal string WebServiceName;
65                 internal string WebServiceNamespace;
66                 internal string WebServiceAbstractNamespace;
67                 internal string Description;
68                 internal Type Type;
69                 SoapBindingUse bindingUse;
70                 SoapServiceRoutingStyle routingStyle;
71
72                 TypeStubInfo soapProtocol;
73 #if NET_2_0
74                 TypeStubInfo soap12Protocol;
75 #endif
76                 TypeStubInfo httpGetProtocol;
77                 TypeStubInfo httpPostProtocol;
78                 
79                 public LogicalTypeInfo (Type t)
80                 {
81                         this.Type = t;
82
83                         object [] o = Type.GetCustomAttributes (typeof (WebServiceAttribute), false);
84                         if (o.Length == 1){
85                                 WebServiceAttribute a = (WebServiceAttribute) o [0];
86                                 WebServiceName = (a.Name != string.Empty) ? a.Name : Type.Name;
87                                 WebServiceNamespace = (a.Namespace != string.Empty) ? a.Namespace : WebServiceAttribute.DefaultNamespace;
88                                 Description = a.Description;
89                         } else {
90                                 WebServiceName = Type.Name;
91                                 WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
92                         }
93                         
94                         // Determine the namespaces for literal and encoded schema types
95                         
96                         bindingUse = SoapBindingUse.Literal;
97                         
98                         o = t.GetCustomAttributes (typeof(SoapDocumentServiceAttribute), true);
99                         if (o.Length > 0) {
100                                 SoapDocumentServiceAttribute at = (SoapDocumentServiceAttribute) o[0];
101                                 bindingUse = at.Use;
102                                 if (bindingUse == SoapBindingUse.Default)
103                                         bindingUse = SoapBindingUse.Literal;
104                                 routingStyle = at.RoutingStyle;
105                         }
106                         else if (t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true).Length > 0) {
107                                 o = t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true);
108                                 SoapRpcServiceAttribute at = (SoapRpcServiceAttribute) o[0];
109 #if NET_2_0
110                                 bindingUse = at.Use;
111 #else
112                                 bindingUse = SoapBindingUse.Encoded;
113 #endif
114                                 routingStyle = at.RoutingStyle;
115                                 if (bindingUse == SoapBindingUse.Default)
116                                         bindingUse = SoapBindingUse.Encoded;
117                         }
118                         else
119                                 routingStyle = SoapServiceRoutingStyle.SoapAction;
120                         string sep = WebServiceNamespace.EndsWith ("/") ? "" : "/";
121
122                         WebServiceAbstractNamespace = WebServiceNamespace + sep + "AbstractTypes";
123 #if NET_2_0
124                         MethodInfo [] type_methods;
125                         if (typeof (WebClientProtocol).IsAssignableFrom (Type))
126                                 type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public);
127                         else {
128                                 MethodInfo [] all_type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
129                                 ArrayList list = new ArrayList (all_type_methods.Length);
130                                 foreach (MethodInfo mi in all_type_methods) {
131                                         if (mi.IsPublic && mi.GetCustomAttributes (typeof (WebMethodAttribute), false).Length > 0)
132                                                 list.Add (mi);
133                                         else {
134                                                 foreach (Type ifaceType in Type.GetInterfaces ()) {
135                                                         if (ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false).Length > 0) {
136                                                                 MethodInfo found = FindInInterface (ifaceType, mi);
137                                                                 if (found != null) {
138                                                                         if (found.GetCustomAttributes (typeof (WebMethodAttribute), false).Length > 0)
139                                                                                 list.Add (found);
140
141                                                                         break;
142                                                                 }
143                                                         }
144                                                 }
145                                         }
146                                 }
147                                 type_methods = (MethodInfo []) list.ToArray (typeof (MethodInfo));
148                         }
149 #else
150                         MethodInfo [] type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public);
151 #endif
152                         logicalMethods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
153                 }
154
155                 static MethodInfo FindInInterface (Type ifaceType, MethodInfo method) {
156                         int nameStartIndex = 0;
157                         if (method.IsPrivate) {
158                                 nameStartIndex = method.Name.LastIndexOf ('.');
159                                 if (nameStartIndex < 0)
160                                         nameStartIndex = 0;
161                                 else {
162                                         if (String.CompareOrdinal (
163                                                 ifaceType.FullName.Replace ('+', '.'), 0, method.Name, 0, nameStartIndex) != 0)
164                                                 return null;
165
166                                         nameStartIndex++;
167                                 }
168                         }
169                         foreach (MethodInfo mi in ifaceType.GetMembers ()) {
170                                 if (method.ReturnType == mi.ReturnType &&
171                                         String.CompareOrdinal(method.Name, nameStartIndex, mi.Name, 0, mi.Name.Length) == 0) {
172                                         ParameterInfo [] rpi = method.GetParameters ();
173                                         ParameterInfo [] lpi = mi.GetParameters ();
174                                         if (rpi.Length == lpi.Length) {
175                                                 bool match = true;
176                                                 for (int i = 0; i < rpi.Length; i++) {
177                                                         if (rpi [i].ParameterType != lpi [i].ParameterType) {
178                                                                 match = false;
179                                                                 break;
180                                                         }
181                                                 }
182
183                                                 if (match)
184                                                         return mi;
185                                         }
186                                 }
187                         }
188
189                         return null;
190                 }
191
192                 internal SoapBindingUse BindingUse {
193                         get { return bindingUse; }
194                 }
195
196                 internal SoapServiceRoutingStyle RoutingStyle {
197                         get { return routingStyle; }
198                 }
199
200                 internal LogicalMethodInfo[] LogicalMethods
201                 {
202                         get { return logicalMethods; }
203                 }
204                 
205                 internal TypeStubInfo GetTypeStub (string protocolName)
206                 {
207                         lock (this)
208                         {
209                                 switch (protocolName)
210                                 {
211                                         case "Soap": 
212                                                 if (soapProtocol == null) soapProtocol = CreateTypeStubInfo (typeof(SoapTypeStubInfo));
213                                                 return soapProtocol;
214 #if NET_2_0
215                                         case "Soap12": 
216                                                 if (soap12Protocol == null) soap12Protocol = CreateTypeStubInfo (typeof(Soap12TypeStubInfo));
217                                                 return soap12Protocol;
218 #endif
219                                         case "HttpGet":
220                                                 if (httpGetProtocol == null) httpGetProtocol = CreateTypeStubInfo (typeof(HttpGetTypeStubInfo));
221                                                 return httpGetProtocol;
222                                         case "HttpPost":
223                                                 if (httpPostProtocol == null) httpPostProtocol = CreateTypeStubInfo (typeof(HttpPostTypeStubInfo));
224                                                 return httpPostProtocol;
225                                 }
226                         }
227                         throw new InvalidOperationException ("Protocol " + protocolName + " not supported");
228                 }
229                 
230                 TypeStubInfo CreateTypeStubInfo (Type type)
231                 {
232                         TypeStubInfo tsi = (TypeStubInfo) Activator.CreateInstance (type, new object[] {this});
233                         tsi.Initialize ();
234                         return tsi;
235                 }
236                 
237                 internal string GetWebServiceLiteralNamespace (string baseNamespace)
238                 {
239                         if (BindingUse == SoapBindingUse.Encoded) {
240                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
241                                 return baseNamespace + sep + "literalTypes";
242                         }
243                         else
244                                 return baseNamespace;
245                 }
246
247                 internal string GetWebServiceEncodedNamespace (string baseNamespace)
248                 {
249                         if (BindingUse == SoapBindingUse.Encoded)
250                                 return baseNamespace;
251                         else {
252                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
253                                 return baseNamespace + sep + "encodedTypes";
254                         }
255                 }
256
257                 internal string GetWebServiceNamespace (string baseNamespace, SoapBindingUse use)
258                 {
259                         if (use == SoapBindingUse.Literal) return GetWebServiceLiteralNamespace (baseNamespace);
260                         else return GetWebServiceEncodedNamespace (baseNamespace);
261                 }
262                 
263         }
264 }