InboxServlet.java
20.5 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.collection.util.ResultSet
* com.adobe.granite.workflow.exec.InboxItem
* com.adobe.granite.workflow.exec.Participant
* com.adobe.granite.workflow.exec.Route
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.exec.filter.InboxItemFilter
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.adobe.granite.workflow.model.WorkflowNode
* com.adobe.granite.workflow.model.WorkflowTransition
* com.adobe.granite.xss.JSONUtil
* com.adobe.granite.xss.XSSFilter
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.adobe.granite.workflow.console.servlet;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.collection.util.ResultSet;
import com.adobe.granite.workflow.console.servlet.ServletBase;
import com.adobe.granite.workflow.console.servlet.WorkflowConsoleUtil;
import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.exec.Participant;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.filter.InboxItemFilter;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowTransition;
import com.adobe.granite.xss.JSONUtil;
import com.adobe.granite.xss.XSSFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"cq/workflow/components/inbox"}), @Property(name="sling.servlet.paths", value={"/bin/workflow/inbox", ""}), @Property(name="sling.servlet.methods", value={"GET", "POST"}), @Property(name="sling.servlet.extensions", value={"json", ""}), @Property(name="service.description", value={"Workflow Publish Process"})})
public class InboxServlet
extends ServletBase {
private static final long serialVersionUID = -3596348616959666034L;
private static final String CMD_ADVANCE = "advance";
private static final String CMD_ADVANCE_BACK = "advanceBack";
private static final String CMD_DELEGATE = "delegate";
private static final String P_CMD = "cmd";
private static final String P_ROUTE = "route";
private static final String P_BACK_ROUTE = "backroute";
private static final String P_ITEM = "item";
private static final String P_DELEGATEE = "delegatee";
public static final String TYPE_JAVA_OBJECT = "JAVA_OBJECT";
public static final String TYPE_JCR_PATH = "JCR_PATH";
public static final String TYPE_JCR_UUID = "JCR_UUID";
public static final String TYPE_URL = "URL";
public static final String TYPE_BINARY = "BINARY";
private static final String[] META_DATA_EXCLUDES = new String[]{"cmd", "item", "route", "backroute", "delegatee"};
@Reference(policy=ReferencePolicy.STATIC)
private XSSFilter xss;
private static final String FAILURE_ROUTE = "retry-current-step";
private static final String FAILURE_ITEM_TYPE = "FailureItem";
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json");
WorkflowSession wfSession = (WorkflowSession)request.getResourceResolver().adaptTo(WorkflowSession.class);
ResultSet items = null;
try {
items = wfSession.getActiveInboxItems(0, -1, null);
}
catch (Exception e) {
throw new ServletException((Throwable)e);
}
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
writer.setTidy(true);
try {
writer.array();
for (InboxItem item : (InboxItem[])items.getItems()) {
writer.object();
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"uri", (String)(request.getContextPath() + item.getId()), (XSSFilter)this.xss);
if (item.getCurrentAssignee() != null) {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"currentAssignee", (String)item.getCurrentAssignee(), (XSSFilter)this.xss);
}
if (item.getTimeStarted() != null) {
writer.key("startTime").value(item.getTimeStarted().getTime());
}
if (item.getTimeEnded() != null) {
writer.key("endTime").value(item.getTimeEnded().getTime());
}
if (item instanceof WorkItem) {
String comment;
WorkItem workItem = (WorkItem)item;
WorkflowData workflowData = workItem.getWorkflowData();
if (workflowData != null && workflowData.getPayloadType() != null) {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"payloadType", (String)workflowData.getPayloadType(), (XSSFilter)this.xss);
if (workflowData.getPayload() != null && ("JCR_PATH".equals(workflowData.getPayloadType()) || "JCR_UUID".equals(workflowData.getPayloadType()) || "URL".equals(workflowData.getPayloadType()))) {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"payload", (String)workflowData.getPayload().toString(), (XSSFilter)this.xss);
}
}
if ((comment = this.getComment(workItem)) != null && !comment.equals("")) {
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"comment", (String)comment, (XSSFilter)this.xss);
}
}
JSONUtil.writeWithProtected((JSONWriter)writer, (String)"type", (String)item.getItemType(), (XSSFilter)this.xss);
writer.endObject();
}
writer.endArray();
}
catch (JSONException e) {
throw new ServletException((Throwable)e);
}
}
private String getComment(WorkItem workItem) {
String comment = null;
if (workItem.getNode().getType().equals("PROCESS")) {
comment = (String)workItem.getMetaDataMap().get("failureMessage", String.class);
} else if (workItem.getNode().getType().equals("PARTICIPANT") || workItem.getNode().getType().equals("DYNAMIC_PARTICIPANT")) {
comment = (String)workItem.getMetaDataMap().get("comment", String.class);
}
return comment;
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block22 : {
WorkflowSession wfSession = (WorkflowSession)request.getResourceResolver().adaptTo(WorkflowSession.class);
RequestParameterMap params = request.getRequestParameterMap();
RequestParameter cmd = params.getValue("cmd");
if (cmd == null) {
RequestParameter itemIdParam = params.getValue("item");
RequestParameter routeIdParam = params.getValue("route");
RequestParameter backRouteIdParam = params.getValue("backroute");
RequestParameter delegateeIdParam = params.getValue("delegatee");
try {
if (itemIdParam != null && routeIdParam != null) {
this.advance(params, wfSession, itemIdParam.getString(), routeIdParam.getString());
this.sendResponse((HttpServletResponse)response, 200, "Workflow(s) advanced");
break block22;
}
if (itemIdParam != null && backRouteIdParam != null) {
this.advanceBack(params, wfSession, itemIdParam.getString(), backRouteIdParam.getString());
this.sendResponse((HttpServletResponse)response, 200, "Workflow(s) advanced back");
break block22;
}
if (itemIdParam != null && delegateeIdParam != null) {
this.delegate(response, wfSession, params, itemIdParam.getString(), delegateeIdParam.getString());
this.sendResponse((HttpServletResponse)response, 200, "Work Item(s) delegated");
break block22;
}
this.sendResponse((HttpServletResponse)response, 400, "Invalid parameters");
}
catch (WorkflowException e) {
this.sendResponse((HttpServletResponse)response, 500, e.getMessage());
}
} else {
RequestParameter[] itemIdParams = params.getValues("item");
if ("advance".equals(cmd.getString()) && itemIdParams != null && itemIdParams.length > 0) {
Map<String, String> p = this.collectParams("route", response, params, itemIdParams);
if (p != null) {
try {
this.advance(response, params, wfSession, p);
}
catch (WorkflowException e) {
this.sendResponse((HttpServletResponse)response, 500, e.getMessage());
}
}
} else if ("delegate".equals(cmd.getString()) && itemIdParams != null && itemIdParams.length > 0) {
Map<String, String> p = this.collectParams("delegatee", response, params, itemIdParams);
if (p != null) {
try {
this.delegate(response, wfSession, params, p);
}
catch (WorkflowException e) {
this.sendResponse((HttpServletResponse)response, 500, e.getMessage());
}
}
} else if ("advanceBack".equals(cmd.getString()) && itemIdParams != null && itemIdParams.length > 0) {
Map<String, String> p = this.collectParams("backroute", response, params, itemIdParams);
if (p != null) {
try {
this.advanceBack(response, wfSession, params, p);
}
catch (WorkflowException e) {
this.sendResponse((HttpServletResponse)response, 500, e.getMessage());
}
}
} else {
this.sendResponse((HttpServletResponse)response, 400, "Invalid parameters");
}
}
}
}
private Map<String, String> collectParams(String namePrefix, SlingHttpServletResponse response, RequestParameterMap params, RequestParameter[] itemIdParams) {
HashMap<String, String> p = new HashMap<String, String>();
for (RequestParameter itemIdParam : itemIdParams) {
String itemId = itemIdParam.getString();
RequestParameter param = params.getValue(namePrefix + "-" + itemId);
if (param == null) {
this.sendResponse((HttpServletResponse)response, 400, "Invalid parameters");
return null;
}
String val = param.getString();
p.put(itemId, val);
}
return p;
}
private void delegate(SlingHttpServletResponse response, WorkflowSession wfSession, RequestParameterMap params, Map<String, String> p) throws AccessControlException, WorkflowException {
for (String itemId : p.keySet()) {
String delegateeId = p.get(itemId);
this.delegate(response, wfSession, params, itemId, delegateeId);
}
this.sendResponse((HttpServletResponse)response, 200, "Work item(s) delegated");
}
private void delegate(SlingHttpServletResponse response, WorkflowSession wfSession, RequestParameterMap params, String itemId, String delegateeId) throws WorkflowException {
boolean delegated = false;
WorkItem item = wfSession.getWorkItem(itemId);
Iterator delegatees = wfSession.getDelegates(item);
while (delegatees.hasNext()) {
Participant delegatee = (Participant)delegatees.next();
if (!delegatee.getID().equals(delegateeId)) continue;
this.updateMetaData(params, item);
wfSession.delegateWorkItem(item, delegatee);
delegated = true;
break;
}
if (!delegated) {
this.sendResponse((HttpServletResponse)response, 500, "Delegatee '" + delegateeId + "' not found");
return;
}
}
private void advanceBack(SlingHttpServletResponse response, WorkflowSession wfSession, RequestParameterMap params, Map<String, String> p) throws WorkflowException {
for (String itemId : p.keySet()) {
String routeId = p.get(itemId);
this.advanceBack(params, wfSession, itemId, routeId);
}
this.sendResponse((HttpServletResponse)response, 200, "Workflow(s) advanced back");
}
private void advanceBack(RequestParameterMap params, WorkflowSession wfSession, String itemId, String routeId) throws WorkflowException {
WorkItem item = wfSession.getWorkItem(itemId);
List routes = wfSession.getBackRoutes(item, true);
this.findRouteAndAdvance(wfSession, params, routeId, item, routes);
}
private void advance(SlingHttpServletResponse response, RequestParameterMap params, WorkflowSession wfSession, Map<String, String> p) throws WorkflowException {
for (String itemId : p.keySet()) {
String routeId = p.get(itemId);
this.advance(params, wfSession, itemId, routeId);
}
this.sendResponse((HttpServletResponse)response, 200, "Workflow(s) advanced");
}
private void advance(RequestParameterMap params, WorkflowSession wfSession, String itemId, String routeId) throws WorkflowException {
WorkItem item = wfSession.getWorkItem(itemId);
List routes = wfSession.getRoutes(item, true);
this.findRouteAndAdvance(wfSession, params, routeId, item, routes);
}
private void findRouteAndAdvance(WorkflowSession wfSession, RequestParameterMap params, String routeId, WorkItem item, List<Route> routes) throws WorkflowException {
if ("retry-current-step".equals(routeId) && "FailureItem".equals(item.getItemSubType())) {
WorkflowConsoleUtil.retryStep(wfSession, item);
} else {
boolean foundRoute = false;
for (Route route : routes) {
WorkflowNode node;
String cRouteId = route.getId();
if (cRouteId.equals(routeId)) {
this.updateMetaData(params, item);
wfSession.complete(item, route);
foundRoute = true;
break;
}
if (!routeId.contains("@") || !(node = ((WorkflowTransition)route.getDestinations().get(0)).getTo()).getType().equals("PARTICIPANT")) continue;
String participant = (String)node.getMetaDataMap().get("PARTICIPANT", String.class);
if (!routeId.startsWith(cRouteId + "@") || !routeId.endsWith("@" + participant)) continue;
this.updateMetaData(params, item);
wfSession.complete(item, route);
foundRoute = true;
break;
}
if (!foundRoute) {
throw new WorkflowException("Route not found");
}
}
}
protected void updateMetaData(RequestParameterMap params, WorkItem item) {
MetaDataMap metaData = item.getMetaDataMap();
RequestParameter cmd = params.getValue("cmd");
if (cmd == null) {
List<String> excludes = Arrays.asList(DEFAULT_META_DATA_EXCLUDES);
for (String key : params.keySet()) {
if (excludes.contains(key) || this.getMetaDataExcludes(params).contains(key)) continue;
this.setMetadataFromRequestParameter(metaData, params, key);
}
} else {
String suffix = "-" + item.getId();
RequestParameter[] itemIdParams = params.getValues("item");
List<String> excludes = Arrays.asList(DEFAULT_META_DATA_EXCLUDES);
Iterator keys = params.keySet().iterator();
while (keys.hasNext()) {
String key = (String)keys.next();
if (excludes.contains(key) || this.getMetaDataExcludes(params).contains(key)) continue;
boolean hasSuffix = false;
if (key.endsWith(suffix)) {
RequestParameter para = params.getValue(key);
key = key.substring(0, key.indexOf(suffix));
metaData.put((Object)key, (Object)para.getString());
hasSuffix = true;
} else {
for (RequestParameter itemIdParam : itemIdParams) {
if (!key.endsWith("-" + itemIdParam.getString())) continue;
hasSuffix = true;
break;
}
}
if (hasSuffix) continue;
this.setMetadataFromRequestParameter(metaData, params, key);
}
}
}
private void setMetadataFromRequestParameter(MetaDataMap metaData, RequestParameterMap params, String key) {
RequestParameter[] paraValues = params.getValues(key);
if (paraValues != null && paraValues.length > 0) {
if (paraValues.length == 1) {
metaData.put((Object)key, (Object)paraValues[0].getString());
} else {
String[] values = new String[paraValues.length];
for (int index = 0; index < values.length; ++index) {
values[index] = paraValues[index].getString();
}
metaData.put((Object)key, (Object)values);
}
}
}
@Override
protected List<String> getMetaDataExcludes(RequestParameterMap params) {
ArrayList<String> excludes = new ArrayList<String>();
excludes.addAll(Arrays.asList(META_DATA_EXCLUDES));
for (String name : params.keySet()) {
if (!name.startsWith("route-") && !name.startsWith("backroute-") && !name.startsWith("delegatee-")) continue;
excludes.add(name);
}
return excludes;
}
protected void bindXss(XSSFilter xSSFilter) {
this.xss = xSSFilter;
}
protected void unbindXss(XSSFilter xSSFilter) {
if (this.xss == xSSFilter) {
this.xss = null;
}
}
}