WebServiceHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // System.Web.Services.Protocols.WebServiceHelper.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  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. #if USE_DEPRECATED
  30. using System;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Xml.Serialization;
  37. using System.Web.Services.Protocols;
  38. using System.Web.Services.Description;
  39. using System.Collections.Generic;
  40. using System.ServiceModel.Channels;
  41. using System.ServiceModel.Description;
  42. namespace System.ServiceModel.Description
  43. {
  44. //Original file: WebServiceHelper.cs
  45. internal class WebServiceHelper
  46. {
  47. public const string SoapEnvelopeNamespace = "http://www.w3.org/2003/05/soap-envelope";
  48. public const string AddressingNamespace = "http://www.w3.org/2005/08/addressing";
  49. public static MetadataSet ReadTransferGetResponse (XmlTextReader xmlReader, SoapHeaderDirection dir,
  50. out SoapHeaderCollection headers, out AddressHeaderCollection addressHeaders)
  51. {
  52. SoapReflectionImporter sri = new SoapReflectionImporter ();
  53. XmlSerializer bodySerializer = new XmlSerializer (sri.ImportTypeMapping (typeof (SoapHeader)));
  54. xmlReader.MoveToContent ();
  55. xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  56. headers = ReadHeaders (xmlReader, out addressHeaders);
  57. xmlReader.MoveToContent ();
  58. xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
  59. /*if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
  60. bodySerializer = Fault.Serializer;
  61. body = bodySerializer.Deserialize (xmlReader);*/
  62. MetadataSet ms = MetadataSet.ReadFrom (xmlReader);
  63. return ms;
  64. }
  65. static SoapHeaderCollection ReadHeaders (XmlTextReader xmlReader, out AddressHeaderCollection addressHeaders)
  66. {
  67. SoapHeaderCollection headers = new SoapHeaderCollection ();
  68. List<AddressHeader> addressList = new List<AddressHeader> ();
  69. xmlReader.Read ();
  70. while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" &&
  71. xmlReader.NamespaceURI == SoapEnvelopeNamespace))
  72. {
  73. /* FIXME: Use some deserializer for AddressHeaders,
  74. * EndpointReference is a complex header..
  75. * Validate 'Action', 'RelatesTo'
  76. */
  77. if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.NamespaceURI == AddressingNamespace &&
  78. !xmlReader.IsEmptyElement)
  79. {
  80. //WS-Addressing headers
  81. /* FIXME: attributes?
  82. while (xmlReader.MoveToNextAttribute ()) {
  83. }*/
  84. xmlReader.ReadStartElement ();
  85. xmlReader.MoveToContent ();
  86. string content = xmlReader.ReadContentAsString ();
  87. //FIXME: Use AddressHeaderSerializer
  88. AddressHeader ah = AddressHeader.CreateAddressHeader (xmlReader.Name, AddressingNamespace,
  89. content);
  90. addressList.Add (ah);
  91. xmlReader.ReadEndElement ();
  92. }
  93. else
  94. //FIXME: Other headers?
  95. xmlReader.Skip ();
  96. }
  97. addressHeaders = new AddressHeaderCollection (addressList);
  98. return headers;
  99. }
  100. class HeaderSerializationHelper
  101. {
  102. SoapHeaderCollection headers;
  103. XmlSerializer headerSerializer;
  104. public HeaderSerializationHelper (XmlSerializer headerSerializer)
  105. {
  106. this.headers = new SoapHeaderCollection ();
  107. this.headerSerializer = headerSerializer;
  108. }
  109. public SoapHeaderCollection Deserialize (XmlTextReader xmlReader)
  110. {
  111. try {
  112. headerSerializer.UnknownElement += new XmlElementEventHandler (OnAddUnknownHeader);
  113. object[] headerArray = (object[]) headerSerializer.Deserialize (xmlReader);
  114. foreach (SoapHeader h in headerArray)
  115. if (h != null) headers.Add (h);
  116. return headers;
  117. } finally {
  118. headerSerializer.UnknownElement -= new XmlElementEventHandler (OnAddUnknownHeader);
  119. }
  120. }
  121. void OnAddUnknownHeader (object sender, XmlElementEventArgs e)
  122. {
  123. SoapUnknownHeader su = new SoapUnknownHeader ();
  124. su.Element = e.Element;
  125. headers.Add (su);
  126. }
  127. }
  128. public static void InvalidOperation (string message, WebResponse response, Encoding enc)
  129. {
  130. if (response == null)
  131. throw new InvalidOperationException (message);
  132. if (enc == null)
  133. enc = Encoding.UTF8;
  134. StringBuilder sb = new StringBuilder ();
  135. sb.Append (message);
  136. if (response.ContentLength > 0) {
  137. sb.Append ("\r\nResponse error message:\r\n--\r\n");
  138. try {
  139. StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
  140. sb.Append (resp.ReadToEnd ());
  141. } catch (Exception) {
  142. }
  143. }
  144. throw new InvalidOperationException (sb.ToString ());
  145. }
  146. }
  147. }
  148. #endif