HttpSimpleProtocolImporter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. xmlExporter.ExportTypeMapping (outputMember);
  112. return met;
  113. }
  114. catch (Exception ex)
  115. {
  116. UnsupportedOperationBindingWarning (ex.Message);
  117. return null;
  118. }
  119. }
  120. XmlMembersMapping ImportInMembersMapping (Message msg)
  121. {
  122. SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
  123. for (int n=0; n<mems.Length; n++)
  124. {
  125. SoapSchemaMember mem = new SoapSchemaMember();
  126. mem.MemberName = msg.Parts[n].Name;
  127. mem.MemberType = msg.Parts[n].Type;
  128. mems[n] = mem;
  129. }
  130. return soapImporter.ImportMembersMapping (Operation.Name, "", mems);
  131. }
  132. XmlTypeMapping ImportOutMembersMapping (Message msg)
  133. {
  134. if (msg.Parts.Count == 0) return null;
  135. if (msg.Parts[0].Name == "Body" && msg.Parts[0].Element == XmlQualifiedName.Empty)
  136. return xmlReflectionImporter.ImportTypeMapping (typeof(XmlNode));
  137. else
  138. return xmlImporter.ImportTypeMapping (msg.Parts[0].Element);
  139. }
  140. CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, HttpOperationBinding httpOper, XmlMembersMapping inputMembers, XmlTypeMapping outputMember)
  141. {
  142. CodeIdentifiers pids = new CodeIdentifiers ();
  143. CodeMemberMethod method = new CodeMemberMethod ();
  144. CodeMemberMethod methodBegin = new CodeMemberMethod ();
  145. CodeMemberMethod methodEnd = new CodeMemberMethod ();
  146. method.Attributes = MemberAttributes.Public;
  147. methodBegin.Attributes = MemberAttributes.Public;
  148. methodEnd.Attributes = MemberAttributes.Public;
  149. // Find unique names for temporary variables
  150. for (int n=0; n<inputMembers.Count; n++)
  151. pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
  152. string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
  153. string varResults = pids.AddUnique ("results","results");
  154. string varCallback = pids.AddUnique ("callback","callback");
  155. string varAsyncState = pids.AddUnique ("asyncState","asyncState");
  156. string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
  157. method.Name = Operation.Name;
  158. methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + Operation.Name),method);
  159. methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + Operation.Name),method);
  160. method.ReturnType = new CodeTypeReference (typeof(void));
  161. methodEnd.ReturnType = new CodeTypeReference (typeof(void));
  162. methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
  163. CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
  164. for (int n=0; n<inputMembers.Count; n++)
  165. {
  166. string ptype = GetSimpleType (inputMembers[n]);
  167. CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression (ptype, inputMembers[n].MemberName);
  168. param.Direction = FieldDirection.In;
  169. method.Parameters.Add (param);
  170. methodBegin.Parameters.Add (param);
  171. paramArray [n] = new CodeVariableReferenceExpression (param.Name);
  172. }
  173. bool isVoid = true;
  174. if (outputMember != null)
  175. {
  176. method.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  177. methodEnd.ReturnType = new CodeTypeReference (outputMember.TypeFullName);
  178. xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, outputMember, "");
  179. isVoid = false;
  180. }
  181. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
  182. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
  183. methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
  184. // Array of input parameters
  185. CodeArrayCreateExpression methodParams;
  186. if (paramArray.Length > 0)
  187. methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
  188. else
  189. methodParams = new CodeArrayCreateExpression (typeof(object), 0);
  190. // Generate method url
  191. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  192. CodeExpression thisURlExp = new CodeFieldReferenceExpression (ethis, "Url");
  193. CodePrimitiveExpression metUrl = new CodePrimitiveExpression (httpOper.Location);
  194. CodeBinaryOperatorExpression expMethodLocation = new CodeBinaryOperatorExpression (thisURlExp, CodeBinaryOperatorType.Add, metUrl);
  195. // Invoke call
  196. CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
  197. CodeMethodInvokeExpression inv;
  198. inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, expMethodLocation, methodParams);
  199. if (!isVoid)
  200. method.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (method.ReturnType, inv)));
  201. else
  202. method.Statements.Add (inv);
  203. // Begin Invoke Call
  204. CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
  205. CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
  206. inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, expMethodLocation, methodParams, expCallb, expAsyncs);
  207. methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
  208. // End Invoke call
  209. CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
  210. inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
  211. if (!isVoid)
  212. methodEnd.Statements.Add (new CodeMethodReturnStatement (new CodeCastExpression (methodEnd.ReturnType, inv)));
  213. else
  214. methodEnd.Statements.Add (inv);
  215. // Attributes
  216. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.HttpMethodAttribute");
  217. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetOutMimeFormatter ())));
  218. att.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(GetInMimeFormatter ())));
  219. AddCustomAttribute (method, att, true);
  220. CodeTypeDeclaration.Members.Add (method);
  221. CodeTypeDeclaration.Members.Add (methodBegin);
  222. CodeTypeDeclaration.Members.Add (methodEnd);
  223. return method;
  224. }
  225. protected virtual Type GetInMimeFormatter ()
  226. {
  227. return null;
  228. }
  229. protected virtual Type GetOutMimeFormatter ()
  230. {
  231. if (OperationBinding.Output.Extensions.Find (typeof(MimeXmlBinding)) != null)
  232. return typeof (XmlReturnReader);
  233. MimeContentBinding bin = (MimeContentBinding) OperationBinding.Output.Extensions.Find (typeof(MimeContentBinding));
  234. if (bin != null && bin.Type == "text/xml")
  235. return typeof (XmlReturnReader);
  236. return typeof(NopReturnReader);
  237. }
  238. string GetSimpleType (XmlMemberMapping member)
  239. {
  240. // MS seems to always use System.String for input parameters, except for byte[]
  241. switch (member.TypeName)
  242. {
  243. case "hexBinary":
  244. case "base64Binary":
  245. return "System.String";
  246. default:
  247. string ptype = member.TypeFullName;
  248. int i = ptype.IndexOf ('[');
  249. if (i == -1)
  250. return "System.String";
  251. else
  252. return "System.String" + ptype.Substring (i);
  253. }
  254. }
  255. #endregion
  256. }
  257. }