PayloadSummaryServlet.java
13.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.projects.api.Project
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.commons.util.UIHelper
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.projects.impl.servlet;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.impl.servlet.ProjectServletUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.UIHelper;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(name="com.adobe.granite.workflow.PayloadSummaryServlet", immediate=1, label="PayloadSummaryServlet", metatype=0, description="Outputs payload information for project tasks")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"payloadsummary", "largeicon.payloadsummary"}), @Property(name="sling.servlet.extensions", value={"json"})})
public class PayloadSummaryServlet
extends SlingSafeMethodsServlet {
private static final Logger LOGGER = LoggerFactory.getLogger(PayloadSummaryServlet.class);
private static final String LARGEICON_SELECTOR = "largeicon";
private static final String MIMETYPE_DAM_ASSET_MANUSCRIPT = "text/plain";
private static final String NODENAME_MANUSCRIPT_ASSIGNMENT = "jcr:content/assignment";
private static final String KEY_EXTRA = "extra";
private static final String KEY_WRITER = "writer";
private static final String KEY_EDITOR = "editor";
private static final String KEY_STATUS = "status";
private static final String KEY_BRIEF = "brief";
private static final String DAM_PATH = "/content/dam";
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType(request.getResponseContentType());
response.setCharacterEncoding("UTF-8");
boolean largeIcon = false;
String[] selectors = request.getRequestPathInfo().getSelectors();
if (selectors != null) {
for (String selector : selectors) {
if (!"largeicon".equals(selector)) continue;
largeIcon = true;
break;
}
}
try {
JSONWriter jsonWriter = new JSONWriter((Writer)response.getWriter());
Resource resource = request.getResource();
ValueMap values = (ValueMap)resource.adaptTo(ValueMap.class);
int iconSize = 48;
if (largeIcon) {
iconSize = 319;
}
jsonWriter.object();
Project project = (Project)resource.adaptTo(Project.class);
if (project != null) {
this.handleProject(jsonWriter, resource, values, iconSize, project);
} else if (resource.isResourceType("dam:Asset")) {
this.handleDamAsset(jsonWriter, resource, iconSize);
} else if (this.isDAMFolder(resource)) {
this.handeDAMFolder(jsonWriter, resource, values, iconSize);
} else if (resource.isResourceType("cq:Page") || resource.isResourceType("cq:PageContent") || "cq:PageContent".equals(values.get((Object)"jcr:primaryType"))) {
this.handlePage(jsonWriter, resource, values, iconSize);
} else {
String title;
LOGGER.debug("no thumbnail found for resource at {}", (Object)resource.getPath());
jsonWriter.key("icon").value((Object)(resource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png"));
String description = this.findDescription(resource, values);
if (description != null) {
jsonWriter.key("description").value((Object)description);
}
if ((title = this.findTitle(resource, values)) != null) {
jsonWriter.key("title").value((Object)title);
}
}
jsonWriter.endObject();
}
catch (JSONException ex) {
throw new ServletException("JSONException in doGet", (Throwable)ex);
}
}
private boolean handeDAMFolder(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize) throws JSONException {
String title;
if (resource.isResourceType("dam/collection") || resource.isResourceType("dam/smartcollection")) {
jsonWriter.key("openPayloadUrl").value((Object)("/libs/dam/gui/content/collections/collectiondetails.html" + Text.escapePath((String)resource.getPath())));
} else {
jsonWriter.key("openPayloadUrl").value((Object)("/assets.html" + Text.escapePath((String)resource.getPath())));
}
jsonWriter.key("icon").value((Object)(resource.getPath() + ".folderthumbnail.jpg?width=" + iconSize + "&height=" + iconSize));
String description = this.findDescription(resource, values);
if (description != null) {
jsonWriter.key("description").value((Object)description);
}
if ((title = this.findTitle(resource, values)) != null) {
jsonWriter.key("title").value((Object)title);
}
return true;
}
private void handlePage(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize) throws JSONException {
String title;
jsonWriter.key("openPayloadUrl").value((Object)("/sites.html" + Text.escapePath((String)resource.getPath())));
jsonWriter.key("icon").value((Object)(resource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png"));
String description = this.findDescription(resource, values);
if (description != null) {
jsonWriter.key("description").value((Object)description);
}
if ((title = this.findTitle(resource, values)) != null) {
jsonWriter.key("title").value((Object)title);
}
}
private boolean handleProject(JSONWriter jsonWriter, Resource resource, ValueMap values, int iconSize, Project project) throws JSONException {
String title;
String thumbnailUrl;
if (project == null) {
return false;
}
String description = this.findDescription(resource, values);
if (description != null) {
jsonWriter.key("description").value((Object)description);
}
if ((title = this.findTitle(resource, values)) != null) {
jsonWriter.key("title").value((Object)title);
}
String detailsUrl = ProjectServletUtil.getProjectDetailsUrl(resource);
jsonWriter.key("openPayloadUrl").value((Object)(detailsUrl + Text.escapePath((String)resource.getPath())));
Resource projectCover = project.getProjectCover();
if (projectCover != null && (thumbnailUrl = this.getAssetUrl(projectCover, iconSize)) != null) {
jsonWriter.key("icon").value((Object)thumbnailUrl);
}
return true;
}
private boolean isDAMFolder(Resource resource) {
if (resource == null) {
return false;
}
if (resource.getPath().startsWith("/content/dam")) {
if (resource.isResourceType("nt:folder") || resource.isResourceType("dam/collection") || resource.isResourceType("dam/smartcollection")) {
return true;
}
Node node = (Node)resource.adaptTo(Node.class);
if (node != null) {
try {
if (node.isNodeType("nt:folder")) {
return true;
}
}
catch (RepositoryException e) {
LOGGER.debug("failed to check node type for resource " + resource.getPath(), (Throwable)e);
}
}
}
return false;
}
private String findTitle(Resource resource, ValueMap values) {
return this.findProperty(resource, values, "jcr:title");
}
private String findDescription(Resource resource, ValueMap values) {
return this.findProperty(resource, values, "jcr:description");
}
private String findProperty(Resource resource, ValueMap values, String propertyName) {
String propertyValue = this.getStringProperty(values, propertyName);
if (propertyValue != null) {
return (String)values.get(propertyName, String.class);
}
Resource jcrContent = resource.getChild("jcr:content");
if (jcrContent != null) {
ValueMap contentValueMap = (ValueMap)jcrContent.adaptTo(ValueMap.class);
return this.getStringProperty(contentValueMap, propertyName);
}
return null;
}
private String getStringProperty(ValueMap values, String propertyName) {
if (values == null || propertyName == null) {
return null;
}
if (values.containsKey((Object)propertyName)) {
return (String)values.get(propertyName, String.class);
}
return null;
}
private boolean handleDamAsset(JSONWriter jsonWriter, Resource resource, int iconSize) throws JSONException {
ValueMap assignmentMap;
Resource assignmentResource;
String thumbnailUrl;
ValueMap metadataMap;
Asset asset = (Asset)resource.adaptTo(Asset.class);
if (asset == null) {
return false;
}
jsonWriter.key("openPayloadUrl").value((Object)("/assetdetails.html" + Text.escapePath((String)resource.getPath())));
Resource metadataResource = resource.getChild("jcr:content/metadata");
if (metadataResource != null && (metadataMap = (ValueMap)metadataResource.adaptTo(ValueMap.class)) != null) {
String description;
String title = (String)metadataMap.get("dc:title", String.class);
if (title != null) {
jsonWriter.key("title").value((Object)title);
}
if ((description = (String)metadataMap.get("dc:description", String.class)) != null) {
jsonWriter.key("description").value((Object)description);
}
}
if ((thumbnailUrl = this.getAssetUrl(resource, asset, iconSize)) != null) {
jsonWriter.key("icon").value((Object)thumbnailUrl);
}
if ("text/plain".equals(asset.getMimeType()) && (assignmentResource = resource.getChild("jcr:content/assignment")) != null && (assignmentMap = (ValueMap)assignmentResource.adaptTo(ValueMap.class)) != null) {
jsonWriter.key("extra").object();
if (assignmentMap.containsKey((Object)"writer")) {
jsonWriter.key("Writer").value(assignmentMap.get("writer", String.class));
}
if (assignmentMap.containsKey((Object)"editor")) {
jsonWriter.key("Editor").value(assignmentMap.get("editor", String.class));
}
if (assignmentMap.containsKey((Object)"status")) {
jsonWriter.key("Status").value(assignmentMap.get("status", String.class));
}
if (assignmentMap.containsKey((Object)"brief")) {
jsonWriter.key("Brief").value(assignmentMap.get("brief", String.class));
}
jsonWriter.endObject();
}
return true;
}
private String getAssetUrl(Resource assetResource, int iconSize) {
Asset asset = (Asset)assetResource.adaptTo(Asset.class);
return this.getAssetUrl(assetResource, asset, iconSize);
}
private String getAssetUrl(Resource assetResource, Asset asset, int iconSize) {
String thumbnailUrl = null;
if (assetResource != null && asset != null) {
Rendition thumbnailRendition = UIHelper.getBestfitRendition((Asset)asset, (int)iconSize);
if (thumbnailRendition != null) {
try {
thumbnailUrl = asset.getPath() + "/jcr:content/renditions/" + thumbnailRendition.getName();
}
catch (Exception e) {
LOGGER.info("Unable to get thumbnail rendition information", (Throwable)e);
}
} else {
thumbnailUrl = assetResource.getPath() + ".thumb." + iconSize + "." + iconSize + ".png";
}
}
return thumbnailUrl;
}
}