ProtocolReflector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // System.Web.Services.Description.ProtocolReflector.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  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:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  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.
  29. //
  30. using System.Web.Services;
  31. using System.Web.Services.Protocols;
  32. using System.Xml.Serialization;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Collections;
  36. namespace System.Web.Services.Description {
  37. public abstract class ProtocolReflector {
  38. #region Fields
  39. Binding binding;
  40. string defaultNamespace;
  41. MessageCollection headerMessages;
  42. Message inputMessage;
  43. LogicalMethodInfo[] methods;
  44. Operation operation;
  45. OperationBinding operationBinding;
  46. Message outputMessage;
  47. Port port;
  48. PortType portType;
  49. string protocolName;
  50. XmlSchemaExporter schemaExporter;
  51. Service service;
  52. ServiceDescription serviceDescription;
  53. Type serviceType;
  54. string serviceUrl;
  55. SoapSchemaExporter soapSchemaExporter;
  56. MethodStubInfo methodStubInfo;
  57. TypeStubInfo typeInfo;
  58. ArrayList extensionReflectors;
  59. ServiceDescriptionReflector serviceReflector;
  60. XmlReflectionImporter reflectionImporter;
  61. SoapReflectionImporter soapReflectionImporter;
  62. CodeIdentifiers portNames;
  63. #endregion // Fields
  64. #region Constructors
  65. protected ProtocolReflector ()
  66. {
  67. defaultNamespace = WebServiceAttribute.DefaultNamespace;
  68. extensionReflectors = ExtensionManager.BuildExtensionReflectors ();
  69. }
  70. #endregion // Constructors
  71. #region Properties
  72. internal ServiceDescriptionReflector Parent {
  73. get { return serviceReflector; }
  74. }
  75. public Binding Binding {
  76. get { return binding; }
  77. }
  78. public string DefaultNamespace {
  79. get { return defaultNamespace; }
  80. }
  81. public MessageCollection HeaderMessages {
  82. get { return headerMessages; } // TODO: set
  83. }
  84. public Message InputMessage {
  85. get { return inputMessage; }
  86. }
  87. public LogicalMethodInfo Method {
  88. get { return methodStubInfo.MethodInfo; }
  89. }
  90. public WebMethodAttribute MethodAttribute {
  91. get { return methodStubInfo.MethodAttribute; }
  92. }
  93. public LogicalMethodInfo[] Methods {
  94. get { return typeInfo.LogicalType.LogicalMethods; }
  95. }
  96. public Operation Operation {
  97. get { return operation; }
  98. }
  99. public OperationBinding OperationBinding {
  100. get { return operationBinding; }
  101. }
  102. public Message OutputMessage {
  103. get { return outputMessage; }
  104. }
  105. public Port Port {
  106. get { return port; }
  107. }
  108. public PortType PortType {
  109. get { return portType; }
  110. }
  111. public abstract string ProtocolName {
  112. get;
  113. }
  114. public XmlReflectionImporter ReflectionImporter
  115. {
  116. get
  117. {
  118. if (reflectionImporter == null) {
  119. reflectionImporter = typeInfo.XmlImporter;
  120. if (reflectionImporter == null)
  121. reflectionImporter = new XmlReflectionImporter();
  122. }
  123. return reflectionImporter;
  124. }
  125. }
  126. internal SoapReflectionImporter SoapReflectionImporter
  127. {
  128. get
  129. {
  130. if (soapReflectionImporter == null) {
  131. soapReflectionImporter = typeInfo.SoapImporter;
  132. if (soapReflectionImporter == null)
  133. soapReflectionImporter = new SoapReflectionImporter();
  134. }
  135. return soapReflectionImporter;
  136. }
  137. }
  138. public XmlSchemaExporter SchemaExporter {
  139. get { return schemaExporter; }
  140. }
  141. internal SoapSchemaExporter SoapSchemaExporter {
  142. get { return soapSchemaExporter; }
  143. }
  144. public XmlSchemas Schemas {
  145. get { return serviceReflector.Schemas; }
  146. }
  147. public Service Service {
  148. get { return service; }
  149. }
  150. public ServiceDescription ServiceDescription {
  151. get { return serviceDescription; }
  152. }
  153. public ServiceDescriptionCollection ServiceDescriptions {
  154. get { return serviceReflector.ServiceDescriptions; }
  155. }
  156. public Type ServiceType {
  157. get { return serviceType; }
  158. }
  159. public string ServiceUrl {
  160. get { return serviceUrl; }
  161. }
  162. internal MethodStubInfo MethodStubInfo {
  163. get { return methodStubInfo; }
  164. }
  165. internal TypeStubInfo TypeInfo {
  166. get { return typeInfo; }
  167. }
  168. #endregion // Properties
  169. #region Methods
  170. internal void Reflect (ServiceDescriptionReflector serviceReflector, Type type, string url, XmlSchemaExporter xxporter, SoapSchemaExporter sxporter)
  171. {
  172. portNames = new CodeIdentifiers ();
  173. this.serviceReflector = serviceReflector;
  174. serviceUrl = url;
  175. serviceType = type;
  176. schemaExporter = xxporter;
  177. soapSchemaExporter = sxporter;
  178. typeInfo = TypeStubManager.GetTypeStub (type, ProtocolName);
  179. ServiceDescription desc = ServiceDescriptions [typeInfo.LogicalType.WebServiceNamespace];
  180. if (desc == null)
  181. {
  182. desc = new ServiceDescription ();
  183. desc.TargetNamespace = typeInfo.LogicalType.WebServiceNamespace;
  184. desc.Name = typeInfo.LogicalType.WebServiceName;
  185. ServiceDescriptions.Add (desc);
  186. }
  187. ImportService (desc, typeInfo, url);
  188. }
  189. void ImportService (ServiceDescription desc, TypeStubInfo typeInfo, string url)
  190. {
  191. service = desc.Services [typeInfo.LogicalType.WebServiceName];
  192. if (service == null)
  193. {
  194. service = new Service ();
  195. service.Name = typeInfo.LogicalType.WebServiceName;
  196. service.Documentation = typeInfo.LogicalType.Description;
  197. desc.Services.Add (service);
  198. }
  199. foreach (BindingInfo binfo in typeInfo.Bindings)
  200. ImportBinding (desc, service, typeInfo, url, binfo);
  201. }
  202. void ImportBinding (ServiceDescription desc, Service service, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  203. {
  204. port = new Port ();
  205. port.Name = portNames.AddUnique (binfo.Name, port);
  206. bool bindingFull = true;
  207. if (binfo.Namespace != desc.TargetNamespace)
  208. {
  209. if (binfo.Location == null || binfo.Location == string.Empty)
  210. {
  211. ServiceDescription newDesc = new ServiceDescription();
  212. newDesc.TargetNamespace = binfo.Namespace;
  213. newDesc.Name = binfo.Name;
  214. bindingFull = ImportBindingContent (newDesc, typeInfo, url, binfo);
  215. if (bindingFull) {
  216. int id = ServiceDescriptions.Add (newDesc);
  217. AddImport (desc, binfo.Namespace, GetWsdlUrl (url,id));
  218. }
  219. }
  220. else {
  221. AddImport (desc, binfo.Namespace, binfo.Location);
  222. bindingFull = true;
  223. }
  224. }
  225. else
  226. bindingFull = ImportBindingContent (desc, typeInfo, url, binfo);
  227. if (bindingFull)
  228. {
  229. port.Binding = new XmlQualifiedName (binding.Name, binfo.Namespace);
  230. int n = 0;
  231. string name = binfo.Name;
  232. bool found;
  233. do {
  234. found = false;
  235. foreach (Port p in service.Ports)
  236. if (p.Name == name) { found = true; n++; name = binfo.Name + n; break; }
  237. }
  238. while (found);
  239. port.Name = name;
  240. service.Ports.Add (port);
  241. }
  242. #if NET_2_0
  243. if (binfo.WebServiceBindingAttribute != null && binfo.WebServiceBindingAttribute.ConformsTo != WsiProfiles.None && String.IsNullOrEmpty (binfo.WebServiceBindingAttribute.Name)) {
  244. BasicProfileViolationCollection violations = new BasicProfileViolationCollection ();
  245. desc.Types.Schemas.Add (Schemas);
  246. ServiceDescriptionCollection col = new ServiceDescriptionCollection ();
  247. col.Add (desc);
  248. ConformanceCheckContext ctx = new ConformanceCheckContext (col, violations);
  249. ctx.ServiceDescription = desc;
  250. ConformanceChecker[] checkers = WebServicesInteroperability.GetCheckers (binfo.WebServiceBindingAttribute.ConformsTo);
  251. foreach (ConformanceChecker checker in checkers) {
  252. ctx.Checker = checker;
  253. WebServicesInteroperability.Check (ctx, checker, binding);
  254. if (violations.Count > 0)
  255. throw new InvalidOperationException (violations [0].ToString ());
  256. }
  257. }
  258. #endif
  259. }
  260. bool ImportBindingContent (ServiceDescription desc, TypeStubInfo typeInfo, string url, BindingInfo binfo)
  261. {
  262. serviceDescription = desc;
  263. // Look for an unused name
  264. int n=0;
  265. string name = binfo.Name;
  266. bool found;
  267. do
  268. {
  269. found = false;
  270. foreach (Binding bi in desc.Bindings)
  271. if (bi.Name == name) { found = true; n++; name = binfo.Name+n; break; }
  272. }
  273. while (found);
  274. // Create the binding
  275. binding = new Binding ();
  276. binding.Name = name;
  277. binding.Type = new XmlQualifiedName (binding.Name, binfo.Namespace);
  278. #if NET_2_0
  279. if (binfo.WebServiceBindingAttribute != null && binfo.WebServiceBindingAttribute.EmitConformanceClaims) {
  280. XmlDocument doc = new XmlDocument ();
  281. XmlElement docElement = doc.CreateElement ("wsdl", "documentation", "http://schemas.xmlsoap.org/wsdl/");
  282. XmlElement claimsElement = doc.CreateElement ("wsi", "Claim", "http://ws-i.org/schemas/conformanceClaim/");
  283. claimsElement.Attributes.Append (doc.CreateAttribute ("conformsTo")).Value = "http://ws-i.org/profiles/basic/1.1";
  284. docElement.AppendChild (claimsElement);
  285. binding.DocumentationElement = docElement;
  286. }
  287. #endif
  288. portType = new PortType ();
  289. portType.Name = binding.Name;
  290. BeginClass ();
  291. foreach (SoapExtensionReflector reflector in extensionReflectors)
  292. {
  293. reflector.ReflectionContext = this;
  294. reflector.ReflectDescription ();
  295. }
  296. foreach (MethodStubInfo method in typeInfo.Methods)
  297. {
  298. methodStubInfo = method;
  299. string metBinding = ReflectMethodBinding ();
  300. if (typeInfo.GetBinding (metBinding) != binfo) continue;
  301. operation = new Operation ();
  302. operation.Name = method.OperationName;
  303. operation.Documentation = method.MethodAttribute.Description;
  304. // FIXME: SOAP 1.1 and SOAP 1.2 should share
  305. // the same message definitions.
  306. inputMessage = new Message ();
  307. inputMessage.Name = method.Name + ProtocolName + "In";
  308. ServiceDescription.Messages.Add (inputMessage);
  309. outputMessage = new Message ();
  310. outputMessage.Name = method.Name + ProtocolName + "Out";
  311. ServiceDescription.Messages.Add (outputMessage);
  312. OperationInput inOp = new OperationInput ();
  313. if (method.Name != method.OperationName) inOp.Name = method.Name;
  314. Operation.Messages.Add (inOp);
  315. inOp.Message = new XmlQualifiedName (inputMessage.Name, ServiceDescription.TargetNamespace);
  316. OperationOutput outOp = new OperationOutput ();
  317. if (method.Name != method.OperationName) outOp.Name = method.Name;
  318. Operation.Messages.Add (outOp);
  319. outOp.Message = new XmlQualifiedName (outputMessage.Name, ServiceDescription.TargetNamespace);
  320. portType.Operations.Add (operation);
  321. ImportOperationBinding ();
  322. if (!ReflectMethod ()) {
  323. #if NET_2_0
  324. // (It is somewhat hacky) If we don't
  325. // add input/output Messages, update
  326. // portType/input/@message and
  327. // porttype/output/@message.
  328. Message dupIn = Parent.MappedMessagesIn [method.MethodInfo];
  329. ServiceDescription.Messages.Remove (inputMessage);
  330. inOp.Message = new XmlQualifiedName (dupIn.Name, ServiceDescription.TargetNamespace);
  331. Message dupOut = Parent.MappedMessagesOut [method.MethodInfo];
  332. ServiceDescription.Messages.Remove (outputMessage);
  333. outOp.Message = new XmlQualifiedName (dupOut.Name, ServiceDescription.TargetNamespace);
  334. #endif
  335. }
  336. foreach (SoapExtensionReflector reflector in extensionReflectors)
  337. {
  338. reflector.ReflectionContext = this;
  339. reflector.ReflectMethod ();
  340. }
  341. }
  342. EndClass ();
  343. if (portType.Operations.Count > 0)
  344. {
  345. desc.Bindings.Add (binding);
  346. desc.PortTypes.Add (portType);
  347. return true;
  348. }
  349. else
  350. return false;
  351. }
  352. void ImportOperationBinding ()
  353. {
  354. operationBinding = new OperationBinding ();
  355. operationBinding.Name = methodStubInfo.OperationName;
  356. InputBinding inOp = new InputBinding ();
  357. operationBinding.Input = inOp;
  358. OutputBinding outOp = new OutputBinding ();
  359. operationBinding.Output = outOp;
  360. if (methodStubInfo.OperationName != methodStubInfo.Name)
  361. inOp.Name = outOp.Name = methodStubInfo.Name;
  362. binding.Operations.Add (operationBinding);
  363. }
  364. internal static void AddImport (ServiceDescription desc, string ns, string location)
  365. {
  366. Import im = new Import();
  367. im.Namespace = ns;
  368. im.Location = location;
  369. desc.Imports.Add (im);
  370. }
  371. string GetWsdlUrl (string baseUrl, int id)
  372. {
  373. return baseUrl + "?wsdl=" + id;
  374. }
  375. protected virtual void BeginClass ()
  376. {
  377. }
  378. protected virtual void EndClass ()
  379. {
  380. }
  381. public ServiceDescription GetServiceDescription (string ns)
  382. {
  383. return ServiceDescriptions [ns];
  384. }
  385. protected abstract bool ReflectMethod ();
  386. protected virtual string ReflectMethodBinding ()
  387. {
  388. return null;
  389. }
  390. #if NET_2_0
  391. [MonoNotSupported("Not Implemented")]
  392. protected virtual void ReflectDescription ()
  393. {
  394. throw new NotImplementedException ();
  395. }
  396. #endif
  397. #endregion
  398. }
  399. }