FrameworkContentExporter.java 4.85 KB
/*
 * 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";
    }
}