KernelConfigServlet.java 4.58 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  org.apache.commons.collections.Predicate
 *  org.apache.commons.collections.iterators.FilterIterator
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.contexthub.impl;

import com.adobe.granite.contexthub.api.ContextHub;
import com.adobe.granite.contexthub.api.Store;
import com.adobe.granite.contexthub.impl.StoreConfigFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.lang3.StringUtils;
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.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
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.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(resourceTypes={"/libs/granite/contexthub/cloudsettings/components/baseconfiguration"}, selectors={"config_kernel"}, extensions={"json"}, methods={"GET"})
public class KernelConfigServlet
extends SlingSafeMethodsServlet {
    private static final long serialVersionUID = 6670736259060128610L;
    private static final Logger log = LoggerFactory.getLogger(KernelConfigServlet.class);
    @Reference
    private ContextHub contextHub;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ValueMap vm = ResourceUtil.getValueMap((Resource)request.getResource());
        try {
            Boolean isDebug = (Boolean)vm.get("debug", (Object)false);
            if (this.contextHub.isSilentMode()) {
                isDebug = false;
            }
            JSONObject config = new JSONObject();
            config.put("debug", (Object)isDebug);
            config.put("initializationTimeout", vm.get("initializationTimeout", (Object)2000));
            JSONObject stores = new JSONObject();
            config.put("stores", (Object)stores);
            Iterator childrenIt = request.getResource().listChildren();
            FilterIterator configIt = new FilterIterator(childrenIt, (Predicate)new StoreConfigFilter());
            while (configIt.hasNext()) {
                Resource storeConfigRes = (Resource)configIt.next();
                Store store = new Store(storeConfigRes);
                if (!store.isEnabled()) continue;
                ValueMap storeConfigVm = ResourceUtil.getValueMap((Resource)storeConfigRes);
                JSONObject storeConfigItem = new JSONObject();
                stores.put(storeConfigRes.getName(), (Object)storeConfigItem);
                storeConfigItem.put("type", storeConfigVm.get("storeType", String.class));
                storeConfigItem.put("required", storeConfigVm.get("required", (Object)false));
                String configJson = (String)storeConfigVm.get("configjson", String.class);
                if (!StringUtils.isNotEmpty((CharSequence)configJson)) continue;
                JSONObject storeJSONConfig = new JSONObject(configJson);
                storeConfigItem.put("config", (Object)storeJSONConfig);
            }
            response.getWriter().append(config.toString(4));
        }
        catch (JSONException ex) {
            log.warn("Could not create valid JSON config for ContextHub: ", (Throwable)ex);
            response.getWriter().append("{ \"error\": true }");
        }
    }

    protected void bindContextHub(ContextHub contextHub) {
        this.contextHub = contextHub;
    }

    protected void unbindContextHub(ContextHub contextHub) {
        if (this.contextHub == contextHub) {
            this.contextHub = null;
        }
    }
}