Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
11 KiB

  1. using System;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Web.Services;
  6. using System.Web.Services.Description;
  7. using System.Web.Services.Protocols;
  8. using System.Xml;
  9. using System.Xml.Serialization;
  10. using System.Xml.Xsl;
  11. using System.Xml.XPath;
  12. using System.Text;
  13. using Microsoft.Uddi;
  14. using Microsoft.Uddi.Authentication;
  15. using Microsoft.Uddi.VersionSupport;
  16. namespace Microsoft.Uddi.Web
  17. {
  18. /// <summary>
  19. /// UddiWebResponse allows us to return our own Stream object.
  20. /// </summary>
  21. internal class UddiWebResponse : WebResponse
  22. {
  23. private WebResponse innerWebResponse;
  24. private UddiResponseStream uddiResponseStream;
  25. private UddiVersion uddiVersion;
  26. /// <summary>
  27. /// Constructor
  28. /// </summary>
  29. /// <param name="innerWebResponse">This object should come from the WebResponse created by HttpSoapClientProtocol.</param>
  30. public UddiWebResponse( WebResponse innerWebResponse )
  31. {
  32. this.innerWebResponse = innerWebResponse;
  33. this.uddiVersion = uddiVersion;
  34. }
  35. /// <summary>
  36. /// Return our response stream (UddiResponseStream) instead of the Stream associated with our inner response.
  37. /// </summary>
  38. /// <returns></returns>
  39. public override Stream GetResponseStream()
  40. {
  41. if( null == uddiResponseStream )
  42. {
  43. uddiResponseStream = new UddiResponseStream( innerWebResponse.GetResponseStream() );
  44. }
  45. return uddiResponseStream;
  46. }
  47. /// <summary>
  48. /// Delegates to the inner web response.
  49. /// </summary>
  50. public override void Close()
  51. {
  52. innerWebResponse.Close();
  53. }
  54. /// <summary>
  55. /// Delegates to the inner web response.
  56. /// </summary>
  57. public override long ContentLength
  58. {
  59. get { return innerWebResponse.ContentLength; }
  60. set { innerWebResponse.ContentLength = value; }
  61. }
  62. /// <summary>
  63. /// Delegates to the inner web response.
  64. /// </summary>
  65. public override string ContentType
  66. {
  67. get { return innerWebResponse.ContentType; }
  68. set { innerWebResponse.ContentType = value; }
  69. }
  70. /// <summary>
  71. /// Delegates to the inner web response.
  72. /// </summary>
  73. public override Uri ResponseUri
  74. {
  75. get { return innerWebResponse.ResponseUri; }
  76. }
  77. /// <summary>
  78. /// Delegates to the inner web response.
  79. /// </summary>
  80. public override WebHeaderCollection Headers
  81. {
  82. get { return innerWebResponse.Headers; }
  83. }
  84. }
  85. /// <summary>
  86. /// UddiResponseStream allows us to manipulate the XML sent back from the web service.
  87. /// </summary>
  88. internal class UddiResponseStream : MemoryStream
  89. {
  90. //
  91. // TODO: at some point it may be necessary to pass in the current version as a parameter if the transforms become
  92. // more complicated.
  93. //
  94. /// <summary>
  95. /// Constructor. We read all the XML sent from the server, do our version manipulation, then write the new XML
  96. /// into our inner responseStream object.
  97. /// </summary>
  98. /// <param name="responseStream">This object should be the responseStream from a WebResponse object.</param>
  99. public UddiResponseStream( Stream responseStream )
  100. {
  101. try
  102. {
  103. //
  104. // Get the XML the server sent to us.
  105. //
  106. StreamReader reader = new StreamReader( responseStream );
  107. string responseXml = reader.ReadToEnd();
  108. reader.Close();
  109. //
  110. // TODO: Consider making this class more generic, ie. have a IUddiResponseHandler interface:
  111. // IUddiResponseHandler
  112. // string HandleResponse( string xml );
  113. //
  114. //
  115. // Translate the incoming XML into the current version.
  116. //
  117. string transformedXml = UddiVersionSupport.Translate( responseXml, UddiVersionSupport.CurrentVersion );
  118. //
  119. // Write this transformed XML into the 'real' stream.
  120. //
  121. StreamUtility.WriteStringToStream( this, transformedXml );
  122. }
  123. finally
  124. {
  125. this.Position = 0;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// UddiWebRequest allows us to return our own request and response objects.
  131. /// </summary>
  132. internal class UddiWebRequest : WebRequest
  133. {
  134. private WebRequest innerWebRequest;
  135. private UddiRequestStream uddiRequestStream;
  136. private UddiWebResponse uddiWebResponse;
  137. private UddiVersion uddiVersion;
  138. /// <summary>
  139. /// Constructor
  140. /// </summary>
  141. /// <param name="uri">Uri to the web service we are calling</param>
  142. /// <param name="uddiVersion">Uddi version to use for requests</param>
  143. public UddiWebRequest( WebRequest innerWebRequest, UddiVersion uddiVersion )
  144. {
  145. this.innerWebRequest = innerWebRequest;
  146. this.uddiVersion = uddiVersion;
  147. }
  148. /// <summary>
  149. /// Return a UddiRequestStream object instead of the default one.
  150. /// </summary>
  151. /// <returns>UddiRequestStream object</returns>
  152. public override Stream GetRequestStream()
  153. {
  154. if( null == uddiRequestStream )
  155. {
  156. uddiRequestStream = new UddiRequestStream( innerWebRequest.GetRequestStream(), uddiVersion );
  157. }
  158. return uddiRequestStream;
  159. }
  160. /// <summary>
  161. /// Return a UddiWebRequest object instead of the default one.
  162. /// </summary>
  163. /// <returns>UddiWebResponse object</returns>
  164. public override WebResponse GetResponse()
  165. {
  166. if( null == uddiWebResponse )
  167. {
  168. uddiWebResponse = new UddiWebResponse( innerWebRequest.GetResponse() );
  169. }
  170. return uddiWebResponse;
  171. }
  172. /// <summary>
  173. /// Delegates to our inner WebRequest
  174. /// </summary>
  175. public override string Method
  176. {
  177. get { return innerWebRequest.Method; }
  178. set { innerWebRequest.Method = value; }
  179. }
  180. /// <summary>
  181. /// Delegates to our inner WebRequest
  182. /// </summary>
  183. public override Uri RequestUri
  184. {
  185. get { return innerWebRequest.RequestUri; }
  186. }
  187. /// <summary>
  188. /// Delegates to our inner WebRequest
  189. /// </summary>
  190. public override string ConnectionGroupName
  191. {
  192. get { return innerWebRequest.ConnectionGroupName; }
  193. set { innerWebRequest.ConnectionGroupName = value; }
  194. }
  195. /// <summary>
  196. /// Delegates to our inner WebRequest
  197. /// </summary>
  198. public override WebHeaderCollection Headers
  199. {
  200. get { return innerWebRequest.Headers; }
  201. set { innerWebRequest.Headers = value; }
  202. }
  203. /// <summary>
  204. /// Delegates to our inner WebRequest
  205. /// </summary>
  206. public override long ContentLength
  207. {
  208. get { return innerWebRequest.ContentLength; }
  209. set { innerWebRequest.ContentLength = value; }
  210. }
  211. /// <summary>
  212. /// Delegates to our inner WebRequest
  213. /// </summary>
  214. public override string ContentType
  215. {
  216. get { return innerWebRequest.ContentType; }
  217. set { innerWebRequest.ContentType = value; }
  218. }
  219. /// <summary>
  220. /// Delegates to our inner WebRequest
  221. /// </summary>
  222. public override ICredentials Credentials
  223. {
  224. get { return innerWebRequest.Credentials; }
  225. set { innerWebRequest.Credentials = value; }
  226. }
  227. /// <summary>
  228. /// Delegates to our inner WebRequest
  229. /// </summary>
  230. public override IWebProxy Proxy
  231. {
  232. get { return innerWebRequest.Proxy; }
  233. set { innerWebRequest.Proxy = value; }
  234. }
  235. /// <summary>
  236. /// Delegates to our inner WebRequest
  237. /// </summary>
  238. public override bool PreAuthenticate
  239. {
  240. get { return innerWebRequest.PreAuthenticate; }
  241. set { innerWebRequest.PreAuthenticate = value; }
  242. }
  243. /// <summary>
  244. /// Delegates to our inner WebRequest
  245. /// </summary>
  246. public override int Timeout
  247. {
  248. get { return innerWebRequest.Timeout; }
  249. set { innerWebRequest.Timeout = value; }
  250. }
  251. /// <summary>
  252. /// Delegates to our inner WebRequest
  253. /// </summary>
  254. public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
  255. {
  256. return innerWebRequest.BeginGetResponse( callback, state );
  257. }
  258. /// <summary>
  259. /// Delegates to our inner WebRequest
  260. /// </summary>
  261. public override WebResponse EndGetResponse(IAsyncResult asyncResult)
  262. {
  263. return innerWebRequest.EndGetResponse( asyncResult );
  264. }
  265. /// <summary>
  266. /// Delegates to our inner WebRequest
  267. /// </summary>
  268. public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, Object state)
  269. {
  270. return innerWebRequest.BeginGetRequestStream( callback, state );
  271. }
  272. /// <summary>
  273. /// Delegates to our inner WebRequest
  274. /// </summary>
  275. public override Stream EndGetRequestStream(IAsyncResult asyncResult)
  276. {
  277. return innerWebRequest.EndGetRequestStream( asyncResult );
  278. }
  279. /// <summary>
  280. /// Delegates to our inner WebRequest
  281. /// </summary>
  282. public override void Abort()
  283. {
  284. innerWebRequest.Abort();
  285. }
  286. }
  287. /// <summary>
  288. /// UddiRequestStream allows us to manipulate the XML before we send it to the client. This class will accept all data that
  289. /// is written to it from the ASP.NET web service framework. When the framework closes the stream (ie. wants to send the data), we
  290. /// will manipulate this XML so that it has the right Uddi version, then send it out using our innerStream object.
  291. /// </summary>
  292. internal class UddiRequestStream : MemoryStream
  293. {
  294. private Stream innerStream;
  295. private UddiVersion uddiVersion;
  296. /// <summary>
  297. /// Constructor
  298. /// </summary>
  299. /// <param name="innerStream">Should be from a WebRequest object.</param>
  300. /// <param name="uddiVersion">The UDD version we should use to send to the server</param>
  301. public UddiRequestStream( Stream innerStream, UddiVersion uddiVersion )
  302. {
  303. this.innerStream = innerStream;
  304. this.uddiVersion = uddiVersion;
  305. }
  306. /// <summary>
  307. /// Before we actually close the request stream, we want to manipulate the XML.
  308. /// </summary>
  309. public override void Close()
  310. {
  311. try
  312. {
  313. //
  314. // Rewind ourselves.
  315. //
  316. this.Position = 0;
  317. //
  318. // Read the XML that was written; this is the XML request that will be sent to the Uddi server.
  319. //
  320. StreamReader reader = new StreamReader( this );
  321. string requestXml = reader.ReadToEnd();
  322. //
  323. // TODO: Consider making this class more generic, ie. have a IUddiRequestHandler interface:
  324. // IUddiResponseHandler
  325. // string HandleRequest( string xml );
  326. //
  327. //
  328. // Transform the content to a correct version on the server if we are not using the current version.
  329. //
  330. string transformedXml = requestXml;
  331. if( uddiVersion != UddiVersionSupport.CurrentVersion )
  332. {
  333. transformedXml = UddiVersionSupport.Translate( requestXml, uddiVersion );
  334. }
  335. //
  336. // Write the transformed data to the 'real' stream.
  337. //
  338. StreamUtility.WriteStringToStream( innerStream, transformedXml );
  339. }
  340. finally
  341. {
  342. //
  343. // Make sure we clean up properly.
  344. //
  345. innerStream.Close();
  346. base.Close();
  347. }
  348. }
  349. }
  350. /// <summary>
  351. /// Simple utility class to help us write string data to Stream objects.
  352. /// </summary>
  353. internal sealed class StreamUtility
  354. {
  355. public static void WriteStringToStream( Stream stream, string stringToWrite )
  356. {
  357. for ( int i = 0; i < stringToWrite.Length; i++ )
  358. {
  359. stream.WriteByte( Convert.ToByte( stringToWrite[ i ] ) );
  360. }
  361. }
  362. private StreamUtility()
  363. {
  364. //
  365. // Don't let anyone instantiate this class
  366. //
  367. }
  368. }
  369. }