1
+ using System . Xml ;
2
+ using Microsoft . AspNetCore . Mvc ;
3
+ using WorkflowServer . CallbackApi . Models ;
4
+ using WorkflowServer . CallbackApi . Workflow ;
5
+
6
+ namespace WorkflowServer . CallbackApi . Controllers ;
7
+
8
+ /// <summary>
9
+ /// This controller implements all possible requests from Callback API of Workflow Server 3.0.0.
10
+ /// Callback API allows hosting your code of Actions, Conditions or Rules on third-party servers.
11
+ /// Callback server should be connected in the admin panel on the Callback API page.
12
+ /// More details on the <see href="https://workflowserver.io/documentation/callback-api">workflowserver.io</see>
13
+ /// </summary>
14
+ [ ApiController ]
15
+ [ Route ( "[controller]/[action]" ) ]
16
+ public class ApiController : ControllerBase
17
+ {
18
+ public ApiController ( ILogger < ApiController > logger )
19
+ {
20
+ _logger = logger ;
21
+
22
+ _actionProvider = new ActionProvider ( ) ;
23
+ _conditionProvider = new ConditionProvider ( ) ;
24
+ _identityProvider = new IdentityProvider ( ) ;
25
+
26
+ //The parameters will not be saved, this is an example. You should add the provider as a dependency injection.
27
+ _parameterProvider = new ParameterProvider ( ) ;
28
+ }
29
+
30
+ #region Actions & Conditions Execution
31
+
32
+ /// <summary>
33
+ /// Returns a list of actions that can be executed on the callback server.
34
+ /// When the Workflow Server should execute an Action with a certain name,
35
+ /// it calls the action execution method on the server that has returned this
36
+ /// name in response to the request for the list of actions.
37
+ /// </summary>
38
+ [ HttpGet ]
39
+ public Task < IActionResult > GetActions ( string schemeCode , string ? token = null )
40
+ {
41
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( _actionProvider . ActionNames ) ) ) ;
42
+ }
43
+
44
+ /// <summary>
45
+ /// Request to execute the action. Which of callback servers is called to execute this method, depends on the list of actions returned.
46
+ /// </summary>
47
+ [ HttpPost ]
48
+ public async Task < IActionResult > ExecuteAction ( InstanceRequest request )
49
+ {
50
+ await _actionProvider . ExecuteAction ( request . Name , request . Parameter , request . ProcessInstance ) ;
51
+
52
+ return Ok ( ApiResponse . Ok ( ) ) ;
53
+ }
54
+
55
+ /// <summary>
56
+ /// Returns a list of conditions that can be executed on the callback server.
57
+ /// When the Workflow Server should execute a condition with a certain name,
58
+ /// it calls the condition execution method on the server that has returned
59
+ /// this name in response to the request for the list of conditions.
60
+ /// </summary>
61
+ [ HttpGet ]
62
+ public Task < IActionResult > GetConditions ( string schemeCode , string ? token = null )
63
+ {
64
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( _conditionProvider . ConditionNames ) ) ) ;
65
+ }
66
+
67
+ /// <summary>
68
+ /// Request to execute the condition. Which of the callback servers
69
+ /// is called to execute this method, depends on the list of conditions returned.
70
+ /// </summary>
71
+ [ HttpPost ]
72
+ public async Task < IActionResult > ExecuteCondition ( InstanceRequest request )
73
+ {
74
+ var result = await _conditionProvider . ExecuteCondition ( request . Name , request . Parameter , request . ProcessInstance ) ;
75
+
76
+ return Ok ( ApiResponse . Ok ( result ) ) ;
77
+ }
78
+
79
+ private readonly ActionProvider _actionProvider ;
80
+ private readonly ConditionProvider _conditionProvider ;
81
+
82
+ #endregion
83
+
84
+ #region Authorization Rules Execution
85
+
86
+ /// <summary>
87
+ /// Returns a list of Authorization Rules that can be executed on the callback server.
88
+ /// When the Workflow Server should execute an Authorization Rule with a certain name,
89
+ /// it calls the Authorization Rule execution method on the server that has returned
90
+ /// this name in response to the request for the list of Authorization Rules.
91
+ /// </summary>
92
+ [ HttpGet ]
93
+ public Task < IActionResult > GetRules ( string schemeCode , string ? token = null )
94
+ {
95
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( _identityProvider . RuleNames ) ) ) ;
96
+ }
97
+
98
+ /// <summary>
99
+ /// Check if the User has the access to the command.
100
+ /// </summary>
101
+ [ HttpPost ]
102
+ public async Task < IActionResult > CheckRule ( CheckRuleRequest request )
103
+ {
104
+ var result = await _identityProvider . RuleCheck ( request . Name , request . IdentityId , request . Parameter , request . ProcessInstance ) ;
105
+ return Ok ( ApiResponse . Ok ( result ) ) ;
106
+ }
107
+
108
+ /// <summary>
109
+ /// Returns the list of all Users who have the access.
110
+ /// </summary>
111
+ [ HttpPost ]
112
+ public async Task < IActionResult > GetIdentities ( InstanceRequest request )
113
+ {
114
+ var result = await _identityProvider . RuleGet ( request . Name , request . Parameter , request . ProcessInstance ) ;
115
+ return Ok ( ApiResponse . Ok ( result ) ) ;
116
+ }
117
+
118
+ private readonly IdentityProvider _identityProvider ;
119
+
120
+ #endregion
121
+
122
+ #region Remote Scheme Generation
123
+
124
+ /// <summary>
125
+ /// Workflow Engine (is a part of WorkflowServer) supports scheme generation. If you include this method, then,
126
+ /// to initialize a new scheme, the Workflow Server calls it on all the connected
127
+ /// servers with the settings containing the address of this method.
128
+ /// </summary>
129
+ [ HttpPost ]
130
+ public Task < IActionResult > Generate ( GenerateRequest request )
131
+ {
132
+ XmlDocument xml = new XmlDocument ( ) ;
133
+
134
+ //Your code
135
+
136
+ return Task . FromResult < IActionResult > ( Ok ( xml . OuterXml ) ) ;
137
+ }
138
+
139
+ #endregion
140
+
141
+ #region Event Handlers
142
+
143
+ /// <summary>
144
+ /// The ProcessStatusChanged event is called only after switching to
145
+ /// the statuses of Idled and Finalized. It is not called for subprocesses.
146
+ /// </summary>
147
+ [ HttpPost ]
148
+ public Task < IActionResult > ProcessStatusChanged ( StatusChangedRequest request )
149
+ {
150
+ _logger . LogInformation ( "Process status changed from {OldStatus} to {NewStatus}" ,
151
+ request . OldStatus , request . NewStatus ) ;
152
+
153
+ //Your event actions
154
+
155
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( ) ) ) ;
156
+ }
157
+
158
+ /// <summary>
159
+ /// The ProcessActivityChanged event.
160
+ /// </summary>
161
+ [ HttpPost ]
162
+ public Task < IActionResult > ProcessActivityChanged ( ActivityChangedRequest request )
163
+ {
164
+ _logger . LogInformation ( "Process activity changed from {PreviousActivityName} to {CurrentActivityName}" ,
165
+ request . PreviousActivityName , request . CurrentActivityName ) ;
166
+
167
+ //Your event actions
168
+
169
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( ) ) ) ;
170
+ }
171
+
172
+ /// <summary>
173
+ /// The processLogs event.
174
+ /// </summary>
175
+ [ HttpPost ]
176
+ public Task < IActionResult > ProcessLog ( LogRequest request )
177
+ {
178
+ //Your event actions
179
+
180
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( ) ) ) ;
181
+ }
182
+
183
+ #endregion
184
+
185
+ #region Parameters Providing
186
+
187
+ /// <summary>
188
+ /// Used to get parameters list.
189
+ /// </summary>
190
+ [ HttpGet ]
191
+ public Task < IActionResult > GetParameterNames ( string schemeCode , string ? token = null )
192
+ {
193
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( _parameterProvider . GetNames ) ) ) ;
194
+ }
195
+
196
+ /// <summary>
197
+ /// The method returns a JSON serialized parameter value.
198
+ /// </summary>
199
+ [ HttpPost ]
200
+ public Task < IActionResult > GetParameter ( ParameterRequest request )
201
+ {
202
+ var result = _parameterProvider . GetParameter ( request . Name ) ;
203
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( result ) ) ) ;
204
+ }
205
+
206
+ /// <summary>
207
+ /// Used for set parameter value.
208
+ /// </summary>
209
+ [ HttpPost ]
210
+ public Task < IActionResult > SetParameter ( ParameterRequest request )
211
+ {
212
+ _parameterProvider . SetParameter ( request . Name , request . Value ) ;
213
+ return Task . FromResult < IActionResult > ( Ok ( ApiResponse . Ok ( ) ) ) ;
214
+ }
215
+
216
+ private readonly ParameterProvider _parameterProvider ;
217
+
218
+ #endregion
219
+
220
+ private readonly ILogger < ApiController > _logger ;
221
+ }
0 commit comments