MobileClientCloudServiceTargetHandler.java
12.7 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
/*
* 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
* 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.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.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.HashMap;
import java.util.Map;
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.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="cloudservice.id", value={"testandtarget"})})
public class MobileClientCloudServiceTargetHandler
extends AbstractMobileClientCloudServiceHandler {
public static final String ID_TARGET_CLOUD_SERVICE = "testandtarget";
private static final String PROPERTY_CLIENTCODE = "target.clientcode";
private static final String PROPERTY_EMAIL = "target.email";
private static final String PROPERTY_PASSWORD = "target.password";
private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/testandtarget";
private static final String DEFAULT_FRAMEWORK_NAME = "framework";
private static final String DEFAULT_CONFIG_TEMPLATE = "/libs/cq/analytics/templates/testandtarget";
private static final String DEFAULT_CONFIG_TITLE = "Provisioned Mobile Target Configuration";
private static final String SEGMENTS_POLL_CONFIG_NAME = "segmentsPollConfig";
private static final String PUBLIC_NAME = "public";
private static final String DEFAULT_FRAMEWORK_TEMPLATE = "/libs/cq/analytics/templates/tntframework";
private static final String DEFAULT_FRAMEWORK_TITLE = "Provisioned Mobile Target Framework";
private static final String MAPPINGS_SLING_RT = "cq/analytics/components/mappings/maparsys";
private static final String CQMAPPINGS_SLING_RT = "cq/analytics/components/mappings/cbmappings";
private final String[] mandatoryProperties = new String[]{"target.clientcode", "target.email", "target.password"};
@Reference
private CryptoSupport cryptoSupport;
private final Logger logger;
public MobileClientCloudServiceTargetHandler() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Override
public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
this.logger.info("Creating Target Cloud Service Configuration...");
this.checkProperties(properties, i18n);
try {
String pageName;
String clientCode = this.getStringProperty(properties, "target.clientcode");
String pageTitle = this.getStringProperty(properties, "configName");
if (StringUtils.isBlank((CharSequence)pageTitle)) {
pageTitle = "Provisioned Mobile Target Configuration: " + clientCode;
}
if (this.isSetup(resourceResolver, pageName = this.getName(properties, clientCode))) {
this.logger.debug("Found provisioned configuration for Target, skipping creation.");
return null;
}
Page newConfiguration = this.createTargetConfiguration(pageName, properties, pageTitle, resourceResolver);
return resourceResolver.getResource(newConfiguration.getPath());
}
catch (Exception e) {
this.logger.error("Creation of Target Cloud Service Configuration failed: ", (Object)e.getMessage());
throw new MobileAppException("Creation of Target Cloud Service Configuration failed: " + e.getMessage(), e);
}
}
@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)", ");
this.logger.error(message);
throw new MobileAppException(message);
}
}
@Override
public String getServicePath() {
return "/etc/cloudservices/testandtarget";
}
private Page createTargetConfiguration(String pageName, Map<String, Object> properties, String pageTitle, ResourceResolver resolver) throws MobileAppException {
Page clientCodePage = this.getOrCreateConfigurationPage(pageName, properties, pageTitle, resolver);
this.createFramework(resolver, clientCodePage);
return clientCodePage;
}
private Page getOrCreateConfigurationPage(String pageName, Map<String, Object> properties, String pageTitle, ResourceResolver resolver) throws MobileAppException {
try {
Resource content;
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
Page companyPage = pageManager.getPage("/etc/cloudservices/testandtarget/" + pageName);
if (companyPage == null && (content = (companyPage = pageManager.create("/etc/cloudservices/testandtarget", pageName, "/libs/cq/analytics/templates/testandtarget", pageTitle)).getContentResource()) != null) {
Resource publicConfig;
ModifiableValueMap vm = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
vm.put((Object)"apiKind", (Object)"REST");
vm.put((Object)"email", (Object)this.getStringProperty(properties, "target.email"));
String password = this.getStringProperty(properties, "target.password");
vm.put((Object)"password", (Object)this.cryptoSupport.protect(password));
Resource segmentsPollConfig = resolver.getResource(content, "segmentsPollConfig");
if (segmentsPollConfig == null) {
HashMap<String, Object> pollConfigProps = new HashMap<String, Object>();
pollConfigProps.put("jcr:mixinTypes", new String[]{"cq:PollConfig"});
pollConfigProps.put("enabled", true);
pollConfigProps.put("interval", 60);
pollConfigProps.put("source", "adobe-target-segments:" + companyPage.getPath());
pollConfigProps.put("target", "/etc/segmentation/adobe-target/" + this.getStringProperty(properties, "target.clientcode"));
resolver.create(content, "segmentsPollConfig", pollConfigProps);
}
if ((publicConfig = resolver.getResource(content, "public")) == null) {
HashMap<String, String> publicProps = new HashMap<String, String>();
publicProps.put("clientcode", this.getStringProperty(properties, "target.clientcode"));
publicProps.put("jcr:title", pageTitle);
resolver.create(content, "public", publicProps);
}
}
return companyPage;
}
catch (CryptoException e) {
this.logger.error("Unable to encrypt secret.", (Throwable)e);
throw new MobileAppException("Unable to encrypt secret", (Throwable)e);
}
catch (WCMException e) {
this.logger.error("Unable to create configuration.", (Throwable)e);
throw new MobileAppException("Unable to create configuration", (Throwable)e);
}
catch (PersistenceException e) {
this.logger.error("Unable to create the segments poll configuration.", (Throwable)e);
throw new MobileAppException("Unable to create the segments poll configuration", (Throwable)e);
}
}
private Page createFramework(ResourceResolver resourceResolver, Page parentPage) throws MobileAppException {
try {
Resource content;
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
Page frameworkPage = pageManager.create(parentPage.getPath(), "framework", "/libs/cq/analytics/templates/tntframework", "Provisioned Mobile Target Framework");
if (frameworkPage != null && (content = frameworkPage.getContentResource()) != null) {
Resource mappings = ResourceUtil.getOrCreateResource((ResourceResolver)resourceResolver, (String)(content.getPath() + "/" + "public" + "/mappings"), (String)"cq/analytics/components/mappings/maparsys", (String)"cq/analytics/components/mappings/maparsys", (boolean)false);
if (mappings != null && !ResourceUtil.isNonExistingResource((Resource)mappings)) {
HashMap<String, String> vm = new HashMap<String, String>();
vm.put("sling:resourceType", "cq/analytics/components/mappings/cbmappings");
vm.put("jcr:primaryType", "cq:Component");
vm.put("cq:componentPath", "cq/personalization/components/contextstores/profiledata");
vm.put("cq:componentName", "Profile Data");
vm.put("cq:componentIcon", "/libs/cq/ui/widgets/themes/default/icons/16x16/components.png");
vm.put("profile.age", "profile.age");
vm.put("profile.gender", "profile.gender");
ResourceUtil.getOrCreateResource((ResourceResolver)resourceResolver, (String)(mappings.getPath() + "/cq_personalization_components_contextstores_profiledata"), vm, (String)"cq/analytics/components/mappings/cbmappings", (boolean)true);
}
Resource publicRes = content.getChild("public");
ModifiableValueMap mvp = (ModifiableValueMap)resourceResolver.getResource(publicRes.getPath()).adaptTo(ModifiableValueMap.class);
mvp.put((Object)"jcr:title", content.getValueMap().get("jcr:title", String.class));
}
return frameworkPage;
}
catch (PersistenceException e) {
this.logger.error("Unable to create mapping.", (Throwable)e);
throw new MobileAppException("Unable to create mapping", (Throwable)e);
}
catch (WCMException e) {
this.logger.error("Unable to create framework.", (Throwable)e);
throw new MobileAppException("Unable to create framework", (Throwable)e);
}
}
private boolean isSetup(ResourceResolver resourceResolver, String pageName) {
String defaultPath = "/etc/cloudservices/testandtarget";
defaultPath = defaultPath + "/" + pageName;
defaultPath = defaultPath + "/framework";
return resourceResolver.getResource(defaultPath = defaultPath + "/jcr:content/mappings/cq_personalization_components_contextstores_profiledata") != null;
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
}