HttpSimpleProtocolImporter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. HttpAddressBinding sab = (HttpAddressBinding) Port.Extensions.Find (typeof(HttpAddressBinding));
  62. if (sab != null) location = sab.Location;
  63. string url = GetServiceUrl (location);
  64. CodeConstructor cc = new CodeConstructor ();
  65. cc.Attributes = MemberAttributes.Public;
  66. CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
  67. CodeAssignStatement cas = new CodeAssignStatement (ce, new CodePrimitiveExpression (url));
  68. cc.Statements.Add (cas);
  69. codeClass.Members.Add (cc);
  70. memberIds = new CodeIdentifiers ();
  71. return codeClass;
  72. }
  73. protected override void BeginNamespace ()
  74. {
  75. xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
  76. soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
  77. xmlExporter = new XmlCodeExporter (CodeNamespace, null);
  78. xmlReflectionImporter = new XmlReflectionImporter ();
  79. }
  80. protected override void EndClass ()
  81. {
  82. if (xmlExporter.IncludeMetadata.Count > 0)
  83. {
  84. if (CodeTypeDeclaration.CustomAttributes == null)
  85. CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
  86. CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
  87. }
  88. }
  89. protected override void EndNamespace ()
  90. {
  91. }
  92. protected override bool IsBindingSupported ()
  93. {
  94. throw new NotImplementedException ();
  95. }
  96. [MonoTODO]
  97. protected override bool IsOperationFlowSupported (OperationFlow flow)
  98. {
  99. throw new NotImplementedException ();
  100. }
  101. protected override CodeMemberMethod GenerateMethod ()
  102. {
  103. try
  104. {
  105. HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
  106. if (httpOper == null) throw new Exception ("Http operation binding not found");
  107. XmlMembersMapping inputMembers = ImportInMembersMapping (InputMessage);
  108. XmlTypeMapping outputMember = ImportOutMembersMapping (OutputMessage);
  109. CodeMemberMethod met = GenerateMethod (memberIds, httpOper, inputMembers, outputMember);
  110. xmlExporter.ExportMembersMapping (inputMembers);
  111. if (outputMember != null)
  112. xmlExporter.ExportTypeMapping (outputMember);
  113. return met;
  114. }
  115. catch (Exception ex)
  116. {
  117. UnsupportedOperationBindingWarning (ex.Message);
  118. return null;
  119. }
  120. }
  121. XmlMembersMapping ImportInMembersMapping (Message msg)
  122. {
  123. SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
  124. for (int n=0; n<mems.Length; n++)
  125. {
  126. SoapSchemaMember mem = new SoapSchemaMember();
  127. mem.MemberName = msg.Parts[n].Name;
  128. mem.MemberType = msg.Parts[n].Type;
  129. mems[n] = mem;
  130. }
  131. return soapImporter.ImportMembersMapping (Operation.Name, "", mems);
  132. }
  133. XmlTypeMapping ImportOutMembersMapping (Message msg)
  134. {
  135. if (msg.Parts.Count == 0) return null;
  136. if (msg.Parts[0].Name == "Body" && msg.Parts[0].Element == XmlQualifiedName.Empty)
  137. return xmlReflectionImporter.ImportTypeMapping (typeof(XmlNode));
  138. else
  139. return xmlImporter.ImportTypeMapping (msg.Parts[0].Element);
  140. }
  141. CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, HttpOperationBinding httpOper, XmlMembersMapping inputMembers, XmlTypeMapping outputMember)
  142. {
  143. CodeIdentifiers pids = new CodeIdentifiers ();
  144. CodeMemberMethod method = new CodeMemberMethod ();
  145. CodeMemberMethod methodBegin = new CodeMemberMethod ();
  146. CodeMemberMethod methodEnd = new CodeMemberMethod ();
  147. method.Attributes = MemberAttributes.Public;
  148. methodBegin.Attributes = MemberAttributes.Public;
  149. methodEnd.Attributes = MemberAttributes.Public;
  150. // Find unique names for temporary variables
  151. for (int n=0; n<inputMembers.Count; n++)
  152. pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
  153. string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
  154. string varResults = pids.AddUnique ("results","results");
  155. string varCallback = pids.AddUnique ("callback","callback");
  156. string varAsyncState = pids.AddUnique ("asyncState","asyncState");
  157. string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
  158. method.Name = Operation.Name;
  159. methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + Operation.Name),method);
  160. methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + Operation.Name),method);
  161. method.ReturnType = new CodeTypeReference (typeof(void));
  162. methodEnd.ReturnType = new CodeTypeReference (typeof(void));
  163. methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
  164. CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
  165. for (int n=0; n<inputMembers.Count; n++)
  166. {
  167. string ptype = GetSimpleType (inputMembers[n]);
  168. CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (ptype, inputMembers[n].MemberName);
  169. param.Direction = FieldDirection.In;
  170. method.Parameters.Add (param);
  171. methodBegin.Parameters.Add (param);
  172. paramArray [n] = new CodeVariableReferenceExpression (param.Name);
  173. }
  174. bool isVoid = true;
  175. if (outputMember != null)
  176. {
  177. method.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  178. methodEnd.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  179. xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, outputMember, "");
  180. isVoid = false;
  181. }
  182. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
  183. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
  184. methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
  185. // Array of input parameters
  186. CodeArrayCreateExpression methodParams;
  187. if (paramArray.Length > 0)
  188. methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
  189. else
  190. methodParams = new CodeArrayCreateExpression (typeof(object), 0);
  191. // Generate method url
  192. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  193. CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
  194. CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
  195. CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
  196. // Invoke call
  197. CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
  198. CodeMethodInvokeExpression inv;
  199. inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, expMethodLocation, methodParams);
  200. if (!isVoid)
  201. method.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (method.ReturnType, inv)));
  202. else
  203. method.Statements.Add (inv);
  204. // Begin Invoke Call
  205. CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
  206. CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
  207. inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, expMethodLocation, methodParams, expCallb, expAsyncs);
  208. methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
  209. // End Invoke call
  210. CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
  211. inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
  212. if (!isVoid)
  213. methodEnd.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (methodEnd.ReturnType, inv)));
  214. else
  215. methodEnd.Statements.Add (inv);
  216. // Attributes
  217. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.HttpMethodAttribute");
  218. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetOutMimeFormatter ())));
  219. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetInMimeFormatter ())));
  220. AddCustomAttribute (method, att, true);
  221. CodeTypeDeclaration.Members.Add (method);
  222. CodeTypeDeclaration.Members.Add (methodBegin);
  223. CodeTypeDeclaration.Members.Add (methodEnd);
  224. return method;
  225. }
  226. #if NET_2_0
  227. internal override CodeExpression BuildInvokeAsync (string messageName, CodeArrayCreateExpression paramsArray, CodeExpression delegateField, CodeExpression userStateVar)
  228. {
  229. HttpOperationBinding httpOper = OperationBinding.Extensions.Find (typeof (HttpOperationBinding)) as HttpOperationBinding;
  230. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  231. CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
  232. CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
  233. CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
  234. CodeMethodInvokeExpression inv2 = new CodeMethodInvokeExpression (ethis, "InvokeAsync");
  235. inv2.Parameters.Add (new CodePrimitiveExpression (messageName));
  236. inv2.Parameters.Add (expMethodLocation);
  237. inv2.Parameters.Add (paramsArray);
  238. inv2.Parameters.Add (delegateField);
  239. inv2.Parameters.Add (userStateVar);
  240. return inv2;
  241. }
  242. #endif
  243. protected virtual Type GetInMimeFormatter ()
  244. {
  245. return null;
  246. }
  247. protected virtual Type GetOutMimeFormatter ()
  248. {
  249. if (OperationBinding.Output.Extensions.Find (typeof(MimeXmlBinding)) != null)
  250. return typeof (XmlReturnReader);
  251. MimeContentBinding bin = (MimeContentBinding) OperationBinding.Output.Extensions.Find (typeof(MimeContentBinding));
  252. if (bin != null && bin.Type == "text/xml")
  253. return typeof (XmlReturnReader);
  254. return typeof(NopReturnReader);
  255. }
  256. string GetSimpleType (XmlMemberMapping member)
  257. {
  258. // MS seems to always use System.String for input parameters, except for byte[]
  259. switch (member.TypeName)
  260. {
  261. case "hexBinary":
  262. case "base64Binary":
  263. return "System.String";
  264. default:
  265. string ptype = member.TypeFullName;
  266. int i = ptype.IndexOf ('[');
  267. if (i == -1)
  268. return "System.String";
  269. else
  270. return "System.String" + ptype.Substring (i);
  271. }
  272. }
  273. #endregion
  274. }
  275. }