OpenProjectLinkCommand.java
7.23 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.projects.api.Project
* com.adobe.cq.projects.api.ProjectLink
* com.day.cq.commons.servlets.HtmlStatusResponseHelper
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.commands.WCMCommand
* com.day.cq.wcm.api.commands.WCMCommandContext
* com.day.text.Text
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.HtmlResponse
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.projects.impl.commands;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectLink;
import com.adobe.cq.projects.impl.util.ProjectUtils;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.commands.WCMCommand;
import com.day.cq.wcm.api.commands.WCMCommandContext;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.SlingHttpServletResponse;
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.ValueMap;
import org.apache.sling.api.servlets.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class OpenProjectLinkCommand
implements WCMCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(OpenProjectLinkCommand.class);
private static final String PROJECT_PATH_PARAM = "projectPath";
@Property(value={"GET"})
private static final String CQ_WCMCOMMAND_METHODS = "cq.wcmcommand.methods";
public String getCommandName() {
return "openProjectLink";
}
public HtmlResponse performCommand(WCMCommandContext ctx, SlingHttpServletRequest request, SlingHttpServletResponse response, PageManager pageManager) {
String target;
String path = request.getParameter("path");
if (StringUtils.isEmpty((String)path)) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"No path parameter");
}
ResourceResolver resolver = request.getResourceResolver();
Resource openResource = resolver.getResource(path);
if (openResource == null) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Resource does not exist: " + path));
}
String projectPath = request.getParameter("projectPath");
if (StringUtils.isEmpty((String)projectPath)) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"No project path parameter");
}
Resource projectRes = resolver.getResource(projectPath);
if (projectRes == null) {
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Project does not exist: " + projectPath));
}
ArrayList<ResourceLinkEntry> allLinks = new ArrayList<ResourceLinkEntry>();
Project project = (Project)projectRes.adaptTo(Project.class);
Iterable projectLinks = project.getLinks();
for (ProjectLink link : projectLinks) {
ValueMap linkProps = (ValueMap)link.adaptTo(ValueMap.class);
String consolePath = (String)linkProps.get("consolePath", String.class);
target = (String)linkProps.get("suffix", String.class);
allLinks.add(new ResourceLinkEntry(consolePath, target));
}
try {
Resource gadgets = ProjectUtils.getOrCreateGadgetFolder(projectRes);
Iterator gadgetIterator = gadgets.listChildren();
while (gadgetIterator.hasNext()) {
Resource gadget = (Resource)gadgetIterator.next();
if ("cq/gui/components/projects/admin/pod/channelpod".equals(gadget.getResourceType())) {
Iterator childLinks = gadget.listChildren();
while (childLinks.hasNext()) {
Resource link2 = (Resource)childLinks.next();
ValueMap linkProps = (ValueMap)link2.adaptTo(ValueMap.class);
String consolePath = (String)linkProps.get("consolePath", String.class);
String target2 = (String)linkProps.get("suffix", String.class);
allLinks.add(new ResourceLinkEntry(consolePath, target2));
}
continue;
}
if (!"cq/gui/components/projects/admin/pod/assetpod".equals(gadget.getResourceType())) continue;
ValueMap map = (ValueMap)gadget.adaptTo(ValueMap.class);
String assetPath = (String)map.get("assetPath", String.class);
Resource assetResource = resolver.getResource(assetPath);
if (assetResource == null) {
assetPath = project.getAssetFolder().getPath();
}
allLinks.add(new ResourceLinkEntry("/assets.html", assetPath));
}
for (ResourceLinkEntry link3 : allLinks) {
target = link3.getTarget();
String consolePath = link3.getConsole();
if (!StringUtils.isNotEmpty((String)target) || !Text.isDescendantOrEqual((String)target, (String)path)) continue;
String contextPath = request.getContextPath() != null ? request.getContextPath() : "";
String cmd = request.getParameter("redirectcmd");
if (cmd != null) {
response.setHeader("Location", contextPath + "/bin/wcmcommand?cmd=" + cmd + "&path=" + path);
response.setStatus(302);
return null;
}
if (!StringUtils.isNotEmpty((String)consolePath)) continue;
response.setHeader("Location", contextPath + consolePath + path);
response.setStatus(302);
return null;
}
}
catch (PersistenceException e) {
LOGGER.error("Unable to obtain the gadgets folder", (Throwable)e);
}
return HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"Unable to get link to resource");
}
private class ResourceLinkEntry {
private final String console;
private final String target;
public ResourceLinkEntry(String console, String target) {
this.console = console;
this.target = target;
}
public String getConsole() {
return this.console;
}
public String getTarget() {
return this.target;
}
}
}