MobileAppsCloudServiceOperation.java
17.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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.i18n.I18n
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletRequest
* 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.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.servlets.post.Modification
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.operations;
import com.adobe.cq.mobile.mobilecloudservices.impl.MobileClientCloudServiceHandler;
import com.adobe.cq.mobile.mobileservices.impl.util.AMSUtils;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
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.commons.osgi.OsgiUtil;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="AEM Mobile Cloud Service Updater")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:cloudService"})})
public class MobileAppsCloudServiceOperation
extends MobileAbstractOperation {
public static final String OPERATION_NAME = "cloudService";
private final String PROP_CREATE_CONFIG = "cloudConfigCreate";
private final String PROP_CLOUD_CONFIG_FIELD = "cloudConfigField";
private static final String PROP_CLOUD_SERVICE_NAME = "cloudServiceName";
private static final String PROP_CLOUD_CONFIG = "cloudConfig";
private static final String PROP_ACTION = "configAction";
private static final String PROP_ACTION_UNLINK = "unlink";
private static final String PROP_ACTION_CREATESET = "set";
private final String PROP_TARGET_PATHS = "targetPaths";
private final String PROP_CLOUD_CONFIG_SUFFIX = "cloudConfigSuffix";
private static final String CQ_APPS_ADMIN_CLOUDSERVICE_EDIT = "cq-apps-admin-addcloudservice";
@Reference(name="mobileClientCloudServiceHandler", referenceInterface=MobileClientCloudServiceHandler.class, bind="bindProvisioningHandler", unbind="unbindProvisioningHandler", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private final Map<String, MobileClientCloudServiceHandler> mobileCloudServiceHandlers = new HashMap<String, MobileClientCloudServiceHandler>();
private final Logger logger;
public MobileAppsCloudServiceOperation() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
I18n i18n = new I18n((HttpServletRequest)request);
String errorTitle = i18n.get("Error");
Resource resource = request.getResource();
String action = request.getParameter("configAction");
try {
this.validate(request, resource, action);
}
catch (MobileOperationException mobOpEx) {
this.generateError(response, mobOpEx.getMessage(), errorTitle);
return;
}
if (StringUtils.isBlank((CharSequence)action) || action.equals("set")) {
this.setCloudConfiguration(request, response, changes);
} else if (action.equals("unlink")) {
this.unlinkCloudConfiguration(request, response);
} else {
this.generateError(response, i18n.get("Unknown action for a Cloud Service Operation: {0}", "unknown action requested", new Object[]{action}), errorTitle);
return;
}
}
private void setCloudConfiguration(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
I18n i18n = new I18n((HttpServletRequest)request);
ResourceResolver resolver = request.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
String errorTitle = i18n.get("Error");
String cloudServiceName = request.getParameter("cloudServiceName");
MobileClientCloudServiceHandler handler = this.getMobileClientCloudServiceHandler(cloudServiceName);
boolean createConfig = request.getParameter("cloudConfigCreate").equalsIgnoreCase("true");
String cloudConfigPath = request.getParameter("cloudConfig");
String propertyName = request.getParameter("cloudConfigField");
String cloudConfigSuffix = request.getParameter("cloudConfigSuffix");
if (StringUtils.isBlank((CharSequence)cloudConfigSuffix)) {
cloudConfigSuffix = "";
} else if (!(cloudConfigSuffix = JcrUtil.createValidName((String)cloudConfigSuffix)).startsWith("/")) {
cloudConfigSuffix = "/" + cloudConfigSuffix;
}
String path = null;
try {
if (createConfig) {
Resource newResource = this.createCloudConfigAndChildren(request);
Resource newConfiguration = AMSUtils.getCloudServiceConfiguration(newResource);
cloudConfigPath = newConfiguration.getPath();
if (StringUtils.isBlank((CharSequence)cloudConfigSuffix) && !newResource.getPath().equals(cloudConfigPath)) {
cloudConfigSuffix = newResource.getPath().substring(cloudConfigPath.length());
}
path = cloudConfigPath + cloudConfigSuffix;
} else {
path = cloudConfigPath;
}
}
catch (MobileOperationException mobOpEx) {
this.generateError(response, mobOpEx.getMessage(), errorTitle);
return;
}
try {
ArrayList<String> failedPaths = this.setCloudConfigOnApp(request, propertyName, path, resolver);
Resource resourceToUpdate = resolver.getResource(path);
Map map = request.getParameterMap();
handler.updateConfiguration(resourceToUpdate, map);
if (session.hasPendingChanges()) {
session.save();
}
if (!failedPaths.isEmpty()) {
String failureList = StringUtils.join(failedPaths, (String)", ");
String message = i18n.get("Property failed to be set on the following paths: {0}", "comma delmited list of failed paths", new Object[]{failureList});
this.generateError(response, message, errorTitle);
return;
}
}
catch (RepositoryException repEx) {
String message = repEx.getMessage();
this.generateError(response, message, errorTitle);
return;
}
String redirectTo = this.getRedirect(request);
if (StringUtils.isBlank((CharSequence)redirectTo)) {
redirectTo = "/aem/apps.html/content/mobileapps";
} else if (redirectTo.endsWith(".html")) {
redirectTo = redirectTo + Text.escapePath((String)request.getResource().getPath());
}
String editPath = createConfig ? path : Text.getRelativeParent((String)path, (int)1);
response.addLink("cq-apps-admin-addcloudservice", this.xssAPI.getValidHref(editPath + ".html"), i18n.get("Edit Cloud Configuration"));
String title = i18n.get("Cloud Config Set");
String message = i18n.get("Your App has been updated.");
this.generateResponse(response, 201, message, title, redirectTo, i18n.get("Done"));
}
private void unlinkCloudConfiguration(SlingHttpServletRequest request, HtmlResponse response) {
Resource resource = request.getResource();
String propertyName = request.getParameter("cloudConfigField");
String cloudServiceName = request.getParameter("cloudServiceName");
MobileClientCloudServiceHandler handler = this.getMobileClientCloudServiceHandler(cloudServiceName);
handler.unlinkCloudConfigProperty(resource, propertyName);
}
private void validate(SlingHttpServletRequest request, Resource resource, String action) throws MobileOperationException {
I18n i18n = new I18n((HttpServletRequest)request);
ResourceResolver resolver = request.getResourceResolver();
MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
if (!(mobileResource.isA(MobileResourceType.INSTANCE.getType()) || mobileResource.isA(MobileResourceType.GROUP.getType()) || mobileResource.isA(MobileResourceType.CONTENT.getType()))) {
String message = i18n.get("Unable to resolve mobile application {0}", "Path to Resource", new Object[]{resource.getPath()});
throw new MobileOperationException(message);
}
String propertyName = request.getParameter("cloudConfigField");
if (StringUtils.isBlank((CharSequence)propertyName)) {
String message = i18n.get("Missing property for configuration property name: {0}", "Field Name", new Object[]{"cloudConfigField"});
throw new MobileOperationException(message);
}
String cloudServiceName = request.getParameter("cloudServiceName");
if (StringUtils.isBlank((CharSequence)cloudServiceName)) {
String message = i18n.get("Missing property Cloud Service name. It is mandatory when creating a new Cloud Service");
throw new MobileOperationException(message);
}
if (StringUtils.isBlank((CharSequence)action) || action.equals("set")) {
String createConfigString = request.getParameter("cloudConfigCreate");
if (StringUtils.isBlank((CharSequence)createConfigString)) {
String message = i18n.get("Missing property to indicate creation of service: {0}", "Field Name", new Object[]{"cloudConfigCreate"});
throw new MobileOperationException(message);
}
boolean createConfig = createConfigString.equalsIgnoreCase("true");
if (createConfig) {
MobileClientCloudServiceHandler handler = this.getMobileClientCloudServiceHandler(cloudServiceName);
if (handler == null) {
String message = i18n.get("Invalid Cloud Service specified. It does not have a provisioned handler.");
throw new MobileOperationException(message);
}
Resource cloudService = resolver.resolve(handler.getServicePath());
if (ResourceUtil.isNonExistingResource((Resource)cloudService)) {
String message = i18n.get("Invalid Cloud Service specified. Path '{0}' does not exist.", "Path to service", new Object[]{cloudService});
throw new MobileOperationException(message);
}
} else {
String existingCloudConfig = request.getParameter("cloudConfig");
if (StringUtils.isBlank((CharSequence)existingCloudConfig)) {
String message = i18n.get("Missing property indicating the Cloud Configuration to use.");
throw new MobileOperationException(message);
}
Resource existingConfigRes = resolver.resolve(existingCloudConfig);
if (ResourceUtil.isNonExistingResource((Resource)existingConfigRes)) {
String message = i18n.get("Cloud Configuration specified does not exist: {0}", "Path to missing config", new Object[]{existingCloudConfig});
throw new MobileOperationException(message);
}
}
}
}
private ArrayList<String> setCloudConfigOnApp(SlingHttpServletRequest request, String propertyName, String configPath, ResourceResolver resolver) {
String[] targetPaths = this.getTargetPaths(request);
String cloudServiceName = request.getParameter("cloudServiceName");
MobileClientCloudServiceHandler handler = this.getMobileClientCloudServiceHandler(cloudServiceName);
ArrayList<String> failedPaths = new ArrayList<String>();
for (String nextTarget : targetPaths) {
if (!StringUtils.isNotBlank((CharSequence)nextTarget)) continue;
Resource nextTargetRes = resolver.resolve(nextTarget);
if (ResourceUtil.isNonExistingResource((Resource)nextTargetRes)) {
failedPaths.add(nextTarget);
this.logger.error("Could not apply cloud configuration '" + configPath + "' to '" + nextTarget + "'");
continue;
}
try {
handler.saveCloudConfigProperty(nextTargetRes, propertyName, configPath);
continue;
}
catch (MobileAppException mobileAppEx) {
failedPaths.add(nextTarget);
this.logger.error("Error saving cloud config: " + mobileAppEx.getLocalizedMessage());
this.logger.error("Could not apply cloud configuration '" + configPath + "' to '" + nextTarget + "'");
}
}
return failedPaths;
}
private Resource createCloudConfigAndChildren(SlingHttpServletRequest request) throws MobileOperationException {
Resource newConfig;
I18n i18n = new I18n((HttpServletRequest)request);
ResourceResolver resolver = request.getResourceResolver();
String cloudServiceName = request.getParameter("cloudServiceName");
try {
MobileClientCloudServiceHandler handler = this.getMobileClientCloudServiceHandler(cloudServiceName);
Map map = request.getParameterMap();
newConfig = handler.createConfiguration(map, i18n, resolver);
}
catch (MobileAppException mobileAppEx) {
throw new MobileOperationException(mobileAppEx.getMessage());
}
return newConfig;
}
private String[] getTargetPaths(SlingHttpServletRequest request) {
int length;
String[] propertyTargetPaths = request.getParameterValues("targetPaths");
int n = length = propertyTargetPaths == null ? 0 : propertyTargetPaths.length;
if (propertyTargetPaths != null) {
for (String nextPath : propertyTargetPaths) {
if (!StringUtils.isBlank((CharSequence)nextPath)) continue;
--length;
}
}
String[] targetPaths = new String[length + 1];
if (length > 0) {
for (int i = 0; i < propertyTargetPaths.length; ++i) {
if (!StringUtils.isNotBlank((CharSequence)propertyTargetPaths[i])) continue;
targetPaths[i] = propertyTargetPaths[i];
}
}
targetPaths[length] = request.getResource().getPath();
return targetPaths;
}
private MobileClientCloudServiceHandler getMobileClientCloudServiceHandler(String cloudServiceName) {
return this.mobileCloudServiceHandlers.get(cloudServiceName);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindProvisioningHandler(MobileClientCloudServiceHandler handler, Map<String, Object> properties) {
String serviceId = OsgiUtil.toString((Object)properties.get("cloudservice.id"), (String)null);
if (serviceId == null) {
this.logger.warn("Unnamed MobileClientCloudServiceHandler. Dropping it.");
return;
}
Map<String, MobileClientCloudServiceHandler> map = this.mobileCloudServiceHandlers;
synchronized (map) {
this.mobileCloudServiceHandlers.put(serviceId, handler);
}
this.logger.info("bound new MobileClientCloudServiceHandler: {}, id={}", (Object)handler.getClass().getName(), (Object)serviceId);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindProvisioningHandler(MobileClientCloudServiceHandler handler, Map<String, Object> properties) {
String serviceId = OsgiUtil.toString((Object)properties.get("cloudservice.id"), (String)null);
Map<String, MobileClientCloudServiceHandler> map = this.mobileCloudServiceHandlers;
synchronized (map) {
this.mobileCloudServiceHandlers.remove(serviceId);
}
this.logger.info("unbound MobileClientCloudServiceHandler: {}, id={}", (Object)handler.getClass().getName(), (Object)serviceId);
}
}