| | 1 | | namespace Microsoft.Azure.Batch.Protocol |
| | 2 | | { |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Reflection; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Represents a method that intercepts a request to the Batch service in order |
| | 9 | | /// to modify the parameters. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="request">The parameters of the request being intercepted.</param> |
| | 12 | | public delegate void BatchRequestModificationInterceptHandler(Protocol.IBatchRequest request); |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Represents a method that intercepts a request to the Batch service in order |
| | 16 | | /// to replace the parameters and/or operation context. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="request">The parameters of the request being intercepted.</param> |
| | 19 | | public delegate void BatchRequestReplacementInterceptHandler(ref Protocol.IBatchRequest request); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Derived classes can inspect, replace or modify a BatchRequest. |
| | 23 | | /// </summary> |
| | 24 | | public class RequestReplacementInterceptor : BatchClientBehavior |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of RequestReplacementInterceptor. |
| | 28 | | /// </summary> |
| | 29 | | public RequestReplacementInterceptor() |
| | 30 | | { |
| | 31 | | this.ReplacementInterceptHandler = RequestReplacementInterceptor.NOOP; // benign noop. |
| | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Initializes a new instance of RequestReplacementInterceptor |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="replacementInterceptor"></param> |
| | 38 | | public RequestReplacementInterceptor(BatchRequestReplacementInterceptHandler replacementInterceptor) |
| | 39 | | { |
| | 40 | | this.ReplacementInterceptHandler = replacementInterceptor; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Does nothing. |
| | 45 | | /// </summary> |
| | 46 | | private static void NOOP(ref Protocol.IBatchRequest request) |
| | 47 | | { |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets or sets a method that will be called just before a <see cref="IBatchRequest"/> is executed. |
| | 52 | | /// The request can be inspected, replaced, modified or ignored. |
| | 53 | | /// </summary> |
| | 54 | | public BatchRequestReplacementInterceptHandler ReplacementInterceptHandler |
| | 55 | | { |
| | 56 | | get; |
| | 57 | | set; |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// This class enables an interceptor to inspect, modify or ignore a <see cref="IBatchRequest"/>. |
| | 63 | | /// </summary> |
| | 64 | | public class RequestInterceptor : RequestReplacementInterceptor |
| | 65 | | { |
| | 66 | | private BatchRequestModificationInterceptHandler _modInterceptor; |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Initializes a new instance of RequestInterceptor. |
| | 70 | | /// </summary> |
| 685 | 71 | | public RequestInterceptor() |
| | 72 | | { |
| 685 | 73 | | this.ModificationInterceptHandler = RequestInterceptor.NOOP; // benign default |
| 685 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Initializes a new instance of RequestInterceptor that calls a given BatchRequestModificationInterceptHandler |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="interceptor">A delegate that will be allowed to inspect, modify or ignore a BatchRequest.</para |
| 373 | 80 | | public RequestInterceptor(BatchRequestModificationInterceptHandler interceptor) |
| | 81 | | { |
| 373 | 82 | | this.ModificationInterceptHandler = interceptor; |
| 373 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Gets or sets the BatchRequestModificationInterceptHandler. |
| | 87 | | /// </summary> |
| | 88 | | public BatchRequestModificationInterceptHandler ModificationInterceptHandler |
| | 89 | | { |
| | 90 | | get |
| | 91 | | { |
| 0 | 92 | | return _modInterceptor; |
| | 93 | | } |
| | 94 | | set |
| | 95 | | { |
| 1743 | 96 | | _modInterceptor = value; |
| 1743 | 97 | | base.ReplacementInterceptHandler = this.ReplacementInterceptHandler; |
| 1743 | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// An interceptor that does nothing. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="request"></param> |
| | 105 | | /// <returns></returns> |
| | 106 | | private static void NOOP(object request) |
| | 107 | | { |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// A replacment interceptor that calls our modification interceptor. |
| | 112 | | /// </summary> |
| | 113 | | private new void ReplacementInterceptHandler(ref Protocol.IBatchRequest request) |
| | 114 | | { |
| 266 | 115 | | _modInterceptor(request); |
| 199 | 116 | | } |
| | 117 | | } |
| | 118 | | } |