MobileClientCloudServicePGBuildHandler.java
7.63 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* 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.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.mobilecloudservices.impl;
import com.adobe.cq.mobile.mobilecloudservices.impl.AbstractMobileClientCloudServiceHandler;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
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.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="cloudservice.id", value={"phonegap-build"})})
public class MobileClientCloudServicePGBuildHandler
extends AbstractMobileClientCloudServiceHandler {
public static final String ID_PGBUILD_CLOUD_SERVICE = "phonegap-build";
private static final String PROPERTY_PG_BUILD_ID = "pgBuildUserId";
private static final String PROPERTY_PG_BUILD_PASSWORD = "pgBuildPassword";
private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/phonegap-build";
private static final String DEFAULT_RESOURCE_TYPE = "mobileapps/components/phonegap/phonegap-build-cloudservice-config";
private static final String DEFAULT_CQ_TEMPLATE = "/libs/mobileapps/templates/phonegap/phonegap-build-cloudservice-config";
private static final String DEFAULT_CONFIG_TITLE = "Provisioned PhoneGap Build Configuration";
private String[] mandatoryProperties = new String[]{"pgBuildUserId", "pgBuildPassword"};
@Reference
private CryptoSupport cryptoSupport;
private final Logger logger;
public MobileClientCloudServicePGBuildHandler() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Override
public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
this.logger.info("Creating PG Build Cloud Service Configuration...");
this.checkProperties(properties, i18n);
String pageTitle = this.getStringProperty(properties, "configName");
if (StringUtils.isBlank((CharSequence)pageTitle)) {
pageTitle = "Provisioned PhoneGap Build Configuration";
}
String pageName = this.getName(properties, "Provisioned PhoneGap Build Configuration");
Page newConfiguration = this.createPGBuildConfiguration(pageName, properties, pageTitle, resourceResolver);
this.saveConfigurationPublicNode((Resource)newConfiguration.adaptTo(Resource.class));
return resourceResolver.getResource(newConfiguration.getPath());
}
@Override
public void checkProperties(Map<String, Object> properties, I18n i18n) throws MobileAppException {
ArrayList<String> missing = new ArrayList<String>(this.mandatoryProperties.length + 1);
for (String nextMandatoryProperty : this.mandatoryProperties) {
String value = this.getStringProperty(properties, nextMandatoryProperty);
if (!StringUtils.isBlank((CharSequence)value)) continue;
missing.add(nextMandatoryProperty);
}
if (!missing.isEmpty()) {
String message = "Missing mandatory parameters: ";
if (i18n != null) {
message = i18n.get(message);
}
message = message + StringUtils.join(missing, (String)", ");
throw new MobileAppException(message);
}
}
@Override
public String getServicePath() {
return "/etc/cloudservices/phonegap-build";
}
private Page createPGBuildConfiguration(String pageName, Map<String, Object> properties, String title, ResourceResolver resolver) throws MobileAppException {
Page pgBuildPage;
PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
I18n i18n = (I18n)resolver.adaptTo(I18n.class);
try {
pgBuildPage = pm.create("/etc/cloudservices/phonegap-build", pageName, "/libs/mobileapps/templates/phonegap/phonegap-build-cloudservice-config", title, true);
}
catch (WCMException wcmEx) {
this.logger.error("Could not create PG Build cloud configuration: " + pageName);
throw new MobileAppException(i18n.get("Could not create PG Build cloud configuration: {0}", "config name", new Object[]{pageName}));
}
Session session = (Session)resolver.adaptTo(Session.class);
String userId = session.getUserID();
Calendar now = Calendar.getInstance();
String password = this.getStringProperty(properties, "pgBuildPassword");
String user = this.getStringProperty(properties, "pgBuildUserId");
ModifiableValueMap mvp = (ModifiableValueMap)pgBuildPage.getContentResource().adaptTo(ModifiableValueMap.class);
try {
mvp.put((Object)"pgBuildPassword", (Object)this.cryptoSupport.protect(password));
mvp.put((Object)"pgBuildUserId", (Object)user);
mvp.put((Object)"sling:resourceType", (Object)"mobileapps/components/phonegap/phonegap-build-cloudservice-config");
mvp.put((Object)"jcr:lastModified", (Object)now);
mvp.put((Object)"jcr:lastModifiedBy", (Object)userId);
mvp.put((Object)"cq:lastModified", (Object)now);
mvp.put((Object)"cq:lastModifiedBy", (Object)userId);
mvp.put((Object)"cq:cloudservicename", (Object)"phonegap-build");
if (session.hasPendingChanges()) {
session.save();
}
}
catch (CryptoException cryptoEx) {
this.logger.error("Could not set the PG Build cloud configuration password.");
throw new MobileAppException(i18n.get("Could not set the PG Build cloud configuration password."));
}
catch (RepositoryException repEx) {
this.logger.error("Error saving properties on new cloud config: " + pgBuildPage.getPath());
throw new MobileAppException(i18n.get("Error saving properties on new cloud config: {0}", "path to config", new Object[]{pgBuildPage.getPath()}));
}
return pgBuildPage;
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
}