MobileResourceLocatorImpl.java
6.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Session
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* 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 java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MobileResourceLocatorImpl
implements MobileResourceLocator {
private static final Logger log = LoggerFactory.getLogger(MobileResourceLocatorImpl.class);
private final ResourceResolver resolver;
private final Session session;
private final Set<String> excludeNodeNames;
private final Set<String> appRootPaths;
private final Set<String> containerTypes;
public MobileResourceLocatorImpl(ResourceResolver resolver, Set<String> excludeNodeNames, Set<String> appRootPaths, Set<String> containerTypes) {
if (appRootPaths == null) {
appRootPaths = new HashSet<String>();
}
this.appRootPaths = appRootPaths;
this.resolver = resolver;
this.session = (Session)resolver.adaptTo(Session.class);
this.excludeNodeNames = excludeNodeNames;
this.containerTypes = containerTypes;
}
private boolean isAppRoot(Resource resource) {
if (resource == null) {
return false;
}
return this.appRootPaths.contains(resource.getPath());
}
@Override
public Iterable<MobileResource> findResourcesByType(Resource resource, String type) {
if (resource == null) {
throw new IllegalArgumentException("The resource cannot be null");
}
ArrayList<MobileResource> allResources = new ArrayList<MobileResource>();
Resource normalizedResource = this.normalizeResource(resource);
this.collectResourcesByType(normalizedResource, type, allResources);
if (this.isAppRoot(normalizedResource)) {
for (String path : this.appRootPaths) {
Resource extraResource;
if (path.equals(resource.getPath()) || (extraResource = resource.getResourceResolver().getResource(path)) == null) continue;
this.collectResourcesByType(extraResource, type, allResources);
}
}
return allResources;
}
@Override
public MobileResource getResourceByType(Resource resource, String type, boolean deep) {
Iterator children = resource.listChildren();
while (children.hasNext()) {
MobileResource deepRes;
Resource childRes = (Resource)children.next();
if ("jcr:content".equals(childRes.getName())) continue;
MobileResource mobileRes = (MobileResource)childRes.adaptTo(MobileResource.class);
if (mobileRes.isA(type)) {
return mobileRes;
}
if (!deep || (deepRes = this.getResourceByType((Resource)mobileRes.adaptTo(Resource.class), type, deep)) == null) continue;
return deepRes;
}
return null;
}
@Override
public /* varargs */ MobileResource findAncestorResourceByType(Resource resource, String type, String ... stopTypes) {
if (resource == null) {
throw new IllegalArgumentException("The resource cannot be null");
}
Resource normalizedResource = this.normalizeResource(resource);
for (Resource parentRes = normalizedResource.getParent(); parentRes != null; parentRes = parentRes.getParent()) {
MobileResource parentMobileRes = (MobileResource)parentRes.adaptTo(MobileResource.class);
if (parentMobileRes.isA(type)) {
return parentMobileRes;
}
if (parentMobileRes.isA(stopTypes)) break;
}
log.debug("Mobile ancestor resource cannot be found for type {}.", (Object)type);
return null;
}
@Override
public /* varargs */ MobileResource findClosestResourceByType(Resource resource, String type, String ... stopTypes) {
if (resource == null) {
throw new IllegalArgumentException("The resource cannot be null");
}
MobileResource mobileRes = (MobileResource)resource.adaptTo(MobileResource.class);
if (mobileRes != null && mobileRes.isA(type)) {
return mobileRes;
}
return this.findAncestorResourceByType(resource, type, stopTypes);
}
private Resource normalizeResource(Resource res) {
if ("jcr:content".equals(res.getName())) {
res = res.getParent();
}
return res;
}
private void collectResourcesByType(Resource resource, String type, List<MobileResource> allResources) {
Iterator children = resource.listChildren();
while (children.hasNext()) {
Resource childResource = (Resource)children.next();
if (this.excludeNodeNames.contains(childResource.getName())) continue;
MobileResource mobileResource = (MobileResource)childResource.adaptTo(MobileResource.class);
if (mobileResource.isA(type)) {
allResources.add(mobileResource);
continue;
}
if (!this.isContainer(childResource)) continue;
this.collectResourcesByType(childResource, type, allResources);
}
}
private boolean isContainer(Resource resource) {
MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
if (mobileResource.isA(MobileResourceType.GROUP.getType())) {
return true;
}
for (String containerType : this.containerTypes) {
if (!resource.isResourceType(containerType)) continue;
return true;
}
return false;
}
}