HttpSimpleProtocolImporter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // System.Web.Services.Description.HttpSimpleProtocolImporter.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2003 Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.CodeDom;
  30. using System.Web.Services;
  31. using System.Web.Services.Protocols;
  32. using System.Web.Services.Configuration;
  33. using System.Xml;
  34. using System.Xml.Serialization;
  35. using System.Configuration;
  36. using System.Collections;
  37. namespace System.Web.Services.Description
  38. {
  39. internal abstract class HttpSimpleProtocolImporter : ProtocolImporter
  40. {
  41. #region Fields
  42. HttpBinding httpBinding;
  43. SoapCodeExporter soapExporter;
  44. SoapSchemaImporter soapImporter;
  45. XmlCodeExporter xmlExporter;
  46. XmlSchemaImporter xmlImporter;
  47. CodeIdentifiers memberIds;
  48. XmlReflectionImporter xmlReflectionImporter;
  49. #endregion // Fields
  50. #region Constructors
  51. public HttpSimpleProtocolImporter ()
  52. {
  53. }
  54. #endregion // Constructors
  55. #region Methods
  56. protected override CodeTypeDeclaration BeginClass ()
  57. {
  58. httpBinding = (HttpBinding) Binding.Extensions.Find (typeof(HttpBinding));
  59. CodeTypeDeclaration codeClass = new CodeTypeDeclaration (ClassName);
  60. string location = null;
  61. string url = null;
  62. if (Port != null) {
  63. HttpAddressBinding sab = (HttpAddressBinding) Port.Extensions.Find (typeof(HttpAddressBinding));
  64. if (sab != null) location = sab.Location;
  65. url = GetServiceUrl (location);
  66. }
  67. CodeConstructor cc = new CodeConstructor ();
  68. cc.Attributes = MemberAttributes.Public;
  69. if (url != null) {
  70. CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
  71. CodeAssignStatement cas = new CodeAssignStatement (ce, new CodePrimitiveExpression (url));
  72. cc.Statements.Add (cas);
  73. }
  74. codeClass.Members.Add (cc);
  75. memberIds = new CodeIdentifiers ();
  76. return codeClass;
  77. }
  78. protected override void BeginNamespace ()
  79. {
  80. xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
  81. soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
  82. xmlExporter = new XmlCodeExporter (CodeNamespace, null);
  83. xmlReflectionImporter = new XmlReflectionImporter ();
  84. }
  85. protected override void EndClass ()
  86. {
  87. if (xmlExporter.IncludeMetadata.Count > 0)
  88. {
  89. if (CodeTypeDeclaration.CustomAttributes == null)
  90. CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
  91. CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
  92. }
  93. }
  94. protected override void EndNamespace ()
  95. {
  96. }
  97. protected override bool IsBindingSupported ()
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. [MonoTODO]
  102. protected override bool IsOperationFlowSupported (OperationFlow flow)
  103. {
  104. throw new NotImplementedException ();
  105. }
  106. protected override CodeMemberMethod GenerateMethod ()
  107. {
  108. try
  109. {
  110. HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
  111. if (httpOper == null) throw new Exception ("Http operation binding not found");
  112. XmlMembersMapping inputMembers = ImportInMembersMapping (InputMessage);
  113. XmlTypeMapping outputMember = ImportOutMembersMapping (OutputMessage);
  114. CodeMemberMethod met = GenerateMethod (memberIds, httpOper, inputMembers, outputMember);
  115. xmlExporter.ExportMembersMapping (inputMembers);
  116. if (outputMember != null)
  117. xmlExporter.ExportTypeMapping (outputMember);
  118. return met;
  119. }
  120. catch (Exception ex)
  121. {
  122. UnsupportedOperationBindingWarning (ex.Message);
  123. return null;
  124. }
  125. }
  126. XmlMembersMapping ImportInMembersMapping (Message msg)
  127. {
  128. SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
  129. for (int n=0; n<mems.Length; n++)
  130. {
  131. SoapSchemaMember mem = new SoapSchemaMember();
  132. mem.MemberName = msg.Parts[n].Name;
  133. mem.MemberType = msg.Parts[n].Type;
  134. mems[n] = mem;
  135. }
  136. return soapImporter.ImportMembersMapping (Operation.Name, "", mems);
  137. }
  138. XmlTypeMapping ImportOutMembersMapping (Message msg)
  139. {
  140. if (msg.Parts.Count == 0) return null;
  141. if (msg.Parts[0].Name == "Body" && msg.Parts[0].Element == XmlQualifiedName.Empty)
  142. return xmlReflectionImporter.ImportTypeMapping (typeof(XmlNode));
  143. else {
  144. // This is a bit hacky. The issue is that types such as string[] are to be imported
  145. // as such, not as ArrayOfString class. ImportTypeMapping will return a
  146. // class if the type has not been imported as an array before, hence the
  147. // call to ImportMembersMapping.
  148. xmlImporter.ImportMembersMapping (new XmlQualifiedName[] {msg.Parts[0].Element});
  149. return xmlImporter.ImportTypeMapping (msg.Parts[0].Element);
  150. }
  151. }
  152. CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, HttpOperationBinding httpOper, XmlMembersMapping inputMembers, XmlTypeMapping outputMember)
  153. {
  154. CodeIdentifiers pids = new CodeIdentifiers ();
  155. CodeMemberMethod method = new CodeMemberMethod ();
  156. CodeMemberMethod methodBegin = new CodeMemberMethod ();
  157. CodeMemberMethod methodEnd = new CodeMemberMethod ();
  158. method.Attributes = MemberAttributes.Public;
  159. methodBegin.Attributes = MemberAttributes.Public;
  160. methodEnd.Attributes = MemberAttributes.Public;
  161. // Find unique names for temporary variables
  162. for (int n=0; n<inputMembers.Count; n++)
  163. pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
  164. string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
  165. string varResults = pids.AddUnique ("results","results");
  166. string varCallback = pids.AddUnique ("callback","callback");
  167. string varAsyncState = pids.AddUnique ("asyncState","asyncState");
  168. string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
  169. method.Name = Operation.Name;
  170. methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + Operation.Name),method);
  171. methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + Operation.Name),method);
  172. method.ReturnType = new CodeTypeReference (typeof(void));
  173. methodEnd.ReturnType = new CodeTypeReference (typeof(void));
  174. methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
  175. CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
  176. for (int n=0; n<inputMembers.Count; n++)
  177. {
  178. string ptype = GetSimpleType (inputMembers[n]);
  179. CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (ptype, inputMembers[n].MemberName);
  180. param.Direction = FieldDirection.In;
  181. method.Parameters.Add (param);
  182. methodBegin.Parameters.Add (param);
  183. paramArray [n] = new CodeVariableReferenceExpression (param.Name);
  184. }
  185. bool isVoid = true;
  186. if (outputMember != null)
  187. {
  188. method.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  189. methodEnd.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  190. xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, outputMember, "");
  191. isVoid = false;
  192. }
  193. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
  194. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
  195. methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
  196. // Array of input parameters
  197. CodeArrayCreateExpression methodParams;
  198. if (paramArray.Length > 0)
  199. methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
  200. else
  201. methodParams = new CodeArrayCreateExpression (typeof(object), 0);
  202. // Generate method url
  203. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  204. CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
  205. CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
  206. CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
  207. // Invoke call
  208. CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
  209. CodeMethodInvokeExpression inv;
  210. inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, expMethodLocation, methodParams);
  211. if (!isVoid)
  212. method.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (method.ReturnType, inv)));
  213. else
  214. method.Statements.Add (inv);
  215. // Begin Invoke Call
  216. CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
  217. CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
  218. inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, expMethodLocation, methodParams, expCallb, expAsyncs);
  219. methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
  220. // End Invoke call
  221. CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
  222. inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
  223. if (!isVoid)
  224. methodEnd.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (methodEnd.ReturnType, inv)));
  225. else
  226. methodEnd.Statements.Add (inv);
  227. // Attributes
  228. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.HttpMethodAttribute");
  229. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetOutMimeFormatter ())));
  230. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetInMimeFormatter ())));
  231. AddCustomAttribute (method, att, true);
  232. CodeTypeDeclaration.Members.Add (method);
  233. CodeTypeDeclaration.Members.Add (methodBegin);
  234. CodeTypeDeclaration.Members.Add (methodEnd);
  235. return method;
  236. }
  237. #if NET_2_0
  238. internal override CodeExpression BuildInvokeAsync (string messageName, CodeArrayCreateExpression paramsArray, CodeExpression delegateField, CodeExpression userStateVar)
  239. {
  240. HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
  241. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  242. CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
  243. CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
  244. CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
  245. CodeMethodInvokeExpression inv2 = new CodeMethodInvokeExpression (ethis, "InvokeAsync");
  246. inv2.Parameters.Add (new CodePrimitiveExpression (messageName));
  247. inv2.Parameters.Add (expMethodLocation);
  248. inv2.Parameters.Add (paramsArray);
  249. inv2.Parameters.Add (delegateField);
  250. inv2.Parameters.Add (userStateVar);
  251. return inv2;
  252. }
  253. #endif
  254. protected virtual Type GetInMimeFormatter ()
  255. {
  256. return null;
  257. }
  258. protected virtual Type GetOutMimeFormatter ()
  259. {
  260. if (OperationBinding.Output.Extensions.Find (typeof(MimeXmlBinding)) != null)
  261. return typeof (XmlReturnReader);
  262. MimeContentBinding bin = (MimeContentBinding) OperationBinding.Output.Extensions.Find (typeof(MimeContentBinding));
  263. if (bin != null && bin.Type == "text/xml")
  264. return typeof (XmlReturnReader);
  265. return typeof(NopReturnReader);
  266. }
  267. string GetSimpleType (XmlMemberMapping member)
  268. {
  269. // MS seems to always use System.String for input parameters, except for byte[]
  270. switch (member.TypeName)
  271. {
  272. case "hexBinary":
  273. case "base64Binary":
  274. return "System.String";
  275. default:
  276. string ptype = member.TypeFullName;
  277. int i = ptype.IndexOf ('[');
  278. if (i == -1)
  279. return "System.String";
  280. else
  281. return "System.String" + ptype.Substring (i);
  282. }
  283. }
  284. #endregion
  285. }
  286. }