InitiatePhoneGapBuildProcess.java
10.3 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
/*
* 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.exec.WorkflowProcess
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.day.cq.wcm.api.Page
* javax.jcr.Session
* 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.Service
* org.apache.sling.api.adapter.AdapterManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.json.JSONObject
* org.ccil.cowan.tagsoup.jaxp.SAXParserImpl
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.phonegap.impl.build.workflow;
import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.service.PGBuildManagerAdapterFactory;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
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.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import javax.jcr.Session;
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.Service;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.json.JSONObject;
import org.ccil.cowan.tagsoup.jaxp.SAXParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"PhoneGap Workflow process that creates and updates app builds on PhoneGap build and tracks its build status."}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"Initiate PhoneGap Build"})})
public class InitiatePhoneGapBuildProcess
implements WorkflowProcess {
private static final String WORKFLOW_PAYLOAD_TYPE_JCR_PATH = "JCR_PATH";
private static final Logger logger = LoggerFactory.getLogger(InitiatePhoneGapBuildProcess.class);
@Reference
private AdapterManager adapterManager;
@Reference
PGBuildManagerAdapterFactory phonegapBuildManagerAdapterFactory;
@Reference
private ResourceResolverFactory resourceResolverFactory;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
WorkflowData workflowData;
block14 : {
workflowData = workItem.getWorkflowData();
try {
logger.info("Initiating build of {} with PhoneGap Build", workflowData.getPayload());
if (workflowData.getPayloadType().equals("JCR_PATH")) {
String payloadPath = String.valueOf(workflowData.getPayload());
ResourceResolver resourceResolver = (ResourceResolver)workflowSession.adaptTo(ResourceResolver.class);
Resource resource = resourceResolver.getResource(payloadPath);
Page page = (Page)this.adapterManager.getAdapter((Object)resource, Page.class);
if (page == null) {
throw new PGException("Not a valid page resource for PhoneGap Build workflow: " + (Object)resource);
}
PGBuildManager pgBuildManager = this.phonegapBuildManagerAdapterFactory.getAdapter((Object)resourceResolver, PGBuildManager.class);
AppInfo appinfo = pgBuildManager.getApp(page);
if (appinfo == null) {
pgBuildManager.createApp(page);
} else {
pgBuildManager.updateApp(page);
}
logger.info("Initiating build of {} with PhoneGap Build completed", workflowData.getPayload());
break block14;
}
throw new PGException("Not a valid payload type for PhoneGap Build workflow: " + workflowData.getPayloadType());
}
catch (Exception ex) {
try {
String cause = ex.getCause().getMessage();
if (cause.contains("ErrorDetails: ")) {
if ((cause = cause.substring(cause.indexOf("ErrorDetails: ") + 14).replaceAll("\\s$", "")).startsWith("<!DOCTYPE")) {
PhoneGapBuildHtmlErrorHandler handler = new PhoneGapBuildHtmlErrorHandler();
SAXParserImpl.newInstance((Map)null).parse((InputStream)new ByteArrayInputStream(cause.getBytes("UTF-8")), (DefaultHandler)handler);
cause = handler.getErrorMessage();
} else {
JSONObject causeJson = new JSONObject(cause);
cause = causeJson.getString("error");
}
}
this.updateWorkflowData(workItem, workflowSession, "PGBuildError", cause, true);
WorkflowException wfEx = new WorkflowException(cause, (Throwable)ex);
logger.error(wfEx.getMessage(), (Throwable)wfEx);
throw wfEx;
}
catch (Exception ex2) {
WorkflowException wfEx = new WorkflowException("Attempt to save PGBuild failure message to workflow data failed", (Throwable)ex2);
logger.error(wfEx.getMessage(), (Throwable)wfEx);
throw wfEx;
}
}
}
try {
if (workflowData.getMetaDataMap().get("PGBuildError", String.class) != null) {
this.updateWorkflowData(workItem, workflowSession, "PGBuildError", "Resolved", false);
}
}
catch (Exception ex3) {
WorkflowException wfEx = new WorkflowException("Attempt to clear PGBuild failure message failed", (Throwable)ex3);
logger.error(wfEx.getMessage(), (Throwable)wfEx);
throw wfEx;
}
}
protected void updateWorkflowData(WorkItem item, WorkflowSession wfSession, String key, String value, boolean saveSession) throws Exception {
WorkflowData data = item.getWorkflowData();
String currentValue = (String)data.getMetaDataMap().get(key, String.class);
if (currentValue != null && value.isEmpty() || currentValue == null || !currentValue.equals(value)) {
MetaDataMap metaData = data.getMetaDataMap();
metaData.put((Object)key, (Object)value);
wfSession.updateWorkflowData(item.getWorkflow(), data);
if (saveSession) {
((Session)wfSession.adaptTo(Session.class)).save();
}
}
}
protected void bindAdapterManager(AdapterManager adapterManager) {
this.adapterManager = adapterManager;
}
protected void unbindAdapterManager(AdapterManager adapterManager) {
if (this.adapterManager == adapterManager) {
this.adapterManager = null;
}
}
protected void bindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
this.phonegapBuildManagerAdapterFactory = pGBuildManagerAdapterFactory;
}
protected void unbindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
if (this.phonegapBuildManagerAdapterFactory == pGBuildManagerAdapterFactory) {
this.phonegapBuildManagerAdapterFactory = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
private class PhoneGapBuildHtmlErrorHandler
extends DefaultHandler {
private StringBuilder errorMessage;
private boolean inBody;
private PhoneGapBuildHtmlErrorHandler() {
this.errorMessage = new StringBuilder();
this.inBody = false;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (this.inBody) {
if (qName.equals("h1")) {
qName = "h3";
}
this.errorMessage.append("<" + qName + ">");
}
if (qName.equalsIgnoreCase("body")) {
this.inBody = true;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (this.inBody) {
this.errorMessage.append(Arrays.copyOfRange(ch, start, start + length));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (this.inBody) {
this.errorMessage.append("</" + qName + ">");
}
if (qName.equals("body")) {
this.inBody = false;
}
}
public String getErrorMessage() {
return this.errorMessage.toString();
}
}
}