LaunchesListServlet.java
8.99 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchManager
* com.adobe.cq.launches.api.LaunchManagerFactory
* com.adobe.granite.security.user.UserProperties
* com.adobe.granite.security.user.UserPropertiesManager
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.servlets.AbstractListServlet
* com.day.cq.commons.servlets.AbstractListServlet$ListItem
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.text.Text
* javax.servlet.http.HttpServletRequest
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.launches.impl;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.servlets.AbstractListServlet;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Property(name="service.description", value={"List"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"sling/servlet/default"}, selectors={"launches"}, extensions={"json"}, generateComponent=0)
public class LaunchesListServlet
extends AbstractListServlet {
private static final Logger log = LoggerFactory.getLogger(LaunchesListServlet.class);
@Reference
protected LaunchManagerFactory launchManagerFactory;
@Reference
private XSSAPI xssApi;
protected List<AbstractListServlet.ListItem> getItems(SlingHttpServletRequest request, Predicate predicate) throws Exception {
Resource requestRes = request.getResource();
ResourceResolver resolver = request.getResourceResolver();
Page requestPage = (Page)requestRes.adaptTo(Page.class);
if (requestPage == null) {
log.warn("Ignoring '{}' for non-page resource: {}", (Object)this.getClass().getCanonicalName(), (Object)requestRes.getPath());
return Collections.emptyList();
}
ArrayList<AbstractListServlet.ListItem> items = new ArrayList<AbstractListServlet.ListItem>();
int results = 0;
UserPropertiesManager userPropsMgr = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
Resource productionRes = LaunchUtils.getTargetResource(requestRes, null);
if (productionRes != null) {
Page productionPage = (Page)productionRes.adaptTo(Page.class);
if (productionPage != null) {
String title = I18n.get((HttpServletRequest)request, (String)"Production", (String)"Title to use for the production page in context of launches");
items.add(new LaunchItem(this, results, productionPage, title, ":production", userPropsMgr));
++results;
} else {
log.warn("Ignoring production version for non-page resource: {}", (Object)productionRes.getPath());
}
}
LaunchManager launchManager = this.launchManagerFactory.getLaunchManager(resolver);
Collection launches = launchManager.getLaunches(requestRes);
for (Launch l : launches) {
LaunchItem item = new LaunchItem(this, results, l, requestPage, userPropsMgr);
items.add(item);
++results;
}
return items;
}
protected void bindLaunchManagerFactory(LaunchManagerFactory launchManagerFactory) {
this.launchManagerFactory = launchManagerFactory;
}
protected void unbindLaunchManagerFactory(LaunchManagerFactory launchManagerFactory) {
if (this.launchManagerFactory == launchManagerFactory) {
this.launchManagerFactory = null;
}
}
protected void bindXssApi(XSSAPI xSSAPI) {
this.xssApi = xSSAPI;
}
protected void unbindXssApi(XSSAPI xSSAPI) {
if (this.xssApi == xSSAPI) {
this.xssApi = null;
}
}
public class LaunchItem
implements AbstractListServlet.ListItem {
private static final String LAUNCH_PATH = "launchPath";
private static final String ESCAPED_LAUNCH_PATH = "escapedLaunchPath";
private static final String PAGE_PATH = "pagePath";
private static final String ESCAPED_PAGE_PATH = "escapedPagePath";
private final int index;
private final Resource resource;
private final Page page;
private final UserPropertiesManager userPropsMgr;
private final String title;
private final String label;
private final String launchPath;
private final Calendar lastModified;
private final String lastModifiedBy;
final /* synthetic */ LaunchesListServlet this$0;
public LaunchItem(LaunchesListServlet launchesListServlet, int index, Launch launch, Page page, UserPropertiesManager userPropsMgr) throws Exception {
this.this$0 = launchesListServlet;
this.index = index;
this.resource = launch.getResource();
this.page = page;
this.userPropsMgr = userPropsMgr;
this.title = launch.getTitle();
this.label = launch.getResource().getName();
this.launchPath = launch.getResource().getPath();
this.lastModified = launch.getModified();
this.lastModifiedBy = launch.getModifiedBy();
}
public LaunchItem(LaunchesListServlet launchesListServlet, int index, Page page, String title, String label, UserPropertiesManager userPropsMgr) throws Exception {
this.this$0 = launchesListServlet;
this.index = index;
this.resource = (Resource)page.adaptTo(Resource.class);
this.page = page;
this.userPropsMgr = userPropsMgr;
this.title = title;
this.label = label;
this.launchPath = null;
this.lastModified = page.getLastModified();
this.lastModifiedBy = page.getLastModifiedBy();
}
public void write(JSONWriter out, String[] customProps) throws Exception {
UserProperties profile;
out.object();
this.this$0.writeKey(out, "index", this.index);
boolean isLaunch = this.isLaunch();
this.this$0.writeKey(out, "title", this.this$0.xssApi.filterHTML(this.title));
this.this$0.writeKey(out, "label", this.label);
Launch launch = null;
if (isLaunch) {
launch = (Launch)this.resource.adaptTo(Launch.class);
this.this$0.writeKey(out, "launchPath", this.launchPath);
this.this$0.writeKey(out, "escapedLaunchPath", Text.escapePath((String)this.launchPath));
}
Resource res = this.page.getContentResource().getParent();
Resource pageRes = isLaunch ? LaunchUtils.getLaunchResource(launch, res) : LaunchUtils.getTargetResource(res, null);
this.this$0.writeKey(out, "pagePath", pageRes.getPath());
this.this$0.writeKey(out, "escapedPagePath", Text.escapePath((String)pageRes.getPath()));
this.this$0.writeKey(out, "jcr:lastModified", this.lastModified.getTimeInMillis());
String formattedName = this.lastModifiedBy;
if (this.userPropsMgr != null && (profile = this.userPropsMgr.getUserProperties(this.lastModifiedBy, "profile")) != null) {
formattedName = profile.getDisplayName();
}
this.this$0.writeKey(out, "jcr:lastModifiedBy", this.this$0.xssApi.filterHTML(formattedName));
out.endObject();
}
public Resource getResource() {
return this.resource;
}
private boolean isLaunch() {
return !":production".equals(this.label);
}
}
}