[eglib] Prefer <langinfo.h> to <localcharset.h>
[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         public
38         class ServerType
39         {
40                 LogicalTypeInfo type;
41
42                 public ServerType (Type type)
43                 {
44                         this.type = TypeStubManager.GetLogicalTypeInfo (type);
45                 }
46
47                 internal LogicalTypeInfo LogicalType {
48                         get { return type; }
49                 }
50         }
51
52         //
53         // This class has information about a web service. Through providess
54         // access to the TypeStubInfo instances for each protocol.
55         //
56         internal class LogicalTypeInfo
57         {
58                 LogicalMethodInfo[] logicalMethods;
59
60                 internal string WebServiceName;
61                 internal string WebServiceNamespace;
62                 internal string WebServiceAbstractNamespace;
63                 internal string Description;
64                 internal Type Type;
65                 SoapBindingUse bindingUse;
66                 SoapServiceRoutingStyle routingStyle;
67
68                 TypeStubInfo soapProtocol;
69                 TypeStubInfo soap12Protocol;
70                 TypeStubInfo httpGetProtocol;
71                 TypeStubInfo httpPostProtocol;
72                 
73                 public LogicalTypeInfo (Type t)
74                 {
75                         this.Type = t;
76
77                         object [] o = Type.GetCustomAttributes (typeof (WebServiceAttribute), false);
78                         if (o.Length == 1){
79                                 WebServiceAttribute a = (WebServiceAttribute) o [0];
80                                 WebServiceName = (a.Name != string.Empty) ? a.Name : Type.Name;
81                                 WebServiceNamespace = (a.Namespace != string.Empty) ? a.Namespace : WebServiceAttribute.DefaultNamespace;
82                                 Description = a.Description;
83                         } else {
84                                 WebServiceName = Type.Name;
85                                 WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
86                         }
87                         
88                         // Determine the namespaces for literal and encoded schema types
89                         
90                         bindingUse = SoapBindingUse.Literal;
91                         
92                         o = t.GetCustomAttributes (typeof(SoapDocumentServiceAttribute), true);
93                         if (o.Length > 0) {
94                                 SoapDocumentServiceAttribute at = (SoapDocumentServiceAttribute) o[0];
95                                 bindingUse = at.Use;
96                                 if (bindingUse == SoapBindingUse.Default)
97                                         bindingUse = SoapBindingUse.Literal;
98                                 routingStyle = at.RoutingStyle;
99                         }
100                         else if (t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true).Length > 0) {
101                                 o = t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true);
102                                 SoapRpcServiceAttribute at = (SoapRpcServiceAttribute) o[0];
103                                 bindingUse = at.Use;
104                                 routingStyle = at.RoutingStyle;
105                                 if (bindingUse == SoapBindingUse.Default)
106                                         bindingUse = SoapBindingUse.Encoded;
107                         }
108                         else
109                                 routingStyle = SoapServiceRoutingStyle.SoapAction;
110                         string sep = WebServiceNamespace.EndsWith ("/") ? "" : "/";
111
112                         WebServiceAbstractNamespace = WebServiceNamespace + sep + "AbstractTypes";
113                         MethodInfo [] type_methods;
114                         if (typeof (WebClientProtocol).IsAssignableFrom (Type))
115                                 type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public);
116                         else {
117                                 MethodInfo [] all_type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
118                                 ArrayList list = new ArrayList (all_type_methods.Length);
119                                 foreach (MethodInfo mi in all_type_methods) {
120                                         if (mi.IsPublic && mi.GetCustomAttributes (typeof (WebMethodAttribute), false).Length > 0)
121                                                 list.Add (mi);
122                                         else {
123                                                 foreach (Type ifaceType in Type.GetInterfaces ()) {
124                                                         if (ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false).Length > 0) {
125                                                                 MethodInfo found = FindInInterface (ifaceType, mi);
126                                                                 if (found != null) {
127                                                                         if (found.GetCustomAttributes (typeof (WebMethodAttribute), false).Length > 0)
128                                                                                 list.Add (found);
129
130                                                                         break;
131                                                                 }
132                                                         }
133                                                 }
134                                         }
135                                 }
136                                 type_methods = (MethodInfo []) list.ToArray (typeof (MethodInfo));
137                         }
138                         logicalMethods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
139                 }
140
141                 static MethodInfo FindInInterface (Type ifaceType, MethodInfo method) {
142                         int nameStartIndex = 0;
143                         if (method.IsPrivate) {
144                                 nameStartIndex = method.Name.LastIndexOf ('.');
145                                 if (nameStartIndex < 0)
146                                         nameStartIndex = 0;
147                                 else {
148                                         if (String.CompareOrdinal (
149                                                 ifaceType.FullName.Replace ('+', '.'), 0, method.Name, 0, nameStartIndex) != 0)
150                                                 return null;
151
152                                         nameStartIndex++;
153                                 }
154                         }
155                         foreach (MethodInfo mi in ifaceType.GetMembers ()) {
156                                 if (method.ReturnType == mi.ReturnType &&
157                                         String.CompareOrdinal(method.Name, nameStartIndex, mi.Name, 0, mi.Name.Length) == 0) {
158                                         ParameterInfo [] rpi = method.GetParameters ();
159                                         ParameterInfo [] lpi = mi.GetParameters ();
160                                         if (rpi.Length == lpi.Length) {
161                                                 bool match = true;
162                                                 for (int i = 0; i < rpi.Length; i++) {
163                                                         if (rpi [i].ParameterType != lpi [i].ParameterType) {
164                                                                 match = false;
165                                                                 break;
166                                                         }
167                                                 }
168
169                                                 if (match)
170                                                         return mi;
171                                         }
172                                 }
173                         }
174
175                         return null;
176                 }
177
178                 internal SoapBindingUse BindingUse {
179                         get { return bindingUse; }
180                 }
181
182                 internal SoapServiceRoutingStyle RoutingStyle {
183                         get { return routingStyle; }
184                 }
185
186                 internal LogicalMethodInfo[] LogicalMethods
187                 {
188                         get { return logicalMethods; }
189                 }
190
191                 internal TypeStubInfo GetTypeStub (string protocolName)
192                 {
193                         lock (this)
194                         {
195                                 switch (protocolName)
196                                 {
197                                 case "Soap": 
198                                         if (soapProtocol == null){
199                                                 soapProtocol = new SoapTypeStubInfo (this);
200                                                 soapProtocol.Initialize ();
201                                         }
202                                         return soapProtocol;
203                                         
204                                 case "Soap12": 
205                                         if (soap12Protocol == null){
206                                                 soap12Protocol = new Soap12TypeStubInfo (this);
207                                                 soap12Protocol.Initialize ();
208                                         }
209                                         return soap12Protocol;
210 #if !MOBILE
211                                 case "HttpGet":
212                                         if (httpGetProtocol == null){
213                                                 httpGetProtocol = new HttpGetTypeStubInfo (this);
214                                                 httpGetProtocol.Initialize ();
215                                         }
216                                         return httpGetProtocol;
217                                 case "HttpPost":
218                                         if (httpPostProtocol == null){
219                                                 httpPostProtocol = new HttpPostTypeStubInfo (this);
220                                                 httpPostProtocol.Initialize ();
221                                         }
222                                         return httpPostProtocol;
223 #endif
224                                 }
225                                 throw new InvalidOperationException ("Protocol " + protocolName + " not supported");
226                         }
227                 }
228                 
229                 internal string GetWebServiceLiteralNamespace (string baseNamespace)
230                 {
231                         if (BindingUse == SoapBindingUse.Encoded) {
232                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
233                                 return baseNamespace + sep + "literalTypes";
234                         }
235                         else
236                                 return baseNamespace;
237                 }
238
239                 internal string GetWebServiceEncodedNamespace (string baseNamespace)
240                 {
241                         if (BindingUse == SoapBindingUse.Encoded)
242                                 return baseNamespace;
243                         else {
244                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
245                                 return baseNamespace + sep + "encodedTypes";
246                         }
247                 }
248
249                 internal string GetWebServiceNamespace (string baseNamespace, SoapBindingUse use)
250                 {
251                         if (use == SoapBindingUse.Literal) return GetWebServiceLiteralNamespace (baseNamespace);
252                         else return GetWebServiceEncodedNamespace (baseNamespace);
253                 }
254                 
255         }
256 }