FrameworkContentExporter.java
4.85 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.contentsync.handler.util.RequestResponseFactory
* com.day.cq.wcm.api.Page
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.engine.SlingRequestProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl;
import com.adobe.cq.mobile.angular.data.util.FrameworkContentExporterUtils;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;
import com.day.cq.wcm.api.Page;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.engine.SlingRequestProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FrameworkContentExporter {
private static final Logger LOGGER = LoggerFactory.getLogger(FrameworkContentExporter.class);
private static final String STRING_ENCODING = "UTF-8";
private static final String PN_FRAMEWORK_TYPE = "frameworkType";
private static final String FRAMEWORK_TYPE_ANGULAR = "angular";
private RequestResponseFactory requestResponseFactory;
private SlingRequestProcessor requestProcessor;
private ResourceResolver resourceResolver;
public FrameworkContentExporter(RequestResponseFactory requestResponseFactory, SlingRequestProcessor requestProcessor, ResourceResolver resourceResolver) {
this.requestResponseFactory = requestResponseFactory;
this.requestProcessor = requestProcessor;
this.resourceResolver = resourceResolver;
}
public JSONObject exportAsJSON(Page page, Map<String, Object> exportParams, String frameworkType) throws JSONException {
JSONObject ngPageRoot = new JSONObject();
Resource pageContent = page.getContentResource();
this.collectJSONObject(ngPageRoot, pageContent, exportParams, frameworkType);
return ngPageRoot;
}
private void collectJSONObject(JSONObject jsonObject, Resource resource, Map<String, Object> exportParams, String frameworkType) throws JSONException {
ValueMap resourceValueMap = ResourceUtil.getValueMap((Resource)resource);
String resourceFrameworkType = (String)resourceValueMap.get("frameworkType", String.class);
if (frameworkType != null && frameworkType.equals(resourceFrameworkType)) {
String relativeComponentPath = FrameworkContentExporterUtils.getRelativeComponentPath(resource.getPath());
jsonObject.put(relativeComponentPath, (Object)this.getResourceJSON(resource, exportParams, frameworkType));
}
Iterator resourceIterator = resource.listChildren();
while (resourceIterator.hasNext()) {
Resource childResource = (Resource)resourceIterator.next();
this.collectJSONObject(jsonObject, childResource, exportParams, frameworkType);
}
}
private JSONObject getResourceJSON(Resource resource, Map<String, Object> exportParams, String frameworkType) {
JSONObject jsonObject = null;
String requestURL = null;
try {
if (this.isSupported(frameworkType)) {
requestURL = resource.getPath() + this.getRequestSuffix(frameworkType);
HttpServletRequest request = this.requestResponseFactory.createRequest("GET", requestURL, exportParams);
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse response = this.requestResponseFactory.createResponse((OutputStream)out);
this.requestProcessor.processRequest(request, response, this.resourceResolver);
jsonObject = new JSONObject(new String(out.toByteArray(), "UTF-8"));
}
}
catch (Exception ex) {
LOGGER.warn("Failed to export resource as JSON " + (Object)resource + "," + requestURL, (Throwable)ex);
}
return jsonObject;
}
private boolean isSupported(String frameworkType) {
boolean supported = false;
if ("angular".equals(frameworkType)) {
supported = true;
}
return supported;
}
private String getRequestSuffix(String framework) {
return "." + framework + ".json";
}
}