MobileAppsMetricsOperation.java
9.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.i18n.I18n
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
*/
package com.adobe.cq.mobile.platform.impl.operations;
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.operations.MobileAbstractOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
@Component(metatype=0, label="AEM Mobile Metric Updater")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:appMetrics"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class MobileAppsMetricsOperation
extends MobileAbstractOperation {
private static final String APP_INSTANCE_REPORTS_HOME = "/libs/mobileapps/admin/content/dashboard/tiles/default/appmetrics/jcr:content/reports";
public static final String PARAM_APP_INSTANCE = "appInstance";
public static final String PARAM_MOBILE_SERVICE_CONFIG = "mobileServiceConfig";
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
I18n i18n = new I18n((HttpServletRequest)request);
RequestParameter appInstanceParam = request.getRequestParameter("appInstance");
if (appInstanceParam == null) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"appInstance"});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
Resource appInstance = request.getResourceResolver().getResource(appInstanceParam.toString());
if (appInstance == null) {
String message = i18n.get("Unable to resolve mobile application {0}", null, new Object[]{appInstanceParam.toString()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
MobileResource mobileResource = (MobileResource)appInstance.adaptTo(MobileResource.class);
if (!mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
String message = i18n.get("The resource {0} must be a mobile application", null, new Object[]{mobileResource.getPath()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
String configPath = request.getParameter("mobileServiceConfig");
if (StringUtils.isEmpty((String)configPath)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"mobileServiceConfig"});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
this.updateMobileServicesConfig(response, appInstance, configPath);
this.setupReports(request, request.getResourceResolver(), appInstance, i18n, response);
}
private void setupReports(SlingHttpServletRequest request, ResourceResolver resolver, Resource appInstance, I18n i18n, HtmlResponse response) {
Resource appContent = appInstance.getChild("jcr:content");
String appContentPath = appContent.getPath();
String config = request.getParameter("mobileServiceConfig");
ConfigurationManager configurationManager = (ConfigurationManager)request.getResourceResolver().adaptTo(ConfigurationManager.class);
Configuration configuration = configurationManager.getConfiguration(config);
String reportId = AMSUtils.getReportSuites(configuration);
try {
Node reports;
Session session = (Session)resolver.adaptTo(Session.class);
Node src = session.getNode("/libs/mobileapps/admin/content/dashboard/tiles/default/appmetrics/jcr:content/reports");
String destPath = appContentPath + "/" + "reports" + "/" + reportId;
if (!session.nodeExists(destPath)) {
Node reportNode = JcrUtil.createPath((String)destPath, (String)"nt:unstructured", (Session)session);
reports = JcrUtil.copy((Node)src, (Node)reportNode, (String)null);
} else {
reports = session.getNode(destPath);
this.cleanReports(reports);
}
this.updateDataStore(reports, reportId);
resolver.commit();
}
catch (Exception e) {
String statusTitle = i18n.get("Mobile app instance metrics update error");
String statusMsg = i18n.get("Unable to update the mobile services config for app instance {0}", null, new Object[]{appInstance.getPath()});
this.generateResponse(response, 500, statusMsg, statusTitle);
}
}
private void cleanReports(Node reports) throws RepositoryException {
Node reportNode = reports.getNode("reports");
NodeIterator nodes = reportNode.getNodes();
while (nodes.hasNext()) {
Node childNode = (Node)nodes.next();
if (!childNode.hasProperty("cq:lastAttempt")) continue;
javax.jcr.Property property = childNode.getProperty("cq:lastAttempt");
property.remove();
}
}
private void updateDataStore(Node node, String reportId) throws RepositoryException {
PropertyIterator dataStoreIterator = node.getProperties("data_store");
while (dataStoreIterator.hasNext()) {
javax.jcr.Property property = (javax.jcr.Property)dataStoreIterator.next();
property.setValue("dms/" + reportId);
}
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
Node childNode = (Node)nodes.next();
this.updateDataStore(childNode, reportId);
}
}
private void updateMobileServicesConfig(HtmlResponse response, Resource appInstance, String configPath) {
Resource appContent = appInstance.getChild("jcr:content");
String appContentPath = appContent.getPath();
ModifiableValueMap properties = (ModifiableValueMap)appContent.adaptTo(ModifiableValueMap.class);
boolean modified = properties.containsKey((Object)"mobileServiceConfig");
properties.put((Object)"mobileServiceConfig", (Object)configPath);
boolean hasCloudConfig = properties.containsKey((Object)"phonegapConfig") || properties.containsKey((Object)"notificationConfig");
String[] configs = (String[])properties.get("cq:cloudserviceconfigs", String[].class);
ArrayList<String> newConfigs = new ArrayList<String>();
if (configs != null) {
for (String config : configs) {
if (StringUtils.startsWith((String)config, (String)"/etc/cloudservices/mobileservices")) continue;
newConfigs.add(config);
}
}
newConfigs.add(configPath);
String[] props = new String[newConfigs.size()];
props = newConfigs.toArray(props);
properties.put((Object)"cq:cloudserviceconfigs", (Object)props);
String property = appContentPath + "/" + "mobileServiceConfig";
String cloud = appContentPath + "/" + "cq:cloudserviceconfigs";
if (modified) {
response.onModified(property);
} else {
response.onCreated(property);
}
if (hasCloudConfig) {
response.onModified(cloud);
} else {
response.onCreated(cloud);
}
}
}