DPSUtil.java
15.6 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.dps.client.producer.EntityType
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* javax.servlet.ServletOutputStream
* javax.servlet.http.HttpServletResponse
* org.apache.commons.io.IOUtils
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps.impl.utils;
import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSEntity;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSObject;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.export.ExportOptions;
import com.adobe.dps.client.producer.EntityType;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DPSUtil {
private static final String DPS_METADATA_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final String LINE_SEP = System.getProperty("line.separator");
private static final Logger LOGGER = LoggerFactory.getLogger(DPSUtil.class);
public static boolean isADPSResource(Node currentNode) throws RepositoryException {
return DPSUtil.getDPSResourceType(currentNode) != null;
}
public static boolean isADPSResource(Page currentPage) {
return DPSUtil.getDPSResourceType(currentPage) != null;
}
public static String getDPSResourceType(Page page) {
String dpsResourceType = null;
if (page != null) {
dpsResourceType = (String)page.getProperties().get("dps-resourceType", String.class);
}
return dpsResourceType;
}
public static String getDPSResourceType(Node currentNode) throws RepositoryException {
Node contentNode;
boolean hasDPSResourceTypeProp;
String dpsResourceType = null;
boolean hasContentNode = currentNode.hasNode("jcr:content");
if (hasContentNode && (hasDPSResourceTypeProp = (contentNode = currentNode.getNode("jcr:content")).hasProperty("dps-resourceType"))) {
dpsResourceType = contentNode.getProperty("dps-resourceType").getString();
}
return dpsResourceType;
}
public static Node getDPSFolioRefNode(Node currentNode) throws RepositoryException {
Node issueNode = null;
if (currentNode.getDepth() > 0) {
String type = DPSUtil.getDPSResourceType(currentNode);
if (type != null && type.equals("dps:Project")) {
issueNode = currentNode;
}
if (issueNode == null) {
issueNode = DPSUtil.getDPSFolioRefNode(currentNode.getParent());
}
}
return issueNode;
}
public static String getProperty(Node node, String propKey, String defaultValue) throws RepositoryException, Exception {
String value = defaultValue;
if (node.hasProperty(propKey)) {
value = node.getProperty(propKey).getString();
} else if (value == null) {
throw new Exception("Required property '" + propKey + "' not found for: " + (Object)node);
}
return value;
}
public static String getDateAsDPSString(Date date) {
String stringDate = "";
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
stringDate = formatter.format(date);
}
return stringDate;
}
public static DPSObject getDPSObject(Page page) {
DPSObject dpsObject = null;
String resourceType = DPSUtil.getDPSResourceType(page);
if (resourceType != null) {
if (resourceType.equals("dps:Project")) {
dpsObject = (DPSObject)page.adaptTo(DPSProject.class);
} else if (resourceType.equals("dps:Article")) {
dpsObject = (DPSObject)page.adaptTo(DPSArticle.class);
} else if (resourceType.equals("dps:Banner")) {
dpsObject = (DPSObject)page.adaptTo(DPSBanner.class);
} else if (resourceType.equals("dps:Collection")) {
dpsObject = (DPSObject)page.adaptTo(DPSCollection.class);
}
}
return dpsObject;
}
public static DPSProject getDPSProject(DPSObject dpsObject) {
DPSProject project = null;
if (dpsObject != null) {
if (dpsObject instanceof DPSProject) {
project = (DPSProject)dpsObject;
} else if (dpsObject instanceof DPSEntity) {
project = ((DPSEntity)dpsObject).getProject();
}
}
return project;
}
public static Configuration getDPSConfiguration(ConfigurationManager configurationManager, String cloudServiceConfigPath) throws RepositoryException {
if (StringUtils.isEmpty((CharSequence)cloudServiceConfigPath)) {
LOGGER.debug("Path to cloudServiceConfigPath was null or empty.");
return null;
}
Configuration dpsConfig = null;
try {
Configuration config = configurationManager.getConfiguration(cloudServiceConfigPath);
if (config == null) {
throw new DPSException("Experience Manager Mobile CloudService configuration is not found at: " + cloudServiceConfigPath);
}
String type = (String)config.getProperties().getInherited("sling:resourceType", String.class);
if (type.endsWith("dps-cloudservice-config")) {
dpsConfig = config;
}
}
catch (DPSException ex) {
LOGGER.warn("Failed to get Experience Manager Mobile account configuration", (Throwable)ex);
}
return dpsConfig;
}
public static boolean isImageSet(Resource imageResource) {
boolean isImageSet = false;
if (imageResource != null) {
if (ResourceUtil.getValueMap((Resource)imageResource).containsKey((Object)"fileReference")) {
isImageSet = true;
} else {
Node imageNode = (Node)imageResource.adaptTo(Node.class);
try {
isImageSet = imageNode.hasNode("./file");
}
catch (RepositoryException e) {
// empty catch block
}
}
}
return isImageSet;
}
public static Long getJSONDateValue(Date date) {
return date == null ? null : Long.valueOf(date.getTime());
}
public static long sendZip(Session userSession, HttpServletResponse response, String zipPath, String zipName) throws RepositoryException, IOException {
if (zipName != null) {
response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
}
response.setContentType("application/zip");
long size = DPSUtil.writeNodeToStream(userSession, (OutputStream)response.getOutputStream(), zipPath);
response.setContentLength((int)size);
return size;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static long writeNodeToStream(Session userSession, OutputStream outStream, String path) throws RepositoryException, IOException {
long size;
block5 : {
String binaryDataPath = path + "/jcr:content/jcr:data";
Binary binary = null;
InputStream is = null;
size = 0;
try {
if (userSession.itemExists(binaryDataPath)) {
binary = userSession.getProperty(binaryDataPath).getBinary();
size = binary.getSize();
is = binary.getStream();
IOUtils.copy((InputStream)is, (OutputStream)outStream);
break block5;
}
throw new RepositoryException("Content missing: " + binaryDataPath);
}
finally {
IOUtils.closeQuietly((InputStream)is);
if (binary != null) {
binary.dispose();
}
}
}
return size;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static void writeFileToNode(Session userSession, File file, String path) throws RepositoryException, IOException {
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
JcrUtil.createPath((String)path, (String)"sling:Folder", (String)"nt:file", (Session)userSession, (boolean)false);
Node contentNode = JcrUtil.createPath((String)(path + "/jcr:content"), (String)"nt:resource", (Session)userSession);
contentNode.setProperty("jcr:mimeType", "application/zip");
contentNode.setProperty("jcr:data", userSession.getValueFactory().createBinary((InputStream)fin));
}
finally {
IOUtils.closeQuietly((InputStream)fin);
}
}
public static String getZipName(Page page, ExportOptions exportOptions) {
String name = null;
if (exportOptions.getExportMode().equals((Object)ExportOptions.ExportMode.EXPORT_RESOURCES)) {
name = "HTMLResources.zip";
} else {
boolean exportFolio = exportOptions.getExportMode().equals((Object)ExportOptions.ExportMode.EXPORT_ARTICLE);
if (exportFolio) {
name = page.getName() + ".folio.zip";
}
}
return name;
}
public static final Date getLastModificationDate(DPSProject dpsProject) {
Date ret = dpsProject.getLastCQModified();
for (DPSArticle article : dpsProject.getArticles()) {
Date articleDate = article.getLastCQModified();
if (ret != null && !ret.before(articleDate)) continue;
ret = articleDate;
}
return ret;
}
public static Resource getReferencedResource(SlingHttpServletRequest request) {
ResourceResolver resolver = request.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
RequestPathInfo requestPathInfo = request.getRequestPathInfo();
String targetResourceId = requestPathInfo.getSuffix();
if (targetResourceId == null || targetResourceId.length() == 0) {
return null;
}
targetResourceId = targetResourceId.substring(1);
Resource targetResource = null;
try {
Node targetResourceNode = session.getNodeByIdentifier(targetResourceId);
if (targetResourceNode != null) {
targetResource = resolver.getResource(targetResourceNode.getPath());
}
}
catch (RepositoryException e) {
// empty catch block
}
return targetResource;
}
public static ResourceResolver getResourceResolver(ResourceResolverFactory resourceResolverFactory, Session session) {
ResourceResolver resolver = null;
HashMap<String, Session> authInfo = new HashMap<String, Session>();
authInfo.put("user.jcr.session", session);
try {
resolver = resourceResolverFactory.getResourceResolver(authInfo);
}
catch (LoginException ex) {
LOGGER.error("Unable to obtain the resource resolver", (Throwable)ex);
}
return resolver;
}
public static String getEntityURI(DPSEntity dpsEntity) {
return DPSUtil.getEntityURI(dpsEntity.getProjectId(), DPSUtil.getEntityType(dpsEntity), dpsEntity.getEntityName());
}
public static String getEntityURI(String publicationId, EntityType entityType, String name) {
String typeURIName = "article";
if (entityType.equals((Object)EntityType.COLLECTION)) {
typeURIName = "collection";
} else if (entityType.equals((Object)EntityType.BANNER)) {
typeURIName = "banner";
} else if (entityType.equals((Object)EntityType.LAYOUT)) {
typeURIName = "layout";
}
return "/publication/" + publicationId + "/" + typeURIName + "/" + name;
}
public static EntityType getEntityType(DPSEntity dpsEntity) {
EntityType entityType = EntityType.ARTICLE;
if (dpsEntity instanceof DPSCollection) {
entityType = EntityType.COLLECTION;
} else if (dpsEntity instanceof DPSBanner) {
entityType = EntityType.BANNER;
}
return entityType;
}
public static File createTempFile(String name, String label, String fileExtension) throws IOException {
String filename = "aem-" + label + "-" + name;
File file = File.createTempFile(filename, fileExtension);
LOGGER.debug("Created temp file " + file.getAbsolutePath());
return file;
}
public static String getAndAssertProjectId(DPSObject dpsObject) throws DPSException {
String projectId = dpsObject.getProjectId();
if (projectId == null) {
throw new DPSException("Project not set for " + dpsObject.getPath());
}
return projectId;
}
public static String getArticleImportRoot(DPSProject dpsProject) {
return dpsProject.getPath() + "/" + "articles" + "/" + "imported";
}
public static String getBannerImportRoot(DPSProject dpsProject) {
return dpsProject.getPath() + "/" + "banners" + "/" + "imported";
}
public static String getCollectionImportRoot(DPSProject dpsProject) {
return dpsProject.getPath() + "/" + "collections" + "/" + "imported";
}
public static String getNestedExceptionMessage(Throwable ex) {
StringBuffer messageBuf = new StringBuffer();
messageBuf.append(ex.getMessage());
if (ex.getCause() != null) {
messageBuf.append(LINE_SEP);
messageBuf.append(DPSUtil.getNestedExceptionMessage(ex.getCause()));
}
return messageBuf.toString();
}
}