InterceptorBindingElement.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. //
  2. // InterceptorBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.ObjectModel;
  30. using System.Net;
  31. using System.Net.Security;
  32. using System.Security.Principal;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. using System.ServiceModel.Security;
  38. using System.ServiceModel.Security.Tokens;
  39. using System.Security.Cryptography.Xml;
  40. using System.Threading;
  41. using NUnit.Framework;
  42. using MonoTests.System.ServiceModel.Channels;
  43. namespace MonoTests.System.ServiceModel.Channels
  44. {
  45. public delegate void InterceptorRequestContextHandler (MessageBuffer src);
  46. class InterceptorBindingElement : BindingElement
  47. {
  48. InterceptorRequestContextHandler handler;
  49. public InterceptorBindingElement (InterceptorRequestContextHandler handler)
  50. {
  51. this.handler = handler;
  52. }
  53. public InterceptorRequestContextHandler Handler {
  54. get { return handler; }
  55. }
  56. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  57. {
  58. return true;
  59. }
  60. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  61. {
  62. return true;
  63. }
  64. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (BindingContext context)
  65. {
  66. return new InterceptorChannelListener<TChannel> (this, context);
  67. }
  68. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
  69. {
  70. return new InterceptorChannelFactory<TChannel> (this, context);
  71. }
  72. public override T GetProperty<T> (BindingContext ctx)
  73. {
  74. return ctx.GetInnerProperty<T> ();
  75. }
  76. public override BindingElement Clone ()
  77. {
  78. return this;
  79. }
  80. }
  81. abstract class DefaultTimeoutCommunicationObject : CommunicationObject
  82. {
  83. IDefaultCommunicationTimeouts timeouts;
  84. public DefaultTimeoutCommunicationObject (IDefaultCommunicationTimeouts timeouts)
  85. {
  86. this.timeouts = timeouts;
  87. }
  88. protected override TimeSpan DefaultOpenTimeout {
  89. get { return timeouts.OpenTimeout; }
  90. }
  91. protected override TimeSpan DefaultCloseTimeout {
  92. get { return timeouts.CloseTimeout; }
  93. }
  94. }
  95. class InterceptorChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  96. {
  97. InterceptorBindingElement element;
  98. IChannelFactory<TChannel> inner;
  99. public InterceptorChannelFactory (InterceptorBindingElement element, BindingContext context)
  100. : base (null)
  101. {
  102. this.element = element;
  103. inner = context.BuildInnerChannelFactory<TChannel> ();
  104. }
  105. public InterceptorBindingElement Element {
  106. get { return element; }
  107. }
  108. public override T GetProperty<T> ()
  109. {
  110. return inner.GetProperty<T> ();
  111. }
  112. protected override TChannel OnCreateChannel (EndpointAddress address, Uri uri)
  113. {
  114. if (typeof (TChannel) == typeof (IRequestChannel))
  115. return (TChannel) (object) new InterceptorRequestChannel (
  116. (InterceptorChannelFactory<IRequestChannel>) (object) this,
  117. (IRequestChannel) (object) inner.CreateChannel (address, uri));
  118. throw new NotImplementedException ();
  119. }
  120. protected override void OnOpen (TimeSpan timeout)
  121. {
  122. inner.Open (timeout);
  123. }
  124. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  125. {
  126. return inner.BeginOpen (timeout, callback, state);
  127. }
  128. protected override void OnEndOpen (IAsyncResult result)
  129. {
  130. inner.EndOpen (result);
  131. }
  132. protected override void OnClose (TimeSpan timeout)
  133. {
  134. inner.Close (timeout);
  135. }
  136. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  137. {
  138. return inner.BeginClose (timeout, callback, state);
  139. }
  140. protected override void OnEndClose (IAsyncResult result)
  141. {
  142. inner.EndClose (result);
  143. }
  144. protected override void OnAbort ()
  145. {
  146. inner.Abort ();
  147. }
  148. }
  149. class InterceptorChannelListener<TChannel> : ChannelListenerBase<TChannel> where TChannel : class, IChannel
  150. {
  151. InterceptorBindingElement element;
  152. IChannelListener<TChannel> inner;
  153. public InterceptorChannelListener (InterceptorBindingElement element, BindingContext context)
  154. {
  155. this.element = element;
  156. inner = context.BuildInnerChannelListener<TChannel> ();
  157. }
  158. public InterceptorBindingElement Element {
  159. get { return element; }
  160. }
  161. public override Uri Uri {
  162. get { return inner.Uri; }
  163. }
  164. protected override TChannel OnAcceptChannel (TimeSpan timeout)
  165. {
  166. if (typeof (TChannel) == typeof (IReplyChannel))
  167. return (TChannel) (object) new InterceptorReplyChannel (
  168. (InterceptorChannelListener<IReplyChannel>) (object) this,
  169. (IReplyChannel) (object) inner.AcceptChannel (timeout));
  170. throw new NotImplementedException ();
  171. }
  172. protected override IAsyncResult OnBeginAcceptChannel (
  173. TimeSpan timeout, AsyncCallback callback, object state)
  174. {
  175. return inner.BeginAcceptChannel (timeout, callback, state);
  176. }
  177. protected override TChannel OnEndAcceptChannel (IAsyncResult result)
  178. {
  179. if (typeof (TChannel) == typeof (IReplyChannel))
  180. return (TChannel) (object) new InterceptorReplyChannel (
  181. (InterceptorChannelListener<IReplyChannel>) (object) this,
  182. (IReplyChannel) (object) inner.EndAcceptChannel (result));
  183. throw new NotImplementedException ();
  184. }
  185. protected override bool OnWaitForChannel (TimeSpan timeout)
  186. {
  187. return inner.WaitForChannel (timeout);
  188. }
  189. protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
  190. {
  191. return inner.BeginWaitForChannel (timeout, callback, state);
  192. }
  193. protected override bool OnEndWaitForChannel (IAsyncResult result)
  194. {
  195. return inner.EndWaitForChannel (result);
  196. }
  197. protected override void OnAbort ()
  198. {
  199. inner.Abort ();
  200. }
  201. protected override void OnOpen (TimeSpan timeout)
  202. {
  203. inner.Open (timeout);
  204. }
  205. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  206. {
  207. return inner.BeginOpen (timeout, callback, state);
  208. }
  209. protected override void OnEndOpen (IAsyncResult result)
  210. {
  211. inner.EndOpen (result);
  212. }
  213. protected override void OnClose (TimeSpan timeout)
  214. {
  215. inner.Close (timeout);
  216. }
  217. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  218. {
  219. return inner.BeginClose (timeout, callback, state);
  220. }
  221. protected override void OnEndClose (IAsyncResult result)
  222. {
  223. inner.EndClose (result);
  224. }
  225. }
  226. class InterceptorRequestChannel : ChannelBase, IRequestChannel
  227. {
  228. InterceptorChannelFactory<IRequestChannel> source;
  229. IRequestChannel inner;
  230. public InterceptorRequestChannel (InterceptorChannelFactory<IRequestChannel> source, IRequestChannel inner)
  231. : base (source)
  232. {
  233. this.source = source;
  234. this.inner = inner;
  235. }
  236. public EndpointAddress RemoteAddress {
  237. get { return inner.RemoteAddress; }
  238. }
  239. public Uri Via {
  240. get { return inner.Via; }
  241. }
  242. public Message Request (Message message)
  243. {
  244. return inner.Request (message);
  245. }
  246. public Message Request (Message message, TimeSpan timeout)
  247. {
  248. return inner.Request (message, timeout);
  249. }
  250. public IAsyncResult BeginRequest (Message message, AsyncCallback callback, object state)
  251. {
  252. return inner.BeginRequest (message, callback, state);
  253. }
  254. public IAsyncResult BeginRequest (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  255. {
  256. return inner.BeginRequest (message, timeout, callback, state);
  257. }
  258. public Message EndRequest (IAsyncResult result)
  259. {
  260. return inner.EndRequest (result);
  261. }
  262. protected override void OnAbort ()
  263. {
  264. inner.Abort ();
  265. }
  266. protected override void OnOpen (TimeSpan timeout)
  267. {
  268. inner.Open (timeout);
  269. }
  270. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  271. {
  272. return inner.BeginOpen (timeout, callback, state);
  273. }
  274. protected override void OnEndOpen (IAsyncResult result)
  275. {
  276. inner.EndOpen (result);
  277. }
  278. protected override void OnClose (TimeSpan timeout)
  279. {
  280. inner.Close (timeout);
  281. }
  282. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  283. {
  284. return inner.BeginClose (timeout, callback, state);
  285. }
  286. protected override void OnEndClose (IAsyncResult result)
  287. {
  288. inner.EndClose (result);
  289. }
  290. }
  291. class InterceptorReplyChannel : ReplyChannelBase
  292. {
  293. InterceptorChannelListener<IReplyChannel> source;
  294. IReplyChannel inner;
  295. public InterceptorReplyChannel (InterceptorChannelListener<IReplyChannel> source, IReplyChannel inner)
  296. : base (source)
  297. {
  298. this.source = source;
  299. this.inner = inner;
  300. }
  301. public override EndpointAddress LocalAddress {
  302. get { return inner.LocalAddress; }
  303. }
  304. RequestContext Inspect (RequestContext src)
  305. {
  306. CopyRequestContext ret = new CopyRequestContext (src);
  307. source.Element.Handler (ret.Buffer);
  308. return ret;
  309. }
  310. public override RequestContext ReceiveRequest ()
  311. {
  312. return Inspect (inner.ReceiveRequest ());
  313. }
  314. public override RequestContext ReceiveRequest (TimeSpan timeout)
  315. {
  316. return Inspect (inner.ReceiveRequest (timeout));
  317. }
  318. public override IAsyncResult BeginReceiveRequest (AsyncCallback callback, object state)
  319. {
  320. return inner.BeginReceiveRequest (callback, state);
  321. }
  322. public override IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  323. {
  324. return inner.BeginReceiveRequest (timeout, callback, state);
  325. }
  326. public override RequestContext EndReceiveRequest (IAsyncResult result)
  327. {
  328. return Inspect (inner.EndReceiveRequest (result));
  329. }
  330. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  331. {
  332. if (inner.TryReceiveRequest (timeout, out context)) {
  333. context = Inspect (context);
  334. return true;
  335. }
  336. context = null;
  337. return false;
  338. }
  339. public override IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
  340. {
  341. return inner.BeginTryReceiveRequest (timeout, callback, state);
  342. }
  343. public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
  344. {
  345. if (inner.EndTryReceiveRequest (result, out context)) {
  346. context = Inspect (context);
  347. return true;
  348. }
  349. context = null;
  350. return false;
  351. }
  352. public override bool WaitForRequest (TimeSpan timeout)
  353. {
  354. return inner.WaitForRequest (timeout);
  355. }
  356. public override IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
  357. {
  358. return inner.BeginWaitForRequest (timeout, callback, state);
  359. }
  360. public override bool EndWaitForRequest (IAsyncResult result)
  361. {
  362. return inner.EndWaitForRequest (result);
  363. }
  364. protected override void OnAbort ()
  365. {
  366. inner.Abort ();
  367. }
  368. protected override void OnClose (TimeSpan timeout)
  369. {
  370. inner.Close (timeout);
  371. }
  372. protected override void OnOpen (TimeSpan timeout)
  373. {
  374. inner.Open (timeout);
  375. }
  376. }
  377. class CopyRequestContext : RequestContext
  378. {
  379. RequestContext src;
  380. MessageBuffer buffer;
  381. Message copy_msg;
  382. public CopyRequestContext (RequestContext src)
  383. {
  384. this.src = src;
  385. buffer = src.RequestMessage.CreateBufferedCopy (0x10000);
  386. copy_msg = buffer.CreateMessage ();
  387. }
  388. public MessageBuffer Buffer {
  389. get { return buffer; }
  390. }
  391. public override void Abort ()
  392. {
  393. src.Abort ();
  394. }
  395. public override void Close ()
  396. {
  397. src.Close ();
  398. }
  399. public override void Close (TimeSpan timeout)
  400. {
  401. src.Close (timeout);
  402. }
  403. public override IAsyncResult BeginReply (Message message, AsyncCallback callback, object state)
  404. {
  405. return src.BeginReply (message, callback, state);
  406. }
  407. public override IAsyncResult BeginReply (Message message, TimeSpan timeout, AsyncCallback callback, object state)
  408. {
  409. return src.BeginReply (message, timeout, callback, state);
  410. }
  411. public override void EndReply (IAsyncResult result)
  412. {
  413. src.EndReply (result);
  414. }
  415. public override void Reply (Message message)
  416. {
  417. src.Reply (message);
  418. }
  419. public override void Reply (Message message, TimeSpan timeout)
  420. {
  421. src.Reply (message, timeout);
  422. }
  423. public override Message RequestMessage {
  424. get { return copy_msg; }
  425. }
  426. }
  427. }