MobileResourceImpl.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.LabeledResource
* com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
* com.day.cq.commons.inherit.InheritanceValueMap
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.adapter.SlingAdaptable
* 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.apache.sling.api.wrappers.ValueMapDecorator
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl;
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.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobilePlatformInternalAdapterFactory;
import com.day.cq.commons.LabeledResource;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.adapter.SlingAdaptable;
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.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MobileResourceImpl
extends SlingAdaptable
implements MobileResource {
private static final Logger log = LoggerFactory.getLogger(MobileResourceImpl.class);
private final Resource mobileRes;
private final LabeledResource labeledRes;
private final ResourceResolver resolver;
private final Map<String, String> instanceTypeMap;
private final MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory;
private ValueMap properties;
public MobileResourceImpl(Resource res, Map<String, String> instanceTypeMap, MobilePlatformInternalAdapterFactory mobilePlatformInternalAdapterFactory) {
if (res == null) {
throw new IllegalArgumentException("Cannot instantiate mobile resource. Resource cannot be null.");
}
this.mobileRes = res;
this.labeledRes = (LabeledResource)res.adaptTo(LabeledResource.class);
this.resolver = res.getResourceResolver();
this.instanceTypeMap = instanceTypeMap;
this.mobilePlatformInternalAdapterFactory = mobilePlatformInternalAdapterFactory;
}
public String getPath() {
return this.labeledRes.getPath();
}
public String getName() {
return this.labeledRes.getName();
}
public String getTitle() {
return this.labeledRes.getTitle();
}
public String getDescription() {
return this.labeledRes.getDescription();
}
@Override
public ValueMap getProperties() {
if (this.properties == null) {
Resource resContent;
MobileAppProvider appProvider = this.mobilePlatformInternalAdapterFactory.getAdapter((Object)this.mobileRes, MobileAppProvider.class);
this.properties = appProvider != null ? appProvider.getProperties() : ((resContent = this.getContentResource()) != null && !ResourceUtil.isNonExistingResource((Resource)resContent) ? resContent.getValueMap() : new ValueMapDecorator(new HashMap()));
}
return this.properties;
}
@Override
public /* varargs */ boolean isA(String ... types) {
Set<String> allTypes = this.getAppTypes();
Resource contentRes = this.getContentResource();
if (types != null) {
String[] arr$ = types;
int len$ = arr$.length;
for (int i$ = 0; i$ < len$; ++i$) {
String type;
String resourceType = type = arr$[i$];
if (this.instanceTypeMap.containsKey(type)) {
resourceType = this.instanceTypeMap.get(type);
}
if (contentRes != null && contentRes.isResourceType(resourceType)) {
return true;
}
for (String typeId : allTypes) {
if (!type.equals(typeId)) continue;
return true;
}
}
}
return false;
}
@Override
public MobileResourceType getMobileResourceType() {
for (MobileResourceType type : MobileResourceType.values()) {
if (!this.isA(type.getType())) continue;
return type;
}
return null;
}
@Override
public Set<String> getAppTypes() {
Resource contentRes = this.getContentResource();
if (contentRes != null && !ResourceUtil.isNonExistingResource((Resource)contentRes)) {
String[] types = (String[])contentRes.getValueMap().get("pge-type", String[].class);
if (types == null) {
types = new String[]{};
}
LinkedHashSet<String> typeIdSet = new LinkedHashSet<String>(types.length);
for (String type : types) {
typeIdSet.add(type);
}
String contentType = contentRes.getResourceType();
if (StringUtils.isNotBlank((String)contentType) && contentRes.isResourceType("mobileapps/core/components/resource")) {
typeIdSet.add(contentRes.getResourceType());
}
return typeIdSet;
}
return new HashSet<String>(0);
}
@Override
public boolean addAppType(String type) throws IllegalArgumentException {
MobileResourceLocator locator = (MobileResourceLocator)this.resolver.adaptTo(MobileResourceLocator.class);
MobileResource group = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.GROUP.getType(), new String[0]);
if (type.equals(MobileResourceType.GROUP.getType())) {
if (group != null) {
log.error("Unable to add an app type to {}. An app-group cannot be a descendant of another app-group.", (Object)this.mobileRes.getPath());
throw new IllegalArgumentException("An app-group cannot be a descendant of an app-group.");
}
return this.addTypeProperty(type);
}
if (type.equals(MobileResourceType.INSTANCE.getType()) || type.equals(MobileResourceType.CONTENT.getType())) {
if (group != null) {
MobileResource instance = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.INSTANCE.getType(), new String[0]);
MobileResource content = locator.findClosestResourceByType(this.mobileRes, MobileResourceType.CONTENT.getType(), new String[0]);
if (instance != null) {
log.error("Unable to add an app type to {}. Resource cannot be a descendant of an app-instance.", (Object)this.mobileRes.getPath());
throw new IllegalArgumentException("A resource cannot be a descendant of an app-instance.");
}
if (content != null) {
log.error("Unable to add an app type to {}. Resource cannot be a descendant of an app-content.", (Object)this.mobileRes.getPath());
throw new IllegalArgumentException("Resource cannot be a descendant of an app-content.");
}
return this.addTypeProperty(type);
}
log.error("Unable to add an app type to {}. Resource must be a descendant of an app-group.", (Object)this.mobileRes.getPath());
throw new IllegalArgumentException("Resource must be a descendant of an app-group.");
}
return false;
}
/*
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
private boolean addTypeProperty(String type) {
try {
Resource contentRes = this.getContentResource();
if (contentRes == null || ResourceUtil.isNonExistingResource((Resource)contentRes)) {
log.error("Unable to find content resource for {},", (Object)this.mobileRes.getPath());
return false;
}
ModifiableValueMap properties = (ModifiableValueMap)contentRes.adaptTo(ModifiableValueMap.class);
String[] allTypes = (String[])properties.get("pge-type", String[].class);
if (allTypes == null) {
properties.put((Object)"pge-type", (Object)new String[]{type});
this.resolver.commit();
return true;
}
ArrayList<String> typeList = new ArrayList<String>(allTypes.length);
typeList.addAll(Arrays.asList(allTypes));
if (type != null && !typeList.contains(type)) {
typeList.add(type);
properties.put((Object)"pge-type", (Object)typeList.toArray(new String[typeList.size()]));
this.resolver.commit();
return true;
}
log.debug("App type, {}, not added since it already exists.", (Object)type);
return false;
}
catch (PersistenceException e) {
log.error("Failed to add type to mobile resource.", (Throwable)e);
return false;
}
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == MobileAppProvider.class) {
return (AdapterType)this.mobilePlatformInternalAdapterFactory.getAdapter((Object)this.mobileRes, MobileAppProvider.class);
}
if (type == Resource.class) {
return (AdapterType)this.mobileRes;
}
if (type == ValueMap.class) {
return (AdapterType)this.properties;
}
if (type == InheritanceValueMap.class) {
return (AdapterType)new HierarchyNodeInheritanceValueMap(this.getContentResource());
}
Object ret = super.adaptTo(type);
if (ret == null) {
ret = this.mobileRes.adaptTo(type);
}
return (AdapterType)ret;
}
private Resource getContentResource() {
if ("jcr:content".equals(this.mobileRes.getName()) || this.mobileRes.getChild("jcr:content") == null) {
return this.mobileRes;
}
return this.mobileRes.getChild("jcr:content");
}
}