FailureCommandServlet.java
9.04 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
/*
* 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.exec.WorkItem
* com.adobe.granite.workflow.exec.Workflow
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.adobe.granite.workflow.model.WorkflowModel
* 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.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.api.servlets.HtmlResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.granite.workflow.console.servlet;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.console.servlet.WorkflowConsoleUtil;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
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.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.api.servlets.HtmlResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/cq/workflow/failures"}), @Property(name="sling.servlet.methods", value={"POST", ""}), @Property(name="service.description", value={"Workflow Publish Process"})})
public class FailureCommandServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = 2171661388034822790L;
private static final String COMMAND_TERMINATE_RESTART = "TerminateAndRestart";
private static final String COMMAND_RETRY_ITEM = "RetryItem";
private static final String COMMANDPARAM_NAME = "command";
private static final String PROPERTY_IDS = "IDs";
private static final String METANAME_CURRENTJOBS = "currentJobs";
private static final Set<String> METADATA_IGNORE_MAP = new HashSet<String>();
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block12 : {
RequestParameterMap params = request.getRequestParameterMap();
WorkflowSession wfSession = (WorkflowSession)request.getResourceResolver().adaptTo(WorkflowSession.class);
RequestParameter command = params.getValue("command");
if (command != null) {
try {
String jsonPayload = FailureCommandServlet.readJSON(request);
JSONObject parameterObject = new JSONObject(jsonPayload);
String commandValue = command.getString();
if ("TerminateAndRestart".equals(commandValue)) {
JSONArray arrayOfWorkflows = parameterObject.getJSONArray("IDs");
for (int index = 0; index < arrayOfWorkflows.length(); ++index) {
if (!(arrayOfWorkflows.get(index) instanceof String)) continue;
String worklowId = (String)arrayOfWorkflows.get(index);
try {
Workflow workflow = wfSession.getWorkflow(worklowId);
WorkflowData oldWorkflowData = workflow.getWorkflowData();
WorkflowModel model = workflow.getWorkflowModel();
String modelId = model.getId();
WorkflowModel latestModel = wfSession.getModel(modelId);
this.updateMetaData(params, workflow);
wfSession.terminateWorkflow(workflow);
WorkflowData newWorkflowData = wfSession.newWorkflowData(oldWorkflowData.getPayloadType(), oldWorkflowData.getPayload());
this.copyMetaData(oldWorkflowData, newWorkflowData);
Workflow newWorkflowInstance = wfSession.startWorkflow(latestModel, newWorkflowData);
response.getWriter().write("Restarted " + worklowId + ", new Workflow ID: " + newWorkflowInstance.getId() + "\n");
continue;
}
catch (WorkflowException e) {
response.getWriter().write("Failed to restart workflow " + worklowId + "\n");
continue;
}
catch (IOException e) {
response.getWriter().write("Failed to restart workflow " + worklowId + "\n");
}
}
break block12;
}
if ("RetryItem".equals(commandValue)) {
JSONArray arrayOfItems = parameterObject.getJSONArray("IDs");
for (int index = 0; index < arrayOfItems.length(); ++index) {
if (!(arrayOfItems.get(index) instanceof String)) continue;
String itemId = (String)arrayOfItems.get(index);
WorkItem item = wfSession.getWorkItem(itemId);
WorkflowConsoleUtil.retryStep(wfSession, item);
}
break block12;
}
this.sendResponse((HttpServletResponse)response, 400, "Invalid command");
}
catch (IllegalStateException e) {
this.sendResponse((HttpServletResponse)response, 409, e.getMessage());
}
catch (Exception e) {
this.sendResponse((HttpServletResponse)response, 500, e.getMessage());
}
} else {
this.sendResponse((HttpServletResponse)response, 400, "Invalid parameters");
}
}
}
private void copyMetaData(WorkflowData oldData, WorkflowData newWorkflowData) {
Set oldKeySet = oldData.getMetaDataMap().keySet();
for (String key : oldKeySet) {
if (METADATA_IGNORE_MAP.contains(key)) continue;
newWorkflowData.getMetaDataMap().put((Object)key, oldData.getMetaDataMap().get((Object)key));
}
}
public static String readJSON(SlingHttpServletRequest request) throws IOException {
BufferedReader reader = request.getReader();
return FailureCommandServlet.readJSON(reader);
}
public static String readJSON(BufferedReader reader) throws IOException {
String json = "";
String line = reader.readLine();
while (line != null) {
json = json + line;
line = reader.readLine();
}
return json;
}
protected void sendResponse(HttpServletResponse response, int status, String message) {
HtmlResponse htmlResponse = new HtmlResponse();
htmlResponse.setStatus(status, message);
htmlResponse.setTitle(message);
try {
htmlResponse.send(response, true);
}
catch (IOException e) {
this.log("Error while writing response", (Throwable)e);
}
}
private void updateMetaData(RequestParameterMap params, Workflow workflow) {
for (String key : params.keySet()) {
if (key.equals("command") || key.startsWith(":") || key.startsWith("./")) continue;
RequestParameter para = params.getValue(key);
workflow.getMetaDataMap().put((Object)key, (Object)para.getString());
workflow.getWorkflowData().getMetaDataMap().put((Object)key, (Object)para.getString());
}
}
static {
METADATA_IGNORE_MAP.add("currentJobs");
}
}