5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc.
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
31 using System.Web.Services;
32 using System.Web.Services.Description;
33 using System.Collections;
35 namespace System.Web.Services.Protocols
42 public ServerType (Type type)
44 this.type = TypeStubManager.GetLogicalTypeInfo (type);
47 internal LogicalTypeInfo LogicalType {
53 // This class has information about a web service. Through providess
54 // access to the TypeStubInfo instances for each protocol.
56 internal class LogicalTypeInfo
58 LogicalMethodInfo[] logicalMethods;
60 internal string WebServiceName;
61 internal string WebServiceNamespace;
62 internal string WebServiceAbstractNamespace;
63 internal string Description;
65 SoapBindingUse bindingUse;
66 SoapServiceRoutingStyle routingStyle;
68 TypeStubInfo soapProtocol;
69 TypeStubInfo soap12Protocol;
70 TypeStubInfo httpGetProtocol;
71 TypeStubInfo httpPostProtocol;
73 public LogicalTypeInfo (Type t)
77 object [] o = Type.GetCustomAttributes (typeof (WebServiceAttribute), false);
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;
84 WebServiceName = Type.Name;
85 WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
88 // Determine the namespaces for literal and encoded schema types
90 bindingUse = SoapBindingUse.Literal;
92 o = t.GetCustomAttributes (typeof(SoapDocumentServiceAttribute), true);
94 SoapDocumentServiceAttribute at = (SoapDocumentServiceAttribute) o[0];
96 if (bindingUse == SoapBindingUse.Default)
97 bindingUse = SoapBindingUse.Literal;
98 routingStyle = at.RoutingStyle;
100 else if (t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true).Length > 0) {
101 o = t.GetCustomAttributes (typeof(SoapRpcServiceAttribute), true);
102 SoapRpcServiceAttribute at = (SoapRpcServiceAttribute) o[0];
104 routingStyle = at.RoutingStyle;
105 if (bindingUse == SoapBindingUse.Default)
106 bindingUse = SoapBindingUse.Encoded;
109 routingStyle = SoapServiceRoutingStyle.SoapAction;
110 string sep = WebServiceNamespace.EndsWith ("/") ? "" : "/";
112 WebServiceAbstractNamespace = WebServiceNamespace + sep + "AbstractTypes";
113 MethodInfo [] type_methods;
114 if (typeof (WebClientProtocol).IsAssignableFrom (Type))
115 type_methods = Type.GetMethods (BindingFlags.Instance | BindingFlags.Public);
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)
123 foreach (Type ifaceType in Type.GetInterfaces ()) {
124 if (ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false).Length > 0) {
125 MethodInfo found = FindInInterface (ifaceType, mi);
127 if (found.GetCustomAttributes (typeof (WebMethodAttribute), false).Length > 0)
136 type_methods = (MethodInfo []) list.ToArray (typeof (MethodInfo));
138 logicalMethods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
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)
148 if (String.CompareOrdinal (
149 ifaceType.FullName.Replace ('+', '.'), 0, method.Name, 0, nameStartIndex) != 0)
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) {
162 for (int i = 0; i < rpi.Length; i++) {
163 if (rpi [i].ParameterType != lpi [i].ParameterType) {
178 internal SoapBindingUse BindingUse {
179 get { return bindingUse; }
182 internal SoapServiceRoutingStyle RoutingStyle {
183 get { return routingStyle; }
186 internal LogicalMethodInfo[] LogicalMethods
188 get { return logicalMethods; }
191 internal TypeStubInfo GetTypeStub (string protocolName)
195 switch (protocolName)
198 if (soapProtocol == null){
199 soapProtocol = new SoapTypeStubInfo (this);
200 soapProtocol.Initialize ();
205 if (soap12Protocol == null){
206 soap12Protocol = new Soap12TypeStubInfo (this);
207 soap12Protocol.Initialize ();
209 return soap12Protocol;
212 if (httpGetProtocol == null){
213 httpGetProtocol = new HttpGetTypeStubInfo (this);
214 httpGetProtocol.Initialize ();
216 return httpGetProtocol;
218 if (httpPostProtocol == null){
219 httpPostProtocol = new HttpPostTypeStubInfo (this);
220 httpPostProtocol.Initialize ();
222 return httpPostProtocol;
225 throw new InvalidOperationException ("Protocol " + protocolName + " not supported");
229 internal string GetWebServiceLiteralNamespace (string baseNamespace)
231 if (BindingUse == SoapBindingUse.Encoded) {
232 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
233 return baseNamespace + sep + "literalTypes";
236 return baseNamespace;
239 internal string GetWebServiceEncodedNamespace (string baseNamespace)
241 if (BindingUse == SoapBindingUse.Encoded)
242 return baseNamespace;
244 string sep = baseNamespace.EndsWith ("/") ? "" : "/";
245 return baseNamespace + sep + "encodedTypes";
249 internal string GetWebServiceNamespace (string baseNamespace, SoapBindingUse use)
251 if (use == SoapBindingUse.Literal) return GetWebServiceLiteralNamespace (baseNamespace);
252 else return GetWebServiceEncodedNamespace (baseNamespace);