PGBuildManagerImpl.java
20 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.contentsync.ContentSyncManager
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.IOUtils
* org.apache.http.entity.mime.content.ContentBody
* org.apache.http.entity.mime.content.FileBody
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.sling.api.adapter.AdapterManager
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.phonegap.impl.build.service;
import com.adobe.cq.mobile.phonegap.impl.PGBuildException;
import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBRequest;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildUserAccount;
import com.adobe.cq.mobile.phonegap.impl.build.action.PostAppBuild;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AccountInfo;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.util.RestClientUtil;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.IOUtils;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PGBuildManagerImpl
implements PGBuildManager {
private static String phonegapBuildHost = "build.phonegap.com";
private static int phonegapBuildPort = 443;
private static final Logger LOGGER = LoggerFactory.getLogger(PGBuildManagerImpl.class);
private static final String PGBUILD_SERVICE_NAME = "phonegap-build";
private final boolean useSsl;
private Session userSession = null;
private ConfigurationManager configurationManagerService = null;
private ContentSyncManager contentSyncManagerService = null;
private AdapterManager adapterManagerService = null;
private CryptoSupport cryptoSupportService = null;
private HttpClientBuilderFactory clientBuilderFactory = null;
public PGBuildManagerImpl(Session userSession, ContentSyncManager contentSyncManagerService, AdapterManager adapterManagerService, ConfigurationManager configurationManagerService, CryptoSupport cryptoSupportService, HttpClientBuilderFactory clientBuilderFactory, int port, String host, boolean useSsl) {
this.userSession = userSession;
this.contentSyncManagerService = contentSyncManagerService;
this.adapterManagerService = adapterManagerService;
this.configurationManagerService = configurationManagerService;
this.cryptoSupportService = cryptoSupportService;
this.clientBuilderFactory = clientBuilderFactory;
phonegapBuildPort = port;
phonegapBuildHost = host;
this.useSsl = useSsl;
}
public PGBuildUserAccount getPGAccountInfo(Page phoneGapBuildEnabledPage) throws RepositoryException, CryptoException, PGException {
Configuration pgBuildConfiguration = this.getPGBuildAccountConfiguration(phoneGapBuildEnabledPage);
if (pgBuildConfiguration == null) {
throw new PGException("PGBuild CloudService configuration not found or target does not exist on: " + phoneGapBuildEnabledPage.getPath());
}
InheritanceValueMap cmap = pgBuildConfiguration.getProperties();
String userId = (String)cmap.get("pgBuildUserId", String.class);
String password = (String)cmap.get("pgBuildPassword", String.class);
if (this.cryptoSupportService != null && this.cryptoSupportService.isProtected(password)) {
password = this.cryptoSupportService.unprotect(password);
}
if (userId == null || password == null) {
throw new PGException("PGBuild CloudService configuration invalid. Account credentials not set for configuration: " + pgBuildConfiguration.getName());
}
return new PGBuildUserAccount(userId, password);
}
public Configuration getPGBuildAccountConfiguration(Page phoneGapBuildEnabledPage) throws RepositoryException {
Configuration pgConfig = null;
try {
String[] cloudServiceConfigPaths = (String[])phoneGapBuildEnabledPage.getProperties().get("cq:cloudserviceconfigs", String[].class);
if (cloudServiceConfigPaths != null) {
pgConfig = this.configurationManagerService.getConfiguration("phonegap-build", cloudServiceConfigPaths);
}
}
catch (Exception ex) {
LOGGER.warn("Failed to get PGBuild account configuration", (Throwable)ex);
}
return pgConfig;
}
@Override
public AppInfo createApp(Page phoneGapBuildEnabledPage) throws PGException {
AppInfo appInfo = null;
try {
LOGGER.debug("Creating PhoneGap Build app for {} ", (Object)phoneGapBuildEnabledPage.getPath());
Page thePhoneGapRootPage = this.getPhoneGapBuildRootPage(phoneGapBuildEnabledPage);
if (null == thePhoneGapRootPage) {
throw new PGException("PhoneGap config not found in ancestry of " + (Object)thePhoneGapRootPage);
}
PGBuildUserAccount pgBuildUserAccount = this.getPGAccountInfo(thePhoneGapRootPage);
appInfo = this.initiateBuild(this.userSession, pgBuildUserAccount, thePhoneGapRootPage, null);
LOGGER.debug("App created and build initiated {} ", (Object)appInfo);
}
catch (Exception ex) {
throw new PGException("Failed to create app for " + phoneGapBuildEnabledPage.getPath(), ex);
}
return appInfo;
}
@Override
public AppInfo updateApp(Page phoneGapBuildEnabledPage) throws PGException {
AppInfo appInfo = null;
try {
LOGGER.debug("Updating PhoneGap Build app for {} ", (Object)phoneGapBuildEnabledPage.getPath());
Page thePhoneGapRootPage = this.getPhoneGapBuildRootPage(phoneGapBuildEnabledPage);
if (null == thePhoneGapRootPage) {
throw new PGException("PhoneGap config not found in ancestry of " + (Object)thePhoneGapRootPage);
}
PGBuildUserAccount pgBuildUserAccount = this.getPGAccountInfo(thePhoneGapRootPage);
String buildId = this.getPhoneGapBuildId(thePhoneGapRootPage);
appInfo = this.initiateBuild(this.userSession, pgBuildUserAccount, thePhoneGapRootPage, buildId);
LOGGER.debug("App updated and build initiated {} ", (Object)appInfo);
}
catch (Exception ex) {
throw new PGException("Failed update app for " + phoneGapBuildEnabledPage.getPath(), ex);
}
return appInfo;
}
@Override
public AppInfo getApp(Page phoneGapBuildEnabledPage) throws PGException {
AppInfo appInfo = null;
try {
LOGGER.debug("Get PhoneGap Build app for {}", (Object)phoneGapBuildEnabledPage.getPath());
String appID = this.getPhoneGapBuildId(phoneGapBuildEnabledPage);
if (appID != null) {
PGBuildUserAccount pgBuildUserAccount = this.getPGAccountInfo(phoneGapBuildEnabledPage);
PGBRequest request = new PGBRequest(this.clientBuilderFactory.newBuilder(), phonegapBuildHost, phonegapBuildPort, pgBuildUserAccount, this.useSsl);
JSONObject response = RestClientUtil.submitGetRequest(request, request.getAppURL(appID));
appInfo = new AppInfo(response);
}
}
catch (PGBuildException pgBuildEx) {
ModifiableValueMap modifiableValueMap;
String buildId;
Resource contentRes;
if (pgBuildEx.getErrorCode() == 404 && pgBuildEx.getErrorDetails().matches("app \\d+ not found") && (buildId = (String)(modifiableValueMap = (ModifiableValueMap)(contentRes = phoneGapBuildEnabledPage.getContentResource()).adaptTo(ModifiableValueMap.class)).get("phonegap-buildId", String.class)) != null) {
modifiableValueMap.remove((Object)"phonegap-buildId");
modifiableValueMap.remove((Object)"phonegap-builtBy");
try {
contentRes.getResourceResolver().commit();
}
catch (PersistenceException perEx) {
throw new PGException("Failed to clear bad app id for " + phoneGapBuildEnabledPage.getPath(), (Throwable)perEx);
}
LOGGER.info("Build ID " + buildId + " was removed from the app because it does not exist on the PhoneGap Build server. Affected " + phoneGapBuildEnabledPage.getPath());
throw new PGException("Build id '" + buildId + "' does not exist and was deleted.", pgBuildEx);
}
throw new PGException("Failed to get app for " + phoneGapBuildEnabledPage.getPath(), pgBuildEx);
}
catch (Exception ex) {
throw new PGException("Failed to get app for " + phoneGapBuildEnabledPage.getPath(), ex);
}
return appInfo;
}
@Override
public AccountInfo getAccountProfile(Page phoneGapBuildEnabledPage) throws PGException {
AccountInfo accInfo = null;
try {
LOGGER.debug("Get PhoneGap Build account profile for {}", (Object)phoneGapBuildEnabledPage.getPath());
PGBuildUserAccount pgBuildUserAccount = this.getPGAccountInfo(phoneGapBuildEnabledPage);
PGBRequest request = new PGBRequest(this.clientBuilderFactory.newBuilder(), phonegapBuildHost, phonegapBuildPort, pgBuildUserAccount, this.useSsl);
JSONObject response = RestClientUtil.submitGetRequest(request, request.getAccountProfileURL());
accInfo = new AccountInfo(response);
}
catch (PGException pbEx) {
throw pbEx;
}
catch (Exception ex) {
throw new PGException("Failed to get account profile for " + phoneGapBuildEnabledPage.getPath(), ex);
}
return accInfo;
}
@Override
public AccountInfo getAccountProfile(String userId, String password) throws PGException {
AccountInfo accInfo = null;
try {
LOGGER.debug("Get PhoneGap Build account profile for {}", (Object)userId);
if (this.cryptoSupportService != null && this.cryptoSupportService.isProtected(password)) {
password = this.cryptoSupportService.unprotect(password);
}
PGBuildUserAccount pgBuildUserAccount = new PGBuildUserAccount(userId, password);
PGBRequest request = new PGBRequest(this.clientBuilderFactory.newBuilder(), phonegapBuildHost, phonegapBuildPort, pgBuildUserAccount, this.useSsl);
JSONObject response = RestClientUtil.submitGetRequest(request, request.getAccountProfileURL());
accInfo = new AccountInfo(response);
}
catch (Exception ex) {
throw new PGException("Failed to get account profile for " + userId, ex);
}
return accInfo;
}
private String getContentSyncConfigPath(Page page) {
String contentSyncConfigPath = null;
String propertyKey = "phonegap-build-exportTemplate";
if (page.getProperties().containsKey((Object)propertyKey)) {
contentSyncConfigPath = (String)page.getProperties().get(propertyKey, String.class);
} else {
propertyKey = "phonegap-exportTemplate";
if (page.getProperties().containsKey((Object)propertyKey)) {
contentSyncConfigPath = (String)page.getProperties().get(propertyKey, String.class);
}
}
if (contentSyncConfigPath.startsWith(".")) {
contentSyncConfigPath = page.getContentResource().getPath() + "/" + contentSyncConfigPath;
contentSyncConfigPath = ResourceUtil.normalize((String)contentSyncConfigPath);
}
return contentSyncConfigPath;
}
private Page getPhoneGapBuildRootPage(Page currentPage) throws Exception {
Page thePhoneGapRootPage = null;
if (this.getContentSyncConfigPath(currentPage) != null) {
thePhoneGapRootPage = currentPage;
} else if (currentPage.getParent() != null) {
thePhoneGapRootPage = this.getSiteNodeWithPhoneGapRef(currentPage.getParent());
}
return thePhoneGapRootPage;
}
private Page getSiteNodeWithPhoneGapRef(Page currentPage) throws RepositoryException {
if (currentPage.getDepth() == 0) {
return null;
}
if (this.getContentSyncConfigPath(currentPage) != null) {
return currentPage;
}
return this.getSiteNodeWithPhoneGapRef(currentPage.getParent());
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
private AppInfo initiateBuild(Session jcrSession, PGBuildUserAccount getPGAccountInfo, Page thePhoneGapRootPage, String buildId) throws RepositoryException, PGException, Exception {
PGBRequest request;
AppInfo appInfo = null;
if (thePhoneGapRootPage == null) throw new PGException("PhoneGap config not found at " + (Object)thePhoneGapRootPage);
if (this.getContentSyncConfigPath(thePhoneGapRootPage) == null) throw new PGException("Export config property not found on " + (Object)thePhoneGapRootPage);
ContentBody buildContentStreamSource = this.getBuildContentAsStreamPartSource(thePhoneGapRootPage);
String title = this.getTitle(thePhoneGapRootPage);
Resource res = thePhoneGapRootPage.getContentResource();
Node thePhoneGapRootNode = (Node)res.adaptTo(Node.class);
if (null == buildId) {
request = new PGBRequest(this.clientBuilderFactory.newBuilder(), phonegapBuildHost, phonegapBuildPort, getPGAccountInfo, this.useSsl);
PostAppBuild build = new PostAppBuild();
appInfo = build.createAppBuild(request, buildContentStreamSource, title);
buildId = appInfo.getId();
thePhoneGapRootNode.setProperty("phonegap-buildId", buildId);
} else {
request = new PGBRequest(this.clientBuilderFactory.newBuilder(), phonegapBuildHost, phonegapBuildPort, getPGAccountInfo, this.useSsl);
PostAppBuild build = new PostAppBuild();
appInfo = build.updateAppBuild(request, buildId, buildContentStreamSource, title);
}
LOGGER.debug(String.valueOf(appInfo));
thePhoneGapRootNode.setProperty("phonegap-lastBuilt", Calendar.getInstance());
thePhoneGapRootNode.setProperty("phonegap-builtBy", jcrSession.getUserID());
jcrSession.save();
if (!(buildContentStreamSource instanceof FileBody)) return appInfo;
File file = ((FileBody)buildContentStreamSource).getFile();
file.delete();
return appInfo;
}
private ContentBody getBuildContentAsStreamPartSource(Page thePhoneGapRootPage) throws RepositoryException, PGException {
FileBody buildFileContentBody;
block5 : {
buildFileContentBody = null;
try {
String contentSyncConfigPath = this.getContentSyncConfigPath(thePhoneGapRootPage);
if (contentSyncConfigPath != null) {
Resource configResource = thePhoneGapRootPage.getContentResource().getResourceResolver().getResource(contentSyncConfigPath);
if (configResource.isResourceType("contentsync/config")) {
buildFileContentBody = new FileBody(this.getContentSyncContentAsFile(configResource));
break block5;
}
throw new PGException("Resource referenced at " + contentSyncConfigPath + " is not of type: " + "contentsync/config");
}
throw new PGException("Export template not set: " + (Object)thePhoneGapRootPage);
}
catch (PGException ex) {
throw ex;
}
catch (Exception ex) {
throw new PGException("Failed to get build content using " + (Object)thePhoneGapRootPage, ex);
}
}
return buildFileContentBody;
}
private String getTitle(Page thePhoneGapRootPage) throws RepositoryException {
return (String)thePhoneGapRootPage.getProperties().get("phonegap-title", (Object)thePhoneGapRootPage.getName());
}
private String getPhoneGapBuildId(Page thePhoneGapRootPage) {
String buildId = null;
if (thePhoneGapRootPage.getProperties().containsKey((Object)"phonegap-buildId")) {
buildId = (String)thePhoneGapRootPage.getProperties().get("phonegap-buildId", String.class);
}
return buildId;
}
public File getContentSyncContentAsFile(Resource configResource) throws Exception {
File file;
block5 : {
file = null;
try {
Session session = this.userSession;
this.contentSyncManagerService.updateCache(configResource, session);
String zipPath = this.contentSyncManagerService.getZip(configResource, null, session);
Node zipNode = null;
try {
zipNode = (Node)session.getItem(zipPath);
}
catch (RepositoryException ex) {
zipNode = (Node)session.getItem("/var/contentsync" + zipPath);
}
Node contentNode = zipNode.getNode("jcr:content");
if (contentNode.isNodeType("nt:resource")) {
Property property = contentNode.getProperty("jcr:data");
file = File.createTempFile("cq" + this.getClass().getSimpleName(), ".zip");
InputStream in = property.getStream();
FileOutputStream out = new FileOutputStream(file);
IOUtils.copy((InputStream)in, (OutputStream)out);
in.close();
out.close();
break block5;
}
throw new PGException("Node " + (Object)contentNode + " is not of type: " + "nt:resource");
}
catch (Exception ex) {
throw new PGException("Failed to get ContentSync zip for config " + (Object)configResource, ex);
}
}
return file;
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (Session.class == type) {
return (AdapterType)this.userSession;
}
return null;
}
}