LaunchUtils.java
6.45 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchException
* com.day.text.Text
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.launches.utils;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.day.text.Text;
import java.util.LinkedList;
import java.util.ListIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LaunchUtils {
private static final Logger log = LoggerFactory.getLogger(LaunchUtils.class);
private static final int LAUNCH_NAME_LEVEL = 2;
public static Resource getLaunchResource(Resource resource) {
if (resource == null) {
throw new IllegalArgumentException("Resource cannot be null");
}
while (resource != null) {
Resource contentRes = resource.getChild("jcr:content");
if (contentRes != null && contentRes.isResourceType("wcm/launches/components/launch")) {
return resource;
}
resource = resource.getParent();
}
return null;
}
public static Resource getProductionResource(Resource resource) {
if (!LaunchUtils.isLaunchResourcePath(resource.getPath())) {
return resource;
}
Resource launchResource = LaunchUtils.getLaunchResource(resource);
return resource.getResourceResolver().getResource(resource.getPath().substring(launchResource.getPath().length()));
}
public static Resource getTargetResource(Resource resource, Launch target) throws LaunchException {
String path = LaunchUtils.getTargetResourcePath(resource, target);
return resource.getResourceResolver().getResource(path);
}
public static String getTargetResourcePath(Resource resource, Launch target) throws LaunchException {
if (!LaunchUtils.isLaunchResourcePath(resource.getPath())) {
return target == null ? resource.getPath() : null;
}
Launch currentLaunch = (Launch)LaunchUtils.getLaunchResource(resource).adaptTo(Launch.class);
String path = resource.getPath();
while (target == null || currentLaunch.compareTo(target) != 0) {
path = path.substring(currentLaunch.getResource().getPath().length());
Resource launchResource = LaunchUtils.getLaunchResource(currentLaunch.getSourceRootResource());
if (launchResource == null) {
if (target == null) break;
throw new LaunchException("The specified target launch is not a parent launch of the resource's launch");
}
currentLaunch = (Launch)launchResource.adaptTo(Launch.class);
}
return path;
}
public static Resource getLaunchResource(Launch launch, Resource resource) throws LaunchException {
if (launch == null || resource == null) {
return null;
}
String path = LaunchUtils.getLaunchResourcePath(launch, resource);
return resource.getResourceResolver().getResource(path);
}
public static String getLaunchResourcePath(Launch launch, Resource resource) throws LaunchException {
if (launch == null || resource == null) {
return null;
}
Resource res = LaunchUtils.getLaunchResource(resource);
Launch resourceLaunch = res == null ? null : (Launch)res.adaptTo(Launch.class);
LinkedList<Launch> parents = new LinkedList<Launch>();
Launch currentLaunch = launch;
while (resourceLaunch == null || currentLaunch.compareTo(resourceLaunch) != 0) {
parents.add(currentLaunch);
Resource launchResource = LaunchUtils.getLaunchResource(currentLaunch.getSourceRootResource());
if (launchResource == null) {
if (resourceLaunch == null) break;
throw new LaunchException("The specified launch is not a child launch of the resource's launch");
}
currentLaunch = (Launch)launchResource.adaptTo(Launch.class);
}
StringBuilder path = new StringBuilder(resource.getPath());
ListIterator reverseIterator = parents.listIterator(parents.size());
while (reverseIterator.hasPrevious()) {
path.insert(0, ((Launch)reverseIterator.previous()).getResource().getPath());
}
return path.toString();
}
public static boolean isLaunchBasedPath(String path) {
return Text.isDescendantOrEqual((String)"/content/launches", (String)path);
}
static boolean isLaunchBasedPath(String path, String launchPath) {
return Text.isDescendantOrEqual((String)launchPath, (String)path);
}
public static boolean isLaunchResourcePath(String path) {
return LaunchUtils.isLaunchBasedPath(path) && StringUtils.isNotEmpty((String)Text.getAbsoluteParent((String)path, (int)3));
}
public static String getLaunchPath(String path) {
log.warn("LaunchUtils.getLaunchPath is deprecated and potentially broken. Please remove usages of this method.");
if (LaunchUtils.isLaunchBasedPath(path)) {
String launchPath = Text.getAbsoluteParent((String)path, (int)2);
return StringUtils.isNotEmpty((String)launchPath) ? launchPath : null;
}
return null;
}
public static String getLaunchPathFromName(String launchName) {
return Text.makeCanonicalPath((String)("/content/launches/" + launchName));
}
public static String getProductionResourcePath(String path) {
return LaunchUtils.isLaunchResourcePath(path) ? StringUtils.substringAfter((String)path, (String)LaunchUtils.getLaunchPath(path)) : path;
}
public static String buildLaunchResourcePath(String path, String launchName) {
log.warn("LaunchUtils#buildLaunchResourcePath is deprecated and potentially broken. Please remove usages of this method.");
if (LaunchUtils.isLaunchResourcePath(path)) {
return LaunchUtils.isLaunchBasedPath(path, LaunchUtils.getLaunchPathFromName(launchName)) ? path : null;
}
return !LaunchUtils.isLaunchBasedPath(path) ? Text.makeCanonicalPath((String)("/content/launches/" + launchName + "/" + path)) : null;
}
}