2006-12-01 Atsushi Enomoto <atsushi@ximian.com>
[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
34 namespace System.Web.Services.Protocols
35 {
36         //
37         // This class has information abou a web service. Through providess
38         // access to the TypeStubInfo instances for each protocol.
39         //
40 #if NET_2_0
41         public
42 #else
43         internal
44 #endif
45         class ServerType // It was LogicalTypeInfo until Mono 1.2.
46         {
47                 LogicalMethodInfo[] logicalMethods;
48
49                 internal string WebServiceName;
50                 internal string WebServiceNamespace;
51                 internal string WebServiceAbstractNamespace;
52                 internal string Description;
53                 internal Type Type;
54                 bool useEncoded;
55
56                 TypeStubInfo soapProtocol;
57                 TypeStubInfo httpGetProtocol;
58                 TypeStubInfo httpPostProtocol;
59                 
60                 public ServerType (Type t)
61                 {
62                         this.Type = t;
63
64                         object [] o = Type.GetCustomAttributes (typeof (WebServiceAttribute), false);
65                         if (o.Length == 1){
66                                 WebServiceAttribute a = (WebServiceAttribute) o [0];
67                                 WebServiceName = (a.Name != string.Empty) ? a.Name : Type.Name;
68                                 WebServiceNamespace = (a.Namespace != string.Empty) ? a.Namespace : WebServiceAttribute.DefaultNamespace;
69                                 Description = a.Description;
70                         } else {
71                                 WebServiceName = Type.Name;
72                                 WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
73                         }
74                         
75                         // Determine the namespaces for literal and encoded schema types
76                         
77                         useEncoded = false;
78                         
79                         o = t.GetCustomAttributes (typeof(SoapDocumentServiceAttribute), true);
80                         if (o.Length > 0) {
81                                 SoapDocumentServiceAttribute at = (SoapDocumentServiceAttribute) o[0];
82                                 useEncoded = (at.Use == SoapBindingUse.Encoded);
83                         }
84                         else if (t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true).Length > 0)
85                                 useEncoded = true;
86                         
87                         string sep = WebServiceNamespace.EndsWith ("/") ? "" : "/";
88
89                         WebServiceAbstractNamespace = WebServiceNamespace + sep + "AbstractTypes";
90
91                         MethodInfo [] type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public);
92                         logicalMethods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
93                 }
94
95                 internal bool UseEncoded {
96                         get { return useEncoded; }
97                 }
98
99                 internal LogicalMethodInfo[] LogicalMethods
100                 {
101                         get { return logicalMethods; }
102                 }
103                 
104                 internal TypeStubInfo GetTypeStub (string protocolName)
105                 {
106                         lock (this)
107                         {
108                                 switch (protocolName)
109                                 {
110                                         case "Soap": 
111                                                 if (soapProtocol == null) soapProtocol = CreateTypeStubInfo (typeof(SoapTypeStubInfo));
112                                                 return soapProtocol;
113                                         case "HttpGet":
114                                                 if (httpGetProtocol == null) httpGetProtocol = CreateTypeStubInfo (typeof(HttpGetTypeStubInfo));
115                                                 return httpGetProtocol;
116                                         case "HttpPost":
117                                                 if (httpPostProtocol == null) httpPostProtocol = CreateTypeStubInfo (typeof(HttpPostTypeStubInfo));
118                                                 return httpPostProtocol;
119                                 }
120                         }
121                         throw new InvalidOperationException ("Protocol " + protocolName + " not supported");
122                 }
123                 
124                 TypeStubInfo CreateTypeStubInfo (Type type)
125                 {
126                         TypeStubInfo tsi = (TypeStubInfo) Activator.CreateInstance (type, new object[] {this});
127                         tsi.Initialize ();
128                         return tsi;
129                 }
130                 
131                 internal string GetWebServiceLiteralNamespace (string baseNamespace)
132                 {
133                         if (useEncoded) {
134                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
135                                 return baseNamespace + sep + "literalTypes";
136                         }
137                         else
138                                 return baseNamespace;
139                 }
140
141                 internal string GetWebServiceEncodedNamespace (string baseNamespace)
142                 {
143                         if (useEncoded)
144                                 return baseNamespace;
145                         else {
146                                 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
147                                 return baseNamespace + sep + "encodedTypes";
148                         }
149                 }
150
151                 internal string GetWebServiceNamespace (string baseNamespace, SoapBindingUse use)
152                 {
153                         if (use == SoapBindingUse.Literal) return GetWebServiceLiteralNamespace (baseNamespace);
154                         else return GetWebServiceEncodedNamespace (baseNamespace);
155                 }
156                 
157         }
158 }