DeleteMobileAppOperation.java
6.67 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.day.cq.contentsync.config.Config
* com.day.cq.i18n.I18n
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* javax.servlet.http.HttpServletRequest
* 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.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.PostResponse
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.operations;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileApplicationOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.contentsync.config.Config;
import com.day.cq.i18n.I18n;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.servlet.http.HttpServletRequest;
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.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="Deletes Mobile Applications")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:deleteApps"})})
public class DeleteMobileAppOperation
extends MobileApplicationOperation {
public static final String OPERATION_NAME = "deleteApps";
private static final Logger LOGGER = LoggerFactory.getLogger(DeleteMobileAppOperation.class);
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
I18n i18n = new I18n((HttpServletRequest)request);
String[] appInstancesParam = request.getParameterValues("appInstances");
ArrayList<Resource> appInstances = new ArrayList<Resource>();
if (appInstancesParam != null) {
for (String appPath : appInstancesParam) {
Resource appResource = request.getResourceResolver().getResource(appPath);
if (appResource == null) continue;
appInstances.add(appResource);
}
}
for (Resource appResource : appInstances) {
try {
AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter(request.getResourceResolver().adaptTo(Session.class), AppCacheManager.class);
this.deleteApplication(appCacheManager, request.getResourceResolver(), (PostResponse)response, appResource);
continue;
}
catch (Exception ex) {
if (request.getResourceResolver().hasChanges() && request.getResourceResolver().isLive()) {
request.getResourceResolver().revert();
}
String message = i18n.get("Unable to delete application {0}. {1}", "app path and error message", new Object[]{appResource.getPath(), ex.getLocalizedMessage()});
String title = i18n.get("Error");
this.generateError(response, message, title, ex);
return;
}
}
if (request.getResourceResolver().hasChanges() && request.getResourceResolver().isLive()) {
try {
request.getResourceResolver().commit();
}
catch (PersistenceException e) {
request.getResourceResolver().revert();
String message = i18n.get("Unable to persist changes for the application");
String title = i18n.get("Error");
this.generateError(response, message, title, (Throwable)e);
}
}
String message = i18n.get("Successfully removed mobile applications");
String title = i18n.get("Delete");
this.generateResponse(response, 200, message, title);
}
protected void deleteApplication(AppCacheManager appCacheManager, ResourceResolver resourceResolver, PostResponse postResponse, Resource appInstance) throws Exception {
Resource group;
Session session = (Session)resourceResolver.adaptTo(Session.class);
MobileResourceLocator locator = (MobileResourceLocator)resourceResolver.adaptTo(MobileResourceLocator.class);
MobileResource ancestorResourceByType = locator.findClosestResourceByType(appInstance, MobileResourceType.GROUP.getType(), new String[0]);
if (ancestorResourceByType != null) {
group = (Resource)ancestorResourceByType.adaptTo(Resource.class);
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("SELECT * FROM [cq:ContentSyncConfig] AS s WHERE ISDESCENDANTNODE([" + group.getPath() + "])", "JCR-SQL2");
QueryResult res = q.execute();
NodeIterator iter = res.getNodes();
while (iter.hasNext()) {
Resource r = resourceResolver.getResource(iter.nextNode().getPath());
Config config = (Config)r.adaptTo(Config.class);
LOGGER.debug("deleting mobile content sync cache {}", (Object)config.getPath());
appCacheManager.clearCache(config);
postResponse.onDeleted(config.getPath());
}
} else {
throw new Exception("Could not find the application's Group node.");
}
String appPath = group.getPath();
resourceResolver.delete(group);
LOGGER.debug("deleting mobile application {}", (Object)appPath);
postResponse.onDeleted(appPath);
}
}