* System.Web.Services.dll.sources: Added
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / Methods.cs
index 3b3d438877c080ca097a4fa60a7a8e1eb0b6d6f6..c68ff7d2a2904939171ad21444004a58b3e0717c 100644 (file)
@@ -24,12 +24,8 @@ namespace System.Web.Services.Protocols {
        // This class represents all the information we extract from a MethodInfo
        // in the SoapHttpClientProtocol derivative stub class
        //
-       internal class MethodStubInfo {
-               internal LogicalMethodInfo MethodInfo;
-
-               // The name used bythe stub class to reference this method.
-               internal string Name;
-               
+       internal class SoapMethodStubInfo : MethodStubInfo
+       {
                internal string Action;
                internal string Binding;
 
@@ -41,44 +37,55 @@ namespace System.Web.Services.Protocols {
                internal string ResponseName;
                internal string ResponseNamespace;
                
-               internal bool   OneWay;
+               internal bool OneWay;
                internal SoapParameterStyle ParameterStyle;
-
-               internal bool BufferResponse;
-               internal int CacheDuration;
-               internal string Description;
-               internal bool EnableSession;
+               internal SoapBindingStyle SoapBindingStyle;
+               internal SoapBindingUse Use;
 
                internal XmlSerializer RequestSerializer;
                internal XmlSerializer ResponseSerializer;
 
                internal HeaderInfo[] Headers;
 
+               internal SoapExtensionRuntimeConfig[] SoapExtensions;
+               
+               private XmlMembersMapping[] _membersMapping;
+               
+               internal XmlMembersMapping InputMembersMapping
+               {
+                       get { return _membersMapping[0]; }
+               }
+
+               internal XmlMembersMapping OutputMembersMapping
+               {
+                       get { return _membersMapping[1]; }
+               }
+
                //
                // Constructor
                //
-               MethodStubInfo (TypeStubInfo parent, LogicalMethodInfo source, object kind, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
+               public SoapMethodStubInfo (TypeStubInfo typeStub, LogicalMethodInfo source, object kind, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
+               : base (typeStub, source)
                {
-                       MethodInfo = source;
-
+                       SoapTypeStubInfo parent = (SoapTypeStubInfo) typeStub;
                        XmlElementAttribute optional_ns = null;
-                       SoapBindingUse use;
 
                        if (kind == null) {
-                               use = parent.Use;
+                               Use = parent.Use;
                                RequestName = "";
                                RequestNamespace = parent.WebServiceNamespace;
                                ResponseName = "";
                                ResponseNamespace = parent.WebServiceNamespace;
                                ParameterStyle = parent.ParameterStyle;
+                               SoapBindingStyle = parent.SoapBindingStyle;
                                OneWay = false;
                        }
                        else if (kind is SoapDocumentMethodAttribute){
                                SoapDocumentMethodAttribute dma = (SoapDocumentMethodAttribute) kind;
                                
-                               use = dma.Use;
-                               if (use == SoapBindingUse.Default)
-                                       use = parent.Use;
+                               Use = dma.Use;
+                               if (Use == SoapBindingUse.Default)
+                                       Use = parent.Use;
                                
                                Action = dma.Action;
                                Binding = dma.Binding;
@@ -90,9 +97,10 @@ namespace System.Web.Services.Protocols {
                                if (ParameterStyle == SoapParameterStyle.Default)
                                        ParameterStyle = parent.ParameterStyle;
                                OneWay = dma.OneWay;
+                               SoapBindingStyle = SoapBindingStyle.Document;
                        } else {
                                SoapRpcMethodAttribute rma = (SoapRpcMethodAttribute) kind;
-                               use = SoapBindingUse.Encoded;   // RPC always use encoded
+                               Use = SoapBindingUse.Encoded;   // RPC always use encoded
 
                                Action = rma.Action;
                                Binding = rma.Binding;
@@ -100,7 +108,9 @@ namespace System.Web.Services.Protocols {
                                RequestNamespace = rma.RequestNamespace;
                                ResponseNamespace = rma.ResponseNamespace;
                                ResponseName = rma.ResponseElementName;
+                               ParameterStyle = SoapParameterStyle.Wrapped;
                                OneWay = rma.OneWay;
+                               SoapBindingStyle = SoapBindingStyle.Rpc;
 
                                // For RPC calls, make all arguments be part of the empty namespace
                                optional_ns = new XmlElementAttribute ();
@@ -114,60 +124,38 @@ namespace System.Web.Services.Protocols {
                                        throw new Exception ("OneWay methods should not have out/ref parameters");
                        }
                        
-                       object [] o = source.GetCustomAttributes (typeof (WebMethodAttribute));
-                       if (o.Length == 1){
-                               WebMethodAttribute wma = (WebMethodAttribute) o [0];
-                               BufferResponse = wma.BufferResponse;
-                               CacheDuration = wma.CacheDuration;
-                               Description = wma.Description;
-                               EnableSession = wma.EnableSession;
-                               Name = wma.MessageName;
-
-                               if (Name == "")
-                                       Name = source.Name;
-                       } else
-                               Name = source.Name;
-
-                       if (RequestName == "")
-                               RequestName = Name;
-
-                       if (ResponseName == "")
-                               ResponseName = Name + "Response";
-
+                       if (RequestNamespace == "") RequestNamespace = parent.WebServiceNamespace;
+                       if (ResponseNamespace == "") ResponseNamespace = parent.WebServiceNamespace;
+                       if (RequestName == "") RequestName = Name;
+                       if (ResponseName == "") ResponseName = Name + "Response";
+                       if (Binding == null || Binding == "") Binding = parent.DefaultBinding;
+                       else if (parent.GetBinding (Binding) == null) throw new InvalidOperationException ("Type '" + parent.Type + "' is missing WebServiceBinding attribute that defines a binding named '" + Binding + "'");
+                               
+                       if (Action == null || Action == "")
+                               Action = RequestNamespace.EndsWith("/") ? (RequestNamespace + Name) : (RequestNamespace + "/" + Name);
+                       
+                       bool hasWrappingElem = (ParameterStyle == SoapParameterStyle.Wrapped);
+                       
                        XmlReflectionMember [] in_members = BuildRequestReflectionMembers (optional_ns);
                        XmlReflectionMember [] out_members = BuildResponseReflectionMembers (optional_ns);
 
-                       XmlMembersMapping [] members = new XmlMembersMapping [2];
-                       try {
-                               if (use == SoapBindingUse.Literal) {
-                                       members [0] = xmlImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, true);
-                                       members [1] = xmlImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, true);
-                               }
-                               else {
-                                       members [0] = soapImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, true, true);
-                                       members [1] = soapImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, true, true);
-                               }
-
-                               XmlSerializer [] s = null;
-                               s = XmlSerializer.FromMappings (members);
-                               RequestSerializer = s [0];
-                               ResponseSerializer = s [1];
-                       } catch {
-                               Console.WriteLine ("Got exception while creating serializer");
-                               Console.WriteLine ("Method name: " + RequestName + " parameters are:");
-
-                               for (int i = 0; i < in_members.Length; i++) {
-                                       Console.WriteLine ("    {0}: {1} {2}", i, in_members [i].MemberName, in_members [i].MemberType);
-                               }
+                       _membersMapping = new XmlMembersMapping [2];
 
-                               Console.WriteLine ("Output parameters are:");
-                               for (int i = 0; i < out_members.Length; i++) {
-                                       Console.WriteLine ("    {0}: {1} {2}", i, out_members [i].MemberName, out_members [i].MemberType);
-                               }
-                               throw;
+                       if (Use == SoapBindingUse.Literal) {
+                               _membersMapping [0] = xmlImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem);
+                               _membersMapping [1] = xmlImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem);
+                       }
+                       else {
+                               _membersMapping [0] = soapImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem, true);
+                               _membersMapping [1] = soapImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem, true);
                        }
 
-                       o = source.GetCustomAttributes (typeof (SoapHeaderAttribute));
+                       XmlSerializer [] s = null;
+                       s = XmlSerializer.FromMappings (_membersMapping);
+                       RequestSerializer = s [0];
+                       ResponseSerializer = s [1];
+
+                       object[] o = source.GetCustomAttributes (typeof (SoapHeaderAttribute));
                        Headers = new HeaderInfo[o.Length];
                        for (int i = 0; i < o.Length; i++) {
                                SoapHeaderAttribute att = (SoapHeaderAttribute) o[i];
@@ -178,20 +166,8 @@ namespace System.Web.Services.Protocols {
                                Headers [i] = new HeaderInfo (mems[0], att);
                                parent.RegisterHeaderType (headerType);
                        }
-               }
 
-               static internal MethodStubInfo Create (TypeStubInfo parent, LogicalMethodInfo lmi, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
-               {
-                       object [] o = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
-                       if (o.Length == 0) o = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));
-
-                       if (o.Length == 0)
-                       {
-                               if (lmi.GetCustomAttributes (typeof (WebMethodAttribute)).Length == 0) return null;
-                               return new MethodStubInfo (parent, lmi, null, xmlImporter, soapImporter);
-                       }
-                       else
-                               return new MethodStubInfo (parent, lmi, o [0], xmlImporter, soapImporter);
+                       SoapExtensions = SoapExtension.GetMethodExtensions (source);
                }
 
                XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns)
@@ -293,6 +269,11 @@ namespace System.Web.Services.Protocols {
                        if (Member is PropertyInfo) ((PropertyInfo)Member).SetValue (ob, value, null);
                        else ((FieldInfo)Member).SetValue (ob, value);
                }
+
+               public SoapHeaderDirection Direction
+               {
+                       get { return AttributeInfo.Direction; }
+               }
        }
 
        internal class Fault
@@ -312,44 +293,37 @@ namespace System.Web.Services.Protocols {
                public string faultactor;
                public XmlNode detail;
        }
-
+       
        //
        // Holds the metadata loaded from the type stub, as well as
        // the metadata for all the methods in the type
        //
-       internal class TypeStubInfo {
-               Hashtable name_to_method = new Hashtable ();
+       internal class SoapTypeStubInfo : TypeStubInfo
+       {
                Hashtable header_serializers = new Hashtable ();
                Hashtable header_serializers_byname = new Hashtable ();
-               Hashtable bindings = new Hashtable ();
 
                // Precomputed
                internal SoapParameterStyle      ParameterStyle;
                internal SoapServiceRoutingStyle RoutingStyle;
                internal SoapBindingUse          Use;
-               internal string                  WebServiceName;
-               internal string                  WebServiceNamespace;
                internal XmlSerializer           FaultSerializer;
+               internal SoapExtensionRuntimeConfig[][] SoapExtensions;
+               internal SoapBindingStyle SoapBindingStyle;
+               internal XmlReflectionImporter  xmlImporter;
+               internal SoapReflectionImporter soapImporter;
 
-               void GetTypeAttributes (Type t)
+               public SoapTypeStubInfo (Type t)
+               : base (t)
                {
+                       xmlImporter = new XmlReflectionImporter ();
+                       soapImporter = new SoapReflectionImporter ();
+                       
                        object [] o;
 
                        o = t.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
-
                        foreach (WebServiceBindingAttribute at in o)
-                               bindings.Add (((WebServiceBindingAttribute)at).Name, at);
-
-                       o = t.GetCustomAttributes (typeof (WebServiceAttribute), false);
-                       if (o.Length == 1){
-                               WebServiceAttribute a = (WebServiceAttribute) o [0];
-
-                               WebServiceName = a.Name;
-                               WebServiceNamespace = a.Namespace;
-                       } else {
-                               WebServiceName = t.Name;
-                               WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
-                       }
+                               Bindings.Add (new BindingInfo (at, WebServiceNamespace));
 
                        o = t.GetCustomAttributes (typeof (SoapDocumentServiceAttribute), false);
                        if (o.Length == 1){
@@ -358,6 +332,7 @@ namespace System.Web.Services.Protocols {
                                ParameterStyle = a.ParameterStyle;
                                RoutingStyle = a.RoutingStyle;
                                Use = a.Use;
+                               SoapBindingStyle = SoapBindingStyle.Document;
                        } else {
                                o = t.GetCustomAttributes (typeof (SoapRpcServiceAttribute), false);
                                if (o.Length == 1){
@@ -365,58 +340,59 @@ namespace System.Web.Services.Protocols {
                                        
                                        ParameterStyle = SoapParameterStyle.Wrapped;
                                        RoutingStyle = srs.RoutingStyle;
-                                       Use = SoapBindingUse.Literal;
+                                       Use = SoapBindingUse.Encoded;
+                                       SoapBindingStyle = SoapBindingStyle.Rpc;
                                } else {
                                        ParameterStyle = SoapParameterStyle.Wrapped;
                                        RoutingStyle = SoapServiceRoutingStyle.SoapAction;
                                        Use = SoapBindingUse.Literal;
+                                       SoapBindingStyle = SoapBindingStyle.Document;
                                }
                        }
+                       
+                       if (ParameterStyle == SoapParameterStyle.Default) ParameterStyle = SoapParameterStyle.Wrapped;
+                       if (Use == SoapBindingUse.Default) Use = SoapBindingUse.Literal;
+
                        FaultSerializer = new XmlSerializer (typeof(Fault));
+                       SoapExtensions = SoapExtension.GetTypeExtensions (t);
                }
 
-               //
-               // Extract all method information
-               //
-               void GetTypeMethods (Type t, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
+               public override XmlReflectionImporter XmlImporter 
                {
-                       MethodInfo [] type_methods = t.GetMethods (BindingFlags.Instance | BindingFlags.Public);
-                       LogicalMethodInfo [] methods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
-
-                       foreach (LogicalMethodInfo mi in methods){
-                               MethodStubInfo msi = MethodStubInfo.Create (this, mi, xmlImporter, soapImporter);
-
-                               if (msi == null)
-                                       continue;
+                       get { return xmlImporter; }
+               }
 
-                               name_to_method [msi.Name] = msi;
-                       }
+               public override SoapReflectionImporter SoapImporter 
+               {
+                       get { return soapImporter; }
                }
                
-               internal TypeStubInfo (Type t)
+               public override string ProtocolName
                {
-                       GetTypeAttributes (t);
-
-                       XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
-                       SoapReflectionImporter soapImporter = new SoapReflectionImporter ();
-                       GetTypeMethods (t, xmlImporter, soapImporter);
+                       get { return "Soap"; }
                }
-
-               internal MethodStubInfo GetMethod (string name)
+               
+               protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo lmi, bool isClientProxy)
                {
-                       return (MethodStubInfo) name_to_method [name];
-               }
-
-
+                       object [] ats = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
+                       if (ats.Length == 0) ats = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));
 
+                       if (ats.Length == 0 && isClientProxy)
+                               return null;
+                       else if (ats.Length == 0)
+                               return new SoapMethodStubInfo (parent, lmi, null, xmlImporter, soapImporter);
+                       else
+                               return new SoapMethodStubInfo (parent, lmi, ats[0], xmlImporter, soapImporter);
+               }
+               
                internal void RegisterHeaderType (Type type)
                {
                        XmlSerializer s = (XmlSerializer) header_serializers [type];
                        if (s != null) return;
 
                        XmlReflectionImporter ri = new XmlReflectionImporter ();
-                       XmlTypeMapping tm = ri.ImportTypeMapping (type);
-                       s = new XmlSerializer (type);
+                       XmlTypeMapping tm = ri.ImportTypeMapping (type, WebServiceAttribute.DefaultNamespace);
+                       s = new XmlSerializer (tm);
 
                        header_serializers [type] = s;
                        header_serializers_byname [new XmlQualifiedName (tm.ElementName, tm.Namespace)] = s;
@@ -430,41 +406,6 @@ namespace System.Web.Services.Protocols {
                internal XmlSerializer GetHeaderSerializer (XmlQualifiedName qname)
                {
                        return (XmlSerializer) header_serializers_byname [qname];
-               }
-       }
-       
-       //
-       // Manages 
-       //
-       internal class TypeStubManager {
-               static Hashtable type_to_manager;
-               
-               static TypeStubManager ()
-               {
-                       type_to_manager = new Hashtable ();
-               }
-
-               //
-               // This needs to be thread safe
-               //
-               static internal TypeStubInfo GetTypeStub (Type t)
-               {
-                       TypeStubInfo tm = (TypeStubInfo) type_to_manager [t];
-
-                       if (tm != null)
-                               return tm;
-
-                       lock (typeof (TypeStubInfo)){
-                               tm = (TypeStubInfo) type_to_manager [t];
-
-                               if (tm != null)
-                                       return tm;
-                               
-                               tm = new TypeStubInfo (t);
-                               type_to_manager [t] = tm;
-
-                               return tm;
-                       }
-               }
+               }               
        }
 }