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